当前位置:   article > 正文

Mybatis操作数据库字段与实体类属性的映射关系_mybatis不为空字段必须映射

mybatis不为空字段必须映射

一、mybatis单表查询

   如果实体类属性名称和数据库字段的名称不一致,会出现<在执行查询时,有的字段会出现null值,但是不会报错! >

1、xml配置

  1.1通过resultMap 手动映射

  1. <resultMap id="roleMap" type="role">
  2. <!--id:主键字段
  3. property:实体中的属性名称
  4. column:数据库字段的名称-->
  5. <id property="id" column="id"></id>
  6. <!--
  7. result:普通字段手动赋值
  8. property:实体中的属性名称
  9. column:数据库字段的名称-->
  10. <result property="roleName" column="role_name"></result>
  11. <result property="roleDesc" column="role_desc"></result>
  12. </resultMap>
  13. <select id="findAll" resultMap="roleMap">
  14. select * from role
  15. </select>

  1.2如果是因为驼峰式命名导致映射值为空,那么只需要开启驼峰式命名即可

  1. <settings>
  2. <!--开启驼峰式命名-->
  3. <setting name="mapUnderscoreToCamelCase" value="true"/>
  4. </settings>

 2、@注解配置

  2.1 一对一

  1. @Data
  2. public class Orders {
  3. private Integer id;
  4. private Integer uid;
  5. private String ordertime;
  6. private double money;
  7. /*
  8. * 查询订单的时候 关联出该订单的所属人员信息
  9. * */
  10. private User user1;
  11. }

 

  1. /*
  2. * 根据订单号查询订单信息
  3. *
  4. * */
  5. @Select("select * from orders where id =#{id}")
  6. @Results({
  7. @Result(id = true, property = "id", column = "id"),
  8. @Result(property = "user1", javaType = User.class,
  9. one = @One(select = "cn.yunhe.mapper.UserMapper.findUserByUid"), column = "uid")
  10. })
  11. Orders findOrdersById(Integer id);

   2.2 一对多

  1. @Data
  2. public class Dept {
  3. private Integer deptno;
  4. private String dname;
  5. private String loc;
  6. private List<Emp> empList;// 该部门下的员工数据
  7. }

 

  1. public interface DeptMapper {
  2. /*
  3. * 根据部门编号查询部门信息
  4. * select * from emp where deptno = deptno
  5. * */
  6. @Results({
  7. @Result(id = true,property = "deptno",column = "deptno"),
  8. @Result(property = "empList",javaType = List.class,many = @Many(select = "cn.yunhe.mapper.EmpMapper.findEmpByDeptno",fetchType = FetchType.LAZY),column = "deptno")
  9. })
  10. @Select("select * from dept where deptno=#{deptno}")
  11. Dept findDeptByDeptno(Integer deptno);
  12. }
  1. public interface EmpMapper {
  2. /*select * from emp where deptno =1*/
  3. /*
  4. * 根据部门编号查询该部门下的所属员工信息
  5. * */
  6. @Select("select * from emp where deptno =#{deptno}")
  7. List<Emp> findEmpByDeptno(Integer deptno);
  8. }

  2.3 多对多

  1. @Data
  2. public class Role {
  3. private Integer id;
  4. private String roleName;
  5. private String roleDesc;
  6. }

 

  1. @Data
  2. public class User {
  3. private Integer id;
  4. private String username;
  5. private String birthday;
  6. private String sex;
  7. private String address;
  8. /*
  9. * 用户有多种角色
  10. * */
  11. List<Role> roleList;
  12. }

 

  1. public interface RoleMapper {
  2. /*
  3. * 根据用户的编号查询用户的角色
  4. * */
  5. @Select("select * from role r,user_role ur where r.`id` =ur.`rid` and ur.`uid` =#{id}")
  6. @Results({
  7. @Result(property = "roleName",column = "role_name"),
  8. @Result(property = "roleDesc",column = "role_desc")
  9. })
  10. List<Role> findRoleByUid(Integer id);
  11. }
  1. public interface UserMapper {
  2. /*
  3. * 根据用户的Id查询用户的信息 同时关联出用户拥有的角色
  4. * */
  5. @Select("select * from user where id =#{id}")
  6. @Results({
  7. @Result(id = true,property = "id",column = "id"),
  8. @Result(property = "roleList",javaType = List.class,many = @Many(select = "cn.yunhe.mapper.RoleMapper.findRoleByUid"),column = "id")
  9. })
  10. }

 

 

二、多表查询

  1.查询一个订单,与此同时查询出该订单所属的用户<一对一>

  1. Order实体类
  2. @Data
  3. public class Orders {
  4. private Integer id;
  5. private Integer uid;
  6. private String ordertime;
  7. private double money;
  8. /*来表示 1对1的关系*/
  9. private User user;
  10. }
  1. <mapper namespace="cn.yunhe.mapper.OrdersMapper">
  2. <resultMap id="ordersMap" type="orders">
  3. <!--id标签 封装主键字段
  4. property表示 java类中的属性名称 column 表示 表中的字段名称
  5. result:普通字段赋值-->
  6. <id property="id" column="id"></id>
  7. <result property="uid" column="uid"></result>
  8. <result property="ordertime" column="ordertime"></result>
  9. <result property="money" column="money"></result>
  10. <!--
  11. 来描述1对1关系
  12. association:给1的一方做映射
  13. property:表示 Java类中一的一方的属性名称
  14. javaType:该属性的java的类型
  15. -->
  16. <association property="user" javaType="cn.yunhe.entity.User">
  17. <id property="id" column="uid"></id>
  18. <result property="username" column="username"></result>
  19. <result property="birthday" column="birthday"></result>
  20. <result property="sex" column="sex"></result>
  21. <result property="address" column="address"></result>
  22. </association>
  23. </resultMap>
  24. <select id="findOrder" resultMap="ordersMap">
  25. select
  26. *,u.id uid
  27. from
  28. orders o,user u
  29. where o.`uid` =u.`id`
  30. and o.`id`=#{id};
  31. </select>
  32. </mapper>

  2.<一对多>

  1. @Data
  2. public class Dept {
  3. private Integer deptno;
  4. private String dname;
  5. private String loc;
  6. /*
  7. * 在一个部门下有多个员工
  8. * */
  9. private List<Emp> empList;
  10. }
  1. public interface DeptMapper {
  2. /*
  3. * 根据部门名称查询部门信息
  4. * */
  5. Dept findDept(String dname);
  6. }
  1. <mapper namespace="cn.yunhe.mapper.DeptMapper">
  2. <!--
  3. autoMapping:自定映射
  4. 只要数据表中的字段名称和实体中的属性的名称一致就可以自动进行映射
  5. -->
  6. <resultMap id="DeptMap" type="dept" autoMapping="true">
  7. <!--主表dept表的赋值-->
  8. <id property="deptno" column="deptno"></id>
  9. <!-- <result property="dname" column="dname"></result>
  10. <result property="loc" column="loc"></result>-->
  11. <!--
  12. property:java类中属性的名称
  13. ofType:集合中泛型的数据类型
  14. -->
  15. <collection property="empList" ofType="emp" autoMapping="true">
  16. <!--从表 多的一方的emp的赋值-->
  17. <id property="empno" column="empno"></id>
  18. <!-- <result property="ename" column="ename"></result>
  19. <result property="salary" column="salary"></result>
  20. <result property="hiredate" column="hiredate"></result>-->
  21. </collection>
  22. </resultMap>
  23. <select id="findDept" resultMap="DeptMap">
  24. select
  25. *
  26. from
  27. emp e,dept d
  28. where e.deptno = d.`deptno`
  29. and d.`dname` = #{dname}
  30. </select>
  31. </mapper>

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/625658
推荐阅读
相关标签
  

闽ICP备14008679号