当前位置:   article > 正文

Mybatis—多表查询_mybatis 多表查询

mybatis 多表查询

目录

一、多表查询操作

1.1什么是多表查询

 1.2实现多表查询的准备

建立用户表

建立订单表

建立订单详情表

创建和表对应的java类

1.3一对一和一对多

 接口中写方法

UserMapper中手动映射关系

测试类

运行结果

1.4多对多 

 接口中写方法

OrderMapper中手动映射

测试类

运行结果


一、多表查询操作

1.1什么是多表查询

数据库关系是指数据库中不同表之间的关系或连接方式。数据库关系可以分为以下几种类型:

  1. 一对一关系(One-to-One):一个实体只能与另一个实体关联,而另一个实体也只能与一个实体关联。

  2. 一对多关系(One-to-Many):一个实体可以与多个实体关联,而多个实体只能与一个实体关联。

  3. 多对多关系(Many-to-Many):多个实体可以与多个实体关联。

在数据库中,一对一关系常用于将大型的表拆分为多个较小的表,以提高查询效率和减少冗余数据。一对多关系常用于将关系型数据库中的实体拆分为多个相关联的表。多对一关系常用于表示一个实体对应多个实体的情况,比如一个部门对应多个员工。多对多关系用于表示多个实体之间的复杂关系,需要使用中间表来记录实体之间的关联关系。

数据库关系的定义和实现都是通过使用外键来实现的。外键是一种指向其他表的字段,它用于建立表与表之间的关联关系。通过使用外键,可以在对一个表进行查询时,同时获取与之相关联的其他表的数据。

 1.2实现多表查询的准备

建立用户表

建立订单表

建立订单详情表

创建和表对应的java类

用户类

  1. package com.ba.domain;
  2. /**
  3. * 用户类
  4. */
  5. public class User {
  6. private int id;
  7. private String username;
  8. private String password;
  9. private String gender;
  10. private String hobby;
  11. private String address;
  12. private String remark;
  13. private Order order;
  14. public User() {
  15. }
  16. public User(String username, String password, String gender, String hobby, String address, String remark) {
  17. this.username = username;
  18. this.password = password;
  19. this.gender = gender;
  20. this.hobby = hobby;
  21. this.address = address;
  22. this.remark = remark;
  23. }
  24. public int getId() {
  25. return id;
  26. }
  27. public void setId(int id) {
  28. this.id = id;
  29. }
  30. public String getUsername() {
  31. return username;
  32. }
  33. public void setUsername(String username) {
  34. this.username = username;
  35. }
  36. public String getPassword() {
  37. return password;
  38. }
  39. public void setPassword(String password) {
  40. this.password = password;
  41. }
  42. public String getGender() {
  43. return gender;
  44. }
  45. public void setGender(String gender) {
  46. this.gender = gender;
  47. }
  48. public String getHobby() {
  49. return hobby;
  50. }
  51. public void setHobby(String hobby) {
  52. this.hobby = hobby;
  53. }
  54. public String getAddress() {
  55. return address;
  56. }
  57. public void setAddress(String address) {
  58. this.address = address;
  59. }
  60. public String getRemark() {
  61. return remark;
  62. }
  63. public void setRemark(String remark) {
  64. this.remark = remark;
  65. }
  66. public Order getOrder() {
  67. return order;
  68. }
  69. public void setOrder(Order order) {
  70. this.order = order;
  71. }
  72. }

订单类

  1. package com.ba.domain;
  2. import java.util.List;
  3. /**
  4. * 订单类
  5. */
  6. public class Order {
  7. private int o_id;
  8. private String o_name;
  9. private int o_count;
  10. private User user;
  11. private List<OrderDetails> orderDetailsList;
  12. public int getO_id() {
  13. return o_id;
  14. }
  15. public void setO_id(int o_id) {
  16. this.o_id = o_id;
  17. }
  18. public String getO_name() {
  19. return o_name;
  20. }
  21. public void setO_name(String o_name) {
  22. this.o_name = o_name;
  23. }
  24. public int getO_count() {
  25. return o_count;
  26. }
  27. public void setO_count(int o_count) {
  28. this.o_count = o_count;
  29. }
  30. public User getUser() {
  31. return user;
  32. }
  33. public void setUser(User user) {
  34. this.user = user;
  35. }
  36. public List<OrderDetails> getOrderDetailsList() {
  37. return orderDetailsList;
  38. }
  39. public void setOrderDetailsList(List<OrderDetails> orderDetailsList) {
  40. this.orderDetailsList = orderDetailsList;
  41. }
  42. }

订单详情类

  1. package com.ba.domain;
  2. import java.util.List;
  3. /**
  4. * 订单详情类
  5. */
  6. public class OrderDetails {
  7. private int details_id;
  8. private String trade_name;
  9. private int unit_price;
  10. private List<Order> orderList;
  11. public int getDetails_id() {
  12. return details_id;
  13. }
  14. public void setDetails_id(int details_id) {
  15. this.details_id = details_id;
  16. }
  17. public String getTrade_name() {
  18. return trade_name;
  19. }
  20. public void setTrade_name(String trade_name) {
  21. this.trade_name = trade_name;
  22. }
  23. public int getUnit_price() {
  24. return unit_price;
  25. }
  26. public void setUnit_price(int unit_price) {
  27. this.unit_price = unit_price;
  28. }
  29. public List<Order> getOrderList() {
  30. return orderList;
  31. }
  32. public void setOrderList(List<Order> orderList) {
  33. this.orderList = orderList;
  34. }
  35. }

1.3一对一和一对多

一个订单只会有一个用户(一对一关系)

一个用户可以用多个订单(一对多关系)

 接口中写方法

  1. package com.ba.mapper;
  2. import com.ba.domain.Order;
  3. import com.ba.domain.QueryUser;
  4. import com.ba.domain.User;
  5. import java.util.List;
  6. public interface UserMapper {
  7. public List<Order> findUserOrder();
  8. }

UserMapper中手动映射关系

  1. <resultMap id="userorder" type="com.ba.domain.Order">
  2. <id column="o_id" property="o_id"></id>
  3. <result column="o_name" property="o_name"></result>
  4. <result column="o_count" property="o_count"></result>
  5. <association property="user" javaType="com.ba.domain.User">
  6. <id column="id" property="id"></id>
  7. <result column="username" property="username"></result>
  8. <result column="address" property="address"></result>
  9. </association>
  10. </resultMap>
  11. <select id="findUserOrder" resultMap="userorder">
  12. SELECT o.o_id,o.o_name,o.o_count,u.id,u.username,u.address FROM orders o JOIN USER u ON u.id = o.u_id
  13. </select>

测试类

  1. @Test
  2. public void test9() {
  3. SqlSession sqlSession = ssf.openSession();
  4. UserMapper mapper = sqlSession.getMapper(UserMapper.class);
  5. List<Order> userOrder = mapper.findUserOrder();
  6. for (Order order: userOrder) {
  7. System.out.println(order.getO_id()+","+order.getO_name()+","+order.getO_count()+","+order.getUser().getId()+","+order.getUser().getUsername()+","+order.getUser().getAddress());
  8. }
  9. }

运行结果

1.4多对多 

一个用户可以有多个订单而一个订单又可以有多个商品简单来说多对多就是至少两个一对多的关系组成的

找到关系中的主体(Order)

 接口中写方法

  1. package com.ba.mapper;
  2. import com.ba.domain.Order;
  3. import com.ba.domain.QueryOrderUser;
  4. import java.util.List;
  5. public interface OrderMapper {
  6. public List<Order> findOrderUserOrderDetails();
  7. }

OrderMapper中手动映射

  1. <!--查询用户所有订单的详细信息-->
  2. <resultMap id="OrderUserOrderDetails" type="com.ba.domain.Order">
  3. <id column="o_id" property="o_id"></id>
  4. <result column="o_name" property="o_name"></result>
  5. <result column="o_count" property="o_count"></result>
  6. <association property="user" javaType="User">
  7. <result column="username" property="username"></result>
  8. <result column="address" property="address"></result>
  9. </association>
  10. <collection property="orderDetailsList" ofType="com.ba.domain.OrderDetails">
  11. <id column="details_id" property="details_id"></id>
  12. <result column="trade_name" property="trade_name"></result>
  13. <result column="unit_price" property="unit_price"></result>
  14. </collection>
  15. </resultMap>
  16. <select id="findOrderUserOrderDetails" resultMap="OrderUserOrderDetails">
  17. SELECT
  18. o.o_id,
  19. o.o_name,
  20. o.o_count,
  21. u.username,
  22. u.address,
  23. od.details_id,
  24. od.trade_name,
  25. od.unit_price
  26. FROM
  27. orders o,
  28. orderdetails od,
  29. USER u
  30. WHERE
  31. u.id=o.u_id
  32. AND
  33. od.o_id=o.o_id
  34. </select>

测试类

  1. //多对多
  2. @Test
  3. public void test8() {
  4. SqlSession sqlSession = ssf.openSession();
  5. OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
  6. List<Order> orderUserOrderDetails = mapper.findOrderUserOrderDetails();
  7. for (Order orderUserOrderDetail : orderUserOrderDetails) {
  8. System.out.println(orderUserOrderDetail.getO_id()+","+orderUserOrderDetail.getO_count()+","+orderUserOrderDetail.getO_count()+","+orderUserOrderDetail.getUser().getUsername()+","+orderUserOrderDetail.getUser().getAddress());
  9. List<OrderDetails> orderDetailsList = orderUserOrderDetail.getOrderDetailsList();
  10. for (OrderDetails orderDetail : orderDetailsList) {
  11. System.out.println("\t"+orderDetail.getDetails_id()+","+orderDetail.getTrade_name()+","+orderDetail.getUnit_price());
  12. }
  13. }
  14. }

运行结果

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/煮酒与君饮/article/detail/830557
推荐阅读
相关标签
  

闽ICP备14008679号