1.Mybatis多表查询

1.1 一对一查询

1. 一对一查询的模型
用户表和订单表的关系为,一个用户有多个订单,一个订单只从属于一个用户
一对一查询的需求:查询一个订单,与此同时查询出该订单所属的用户

05_Mybatis的多表操作 - 图1

2. 一对一查询的语句
对应的sql语句:select * from orders o,user u where o.uid=u.id;
查询的结果如下:

05_Mybatis的多表操作 - 图2

3. 创建Order和User实体

  1. public class Order {
  2. private int id;
  3. private Date ordertime;
  4. private double total;
  5. //当前订单属于哪一个用户
  6. private User user;
  1. public class User {
  2. private int id;
  3. private String username;
  4. private String password;
  5. private Date birthday;

4. 创建OrderMapper接口

  1. public interface OrderMapper {
  2. //查询全部的方法
  3. public List<Order> findAll();
  4. }

5. 配置OrderMapper.xml

  1. <mapper namespace="com.itheima.mapper.OrderMapper">
  2. <resultMap id="orderMap" type="order">
  3. <!--手动指定字段与实体属性的映射关系
  4. column: 数据表的字段名称
  5. property:实体的属性名称
  6. -->
  7. <id column="oid" property="id"></id>
  8. <result column="ordertime" property="ordertime"></result>
  9. <result column="total" property="total"></result>
  10. <!--<result column="uid" property="user.id"></result>
  11. <result column="username" property="user.username"></result>
  12. <result column="password" property="user.password"></result>
  13. <result column="birthday" property="user.birthday"></result>-->
  14. <!--
  15. property: 当前实体(order)中的属性名称(private User user)
  16. javaType: 当前实体(order)中的属性的类型(User)
  17. -->
  18. <association property="user" javaType="user">
  19. <id column="uid" property="id"></id>
  20. <result column="username" property="username"></result>
  21. <result column="password" property="password"></result>
  22. <result column="birthday" property="birthday"></result>
  23. </association>
  24. </resultMap>
  25. <select id="findAll" resultMap="orderMap">
  26. SELECT *,o.id oid FROM orders o,USER u WHERE o.uid=u.id
  27. </select>
  28. </mapper>

6. 测试结果

  1. OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
  2. List<Order> orderList = mapper.findAll();
  3. for (Order order : orderList) {
  4. System.out.println(order);
  5. }

05_Mybatis的多表操作 - 图3

1.2 一对多查询

1. 一对多查询的模型
用户表和订单表的关系为,一个用户有多个订单,一个订单只从属于一个用户
一对多查询的需求:查询一个用户,与此同时查询出该用户具有的订单

05_Mybatis的多表操作 - 图4

2. 一对多查询的语句
对应的sql语句:select *,o.id oid from user u left join orders o on u.id=o.uid;
查询的结果如下:

05_Mybatis的多表操作 - 图5

3. 修改User实体

  1. public class Order {
  2. private int id;
  3. private Date ordertime;
  4. private double total;
  5. //当前订单属于哪一个用户
  6. private User user;
  1. public class User {
  2. private int id;
  3. private String username;
  4. private String password;
  5. private Date birthday;
  6. //描述的是当前用户存在哪些订单
  7. private List<Order> orderList;

4. 创建UserMapper接口

  1. public interface UserMapper {
  2. public List<User> findAll();
  3. }

5. 配置UserMapper.xml

  1. <mapper namespace="com.itheima.mapper.UserMapper">
  2. <resultMap id="userMap" type="user">
  3. <id column="uid" property="id"></id>
  4. <result column="username" property="username"></result>
  5. <result column="password" property="password"></result>
  6. <result column="birthday" property="birthday"></result>
  7. <!--配置集合信息
  8. property:集合名称
  9. ofType:当前集合中的数据类型
  10. -->
  11. <collection property="orderList" ofType="order">
  12. <!--封装order的数据-->
  13. <id column="oid" property="id"></id>
  14. <result column="ordertime" property="ordertime"></result>
  15. <result column="total" property="total"></result>
  16. </collection>
  17. </resultMap>
  18. <select id="findAll" resultMap="userMap">
  19. SELECT *,o.id oid FROM USER u,orders o WHERE u.id=o.uid
  20. </select>
  21. </mapper>

6. 测试结果

  1. UserMapper mapper = sqlSession.getMapper(UserMapper.class);
  2. List<User> orderList = mapper.findAll();
  3. for (User user : orderList) {
  4. System.out.println(user);
  5. }

输出

  1. User{id=1, username='zhangsan', password='123', birthday=null, orderList=[Order{id=1, ordertime=Tue Apr 12 16:52:52 CST 2022, total=3000.0, user=null}, Order{id=2, ordertime=Sun Jun 12 16:53:12 CST 2022, total=5800.0, user=null}]}
  2. User{id=2, username='wangwu', password='123', birthday=Tue Apr 12 17:33:43 CST 2022, orderList=[Order{id=3, ordertime=Fri Apr 29 16:53:37 CST 2022, total=2456.0, user=null}]}
  3. User{id=6, username='ceshi', password='abc', birthday=Tue Apr 12 17:33:37 CST 2022, orderList=[Order{id=4, ordertime=Tue Feb 12 17:21:51 CST 2019, total=9999.0, user=null}]}

1.3 多对多查询

1. 多对多查询的模型
用户表和角色表的关系为,一个用户有多个角色,一个角色被多个用户使用
多对多查询的需求:查询用户同时查询出该用户的所有角色

05_Mybatis的多表操作 - 图6

2. 多对多查询的语句
对应的sql语句:SELECT * FROM USER u,sys_user_role ur,sys_role r WHERE u.id=ur.userId AND ur.roleId=r.id

查询的结果如下:

05_Mybatis的多表操作 - 图7

3. 创建Role实体,修改User实体

  1. public class User {
  2. private int id;
  3. private String username;
  4. private String password;
  5. private Date birthday;
  6. //代表当前用户具备哪些订单
  7. private List<Order> orderList;
  8. //代表当前用户具备哪些角色
  9. private List<Role> roleList;
  10. }
  1. public class Role {
  2. private int id;
  3. private String rolename;
  4. }

4. 添加UserMapper接口方法

  1. List<User> findAllUserAndRole();

5. 配置UserMapper.xml

  1. <resultMap id="userRoleMap" type="user">
  2. <!--user的信息-->
  3. <id column="userId" property="id"></id>
  4. <result column="username" property="username"></result>
  5. <result column="password" property="password"></result>
  6. <result column="birthday" property="birthday"></result>
  7. <!--user内部的roleList信息-->
  8. <collection property="roleList" ofType="role">
  9. <id column="roleId" property="id"></id>
  10. <result column="roleName" property="roleName"></result>
  11. <result column="roleDesc" property="roleDesc"></result>
  12. </collection>
  13. </resultMap>
  14. <select id="findUserAndRoleAll" resultMap="userRoleMap">
  15. SELECT * FROM USER u,sys_user_role ur,sys_role r WHERE u.id=ur.userId AND ur.roleId=r.id
  16. </select>

6. 测试结果

  1. UserMapper mapper = sqlSession.getMapper(UserMapper.class);
  2. List<User> userAndRoleAll = mapper.findUserAndRoleAll();
  3. for (User user : userAndRoleAll) {
  4. System.out.println(user);
  5. }

1.4 知识小结

MyBatis多表配置方式:
一对一配置:使用<resultMap>做配置
一对多配置:使用<resultMap>+<collection>做配置
多对多配置:使用<resultMap>+<collection>做配置