网站制作首选 新鸿儒,老薛主机wordpress慢,wordpress 获取当前文章的浏览量,百度推广seo软件目录#xff1a; 第一题. 实体类属性名和表中字段名不⼀样 #xff0c;怎么办?第二题. Mybatis是否可以映射Enum枚举类#xff1f;第三题. Mybatis能执⾏⼀对⼀、⼀对多的关联查询吗#xff1f;第四题. Mybatis是否⽀持延迟加载#xff1f;原理#xff1f;第五题. 如何获… 目录 第一题. 实体类属性名和表中字段名不⼀样 怎么办?第二题. Mybatis是否可以映射Enum枚举类第三题. Mybatis能执⾏⼀对⼀、⼀对多的关联查询吗第四题. Mybatis是否⽀持延迟加载原理第五题. 如何获取⽣成的主键? 第一题. 实体类属性名和表中字段名不⼀样 怎么办?
第1种 通过在查询的SQL语句中定义字段名的别名让字段名的别名和实体类的属性名⼀致。
select idgetOrder parameterTypeint resultTypecom.jourwon.pojo.Orderselect order_id id, order_no orderno ,order_price price form orders where
order_id#{id};
/select第2种 通过resultMap 中的来映射字段名和实体类属性名的⼀⼀对应的关系。
select idgetOrder parameterTypeint resultMaporderResultMapselect * from orders where order_id#{id}
/select
resultMap typecom.jourwon.pojo.Order idorderResultMap!–⽤id属性来映射主键字段–id propertyid columnorder_id!–⽤result属性来映射⾮主键字段property为实体类属性名column为数据库表中的属性–result property orderno column order_no/result propertyprice columnorder_price /
/resultMap第二题. Mybatis是否可以映射Enum枚举类
Mybatis当然可以映射枚举类不单可以映射枚举类Mybatis可以映射任何对象到表的⼀列上。映射⽅式为⾃定义⼀个TypeHandler实现TypeHandler的setParameter()和getResult()接⼝⽅法。TypeHandler有两个作⽤⼀是完成从javaType⾄jdbcType的转换⼆是完成jdbcType⾄javaType的转换体现为setParameter()和getResult()两个⽅法分别代表设置sql问号占位符参数和获取列查询结果。
第三题. Mybatis能执⾏⼀对⼀、⼀对多的关联查询吗
当然可以不⽌⽀持⼀对⼀、⼀对多的关联查询还⽀持多对多、多对⼀的关联查询。 ⼀对⼀ association ⽐如订单和⽀付是⼀对⼀的关系这种关联的实现 实体类:
public class Order {private Integer orderId;private String orderDesc;/*** ⽀付对象*/private Pay pay;//……
}结果映射
!-- 订单resultMap --
resultMap idpeopleResultMap typecn.fighter3.entity.Orderid propertyorderId columnorder_id /result propertyorderDesc columnorder_desc/!--⼀对⼀结果映射--association propertypay javaTypecn.fighter3.entity.Payid columnpayId propertypay_id/result columnaccount propertyaccount//association
/resultMap查询就是普通的关联查
select idgetTeacher resultMapgetTeacherMap parameterTypeintselect * from order oleft join pay p on o.order_idp.order_idwhere o.order_id#{orderId}
/select⼀对多 collection ⽐如商品分类和商品是⼀对多的关系。
实体类
public class Category {private int categoryId;private String categoryName;/*** 商品列表**/ListProduct products;//……
}结果映射
resultMap typeCategory idcategoryBeanid columncategoryId propertycategory_id /result columncategoryName propertycategory_name /!-- ⼀对多的关系 --!-- property: 指的是集合属性的值, ofType指的是集合中元素的类型 --collection propertyproducts ofTypeProductid columnproduct_id propertyproductId /result columnproductName propertyproductName /result columnprice propertyprice //collection
/resultMap查询 查询就是⼀个普通的关联查询
!-- 关联查询分类和产品表 --
select idlistCategory resultMapcategoryBeanselect c.*, p.* from category_ c left join product_ p on c.id p.cid
/select多对一关系映射
在多对一关系中一个实体对象可以关联到另一个实体对象但是另一个实体对象只能关联到一个实体对象。比如订单(Order)和客户(Customer)之间的关系。 实体类
public class Order {private int orderId;private String orderName;private Customer customer;// getters and setters
}public class Customer {private int customerId;private String customerName;// getters and setters
}结果映射
resultMap typeOrder idorderResultMapid propertyorderId columnorder_id/result propertyorderName columnorder_name/!-- 多对一关系 --association propertycustomer javaTypeCustomerid propertycustomerId columncustomer_id/result propertycustomerName columncustomer_name//association
/resultMap多对多关系映射
在多对多关系中一个实体对象可以关联到多个另一个实体对象反之亦然。比如学生(Student)和课程(Course)之间的关系。
实体类
public class Student {private int studentId;private String studentName;private ListCourse courses;// getters and setters
}public class Course {private int courseId;private String courseName;private ListStudent students;// getters and setters
}结果映射
resultMap typeStudent idstudentResultMapid propertystudentId columnstudent_id/result propertystudentName columnstudent_name/!-- 多对多关系 --collection propertycourses ofTypeCourseid propertycourseId columncourse_id/result propertycourseName columncourse_name//collection
/resultMapresultMap typeCourse idcourseResultMapid propertycourseId columncourse_id/result propertycourseName columncourse_name/!-- 多对多关系 --collection propertystudents ofTypeStudentid propertystudentId columnstudent_id/result propertystudentName columnstudent_name//collection
/resultMap第四题. Mybatis是否⽀持延迟加载原理
Mybatis⽀持association关联对象和collection关联集合对象的延迟加载association指的就是⼀对⼀collection指的就是⼀对多查询。在Mybatis配置⽂件中可以配置是否启⽤延迟加载lazyLoadingEnabledtrue|false。它的原理是使⽤CGLIB创建⽬标对象的代理对象当调⽤⽬标⽅法时进⼊拦截器⽅法⽐如调⽤a.getB().getName()拦截器invoke()⽅法发现a.getB()是null值那么就会单独发送事先保存好的查询关联B对象的sql把B查询上来然后调⽤a.setB(b)于是a的对象b属性就有值了接着完成a.getB().getName()⽅法的调⽤。这就是延迟加载的基本原理。当然了不光是Mybatis⼏乎所有的包括Hibernate⽀持延迟加载的原理都是⼀样的。
第五题. 如何获取⽣成的主键?
新增标签中添加keyProperty ID 即可
insert idinsert useGeneratedKeystrue keyPropertyuserId insert into user(user_name, user_password, create_time)values(#{userName}, #{userPassword} , #{createTime, jdbcType TIMESTAMP})
/insert这时候就可以完成回填主键
mapper.insert(user);
user.getId;如果我的内容对你有帮助请点赞评论收藏。创作不易大家的支持就是我坚持下去的动力