Mybatis 多表查询

时间:2024-11-20 07:50:59

使用MyBatis这一强大的框架可以解决很多赋值的问题,其中对于子配置文件中的小细节有很多需要注意的地方,使用这个框架后主要就是对于配置文件的编写和配置。
今天我写了一个多表查询,表的基本结构如下:
用户表t_user:用户编号uid,账号account,昵称nickname,性别gender
订单表t_order:订单编号oid,下单时间gen_time,总价total_price,用户编号user_id
订单详情表:详情编号id,订单编号order_id,商品编号product_id,下单数量amount
商品表:商品编号pid,商品名称pname,商品价格price,商品描述description
其中订单表user_id是外键对应用户表的uid;订单详情表的order_id,product_id分别对应订单表的oid和商品表的pid

 

程序中分别定义了四个实体来映射这四个表
查询语句是:

  1. SELECT
  2. uid,
  3. account,
  4. nickname,
  5. gender,
  6. oid,
  7. gen_time,
  8. total_price,
  9. user_id,
  10. id,
  11. order_id,
  12. product_id,
  13. amount,
  14. pid,
  15. pname,
  16. price,
  17. description
  18. FROM
  19. t_user
  20. JOIN t_order ON t_user.uid = t_order.user_id
  21. JOIN t_order_product ON t_order_product.order_id = t_order.oid
  22. JOIN t_product ON t_product.pid = t_order_product.product_id
  23. WHERE
  24. uid = 1

 

User类:

  1. package cn.;
  2. import ;
  3. public class User {
  4.     private Long id;
  5.     private String account;
  6.     private String nickname;
  7.     private String gender;
  8.     private List<Order> orders;
  9.     /*
  10.      * (non-Javadoc)
  11.      *
  12.      * @see #toString()
  13.      */
  14.     @Override
  15.     public String toString() {
  16.         return "[用户编号:" + id + ", 账号:" + account + ", 昵称:" + nickname + ", 性别:" + gender + ", 所下订单:"
  17.                 + orders + "]";
  18.     }
  19.     /**
  20.      * @return the orders
  21.      */
  22.     public List<Order> getOrders() {
  23.         return orders;
  24.     }
  25.     /**
  26.      * @param orders
  27.      *            the orders to set
  28.      */
  29.     public void setOrders(List<Order> orders) {
  30.         this.orders = orders;
  31.     }
  32.     public Long getId() {
  33.         return id;
  34.     }
  35.     public void setId(Long id) {
  36.         this.id = id;
  37.     }
  38.     public String getAccount() {
  39.         return account;
  40.     }
  41.     public void setAccount(String account) {
  42.         this.account = account;
  43.     }
  44.     public String getNickname() {
  45.         return nickname;
  46.     }
  47.     public void setNickname(String nickname) {
  48.         this.nickname = nickname;
  49.     }
  50.     public String getGender() {
  51.         return gender;
  52.     }
  53.     public void setGender(String gender) {
  54.         this.gender = gender;
  55.     }
  56. }

 

 

Order类:

 

  1. package cn.;
  2. import ;
  3. import ;
  4. public class Order {
  5. private Long id;
  6. private Date genTime;
  7. private Double totalPrice;
  8. private Long userId;
  9. private List<Orderitems> orderitems;
  10. /*
  11. * (non-Javadoc)
  12. *
  13. * @see #toString()
  14. */
  15. @Override
  16. public String toString() {
  17. return "订单详情 [订单号:" + id + ", 下单时间:" + genTime + ", 订单总价:" + totalPrice + ", 所含商品:" + orderitems + "]";
  18. }
  19. /**
  20. * @return the orderitems
  21. */
  22. public List<Orderitems> getOrderitems() {
  23. return orderitems;
  24. }
  25. /**
  26. * @param orderitems
  27. * the orderitems to set
  28. */
  29. public void setOrderitems(List<Orderitems> orderitems) {
  30. this.orderitems = orderitems;
  31. }
  32. public Long getId() {
  33. return id;
  34. }
  35. public void setId(Long id) {
  36. this.id = id;
  37. }
  38. public Date getGenTime() {
  39. return genTime;
  40. }
  41. public void setGenTime(Date genTime) {
  42. this.genTime = genTime;
  43. }
  44. public Double getTotalPrice() {
  45. return totalPrice;
  46. }
  47. public void setTotalPrice(Double totalPrice) {
  48. this.totalPrice = totalPrice;
  49. }
  50. public Long getUserId() {
  51. return userId;
  52. }
  53. public void setUserId(Long userId) {
  54. this.userId = userId;
  55. }
  56. }

 

Orderitems类:

  1. package cn.;
  2. import ;
  3. public class Orderitems {
  4. private Long id;
  5. private Long oid;
  6. private Long pid;
  7. private Long amount;
  8. private List<Product> product;
  9. /*
  10. * (non-Javadoc)
  11. *
  12. * @see #toString()
  13. */
  14. @Override
  15. public String toString() {
  16. return "[数量:" + amount + product +"]" ;
  17. }
  18. /**
  19. * @return the product
  20. */
  21. public List<Product> getProduct() {
  22. return product;
  23. }
  24. /**
  25. * @param product
  26. * the product to set
  27. */
  28. public void setProduct(List<Product> product) {
  29. this.product = product;
  30. }
  31. public Long getId() {
  32. return id;
  33. }
  34. public void setId(Long id) {
  35. this.id = id;
  36. }
  37. public Long getOid() {
  38. return oid;
  39. }
  40. public void setOid(Long oid) {
  41. this.oid = oid;
  42. }
  43. public Long getPid() {
  44. return pid;
  45. }
  46. public void setPid(Long pid) {
  47. this.pid = pid;
  48. }
  49. public Long getAmount() {
  50. return amount;
  51. }
  52. public void setAmount(Long amount) {
  53. this.amount = amount;
  54. }
  55. }

 

Product类:

  1. package cn.;
  2. public class Product {
  3. private Long id;
  4. private String name;
  5. private Double price;
  6. private String description;
  7. /*
  8. * (non-Javadoc)
  9. *
  10. * @see #toString()
  11. */
  12. @Override
  13. public String toString() {
  14. return "[商品编号:" + id + ", 商品名称:" + name + ", 价格:" + price + ", 描述:" + description + "]";
  15. }
  16. public Long getId() {
  17. return id;
  18. }
  19. public void setId(Long id) {
  20. this.id = id;
  21. }
  22. public String getName() {
  23. return name;
  24. }
  25. public void setName(String name) {
  26. this.name = name;
  27. }
  28. public Double getPrice() {
  29. return price;
  30. }
  31. public void setPrice(Double price) {
  32. this.price = price;
  33. }
  34. public String getDescription() {
  35. return description;
  36. }
  37. public void setDescription(String description) {
  38. this.description = description;
  39. }
  40. }

 

 

用户的实现接口,根据输入ID号查询当前用户的信息以及他的历史订单和订单里所包含的商品明细

也即Dao层接口:

  1. package cn.;
  2. import cn.;
  3. public interface UserMapper {
  4. public User getByIdWithOrdersWithProducts(Long id);
  5. }

 

 用户接口的配置文件

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-////DTD Mapper 3.0//EN"
  4. "/dtd/">
  5. <mapper namespace="cn.">
  6. <resultMap type="user" id="userOrderitemsResultMap">
  7. <id property="id" column="uid" />
  8. <result property="account" column="account" />
  9. <result property="nickname" column="nickname" />
  10. <result property="gender" column="gender" />
  11. <collection property="orders" ofType="Order">
  12. <id property="id" column="oid" />
  13. <result property="genTime" column="gen_time" />
  14. <result property="totalPrice" column="total_price" />
  15. <result property="userId" column="user_id" />
  16. <collection property="orderitems" ofType="Orderitems">
  17. <id property="id" column="id" />
  18. <result property="oid" column="order_id" />
  19. <result property="pid" column="product_id" />
  20. <result property="amount" column="amount" />
  21. <collection property="product" ofType="Product">
  22. <id property="id" column="pid" />
  23. <result property="name" column="pname" />
  24. <result property="price" column="price" />
  25. <result property="description" column="description" />
  26. </collection>
  27. </collection>
  28. </collection>
  29. </resultMap>
  30. <select id="getByIdWithOrdersWithProducts" resultMap="userOrderitemsResultMap"
  31. parameterType="long">
  32. SELECT
  33. uid,
  34. account,
  35. nickname,
  36. gender,
  37. oid,
  38. gen_time,
  39. total_price,
  40. user_id,
  41. id,
  42. order_id,
  43. product_id,
  44. amount,
  45. pid,
  46. pname,
  47. price,
  48. description
  49. FROM
  50. t_user
  51. JOIN t_order ON t_user.uid = t_order.user_id
  52. JOIN t_order_product ON t_order_product.order_id = t_order.oid
  53. JOIN t_product ON t_product.pid = t_order_product.product_id
  54. WHERE
  55. uid = 1
  56. </select>
  57. </mapper>

 

核心配置文件

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE configuration
  3. PUBLIC "-////DTD Config 3.0//EN"
  4. "/dtd/">
  5. <configuration>
  6. <typeAliases>
  7. <typeAlias type="cn." alias="User"/>
  8. <typeAlias type="cn." alias="Product"/>
  9. <typeAlias type="cn." alias="Order"/>
  10. <typeAlias type="cn." alias="Orderitems"/>
  11. </typeAliases>
  12. <environments default="development">
  13. <environment id="development">
  14. <transactionManager type="JDBC" />
  15. <dataSource type="POOLED">
  16. <property name="driver" value="" />
  17. <property name="url" value="jdbc:mysql://localhost:3306/storemybatis?characterEncoding=utf-8" />
  18. <property name="username" value="root" />
  19. <property name="password" value="12580" />
  20. </dataSource>
  21. </environment>
  22. </environments>
  23. <!-- <mappers>
  24. <mapper resource=""/>
  25. </mappers> -->
  26. <mappers>
  27. <package name="cn."/>
  28. </mappers>
  29. </configuration>

 

  1. JUnit测试方法:
  2. package cn.;
  3. import ;
  4. import ;
  5. import ;
  6. import ;
  7. import ;
  8. import ;
  9. import ;
  10. import cn.;
  11. public class UserMapperTest {
  12. private SqlSessionFactory sqlSessionFactory = null;
  13. @Before
  14. public void init() throws IOException {
  15. sqlSessionFactory = new SqlSessionFactoryBuilder().build((""));
  16. }
  17. @Test
  18. public void testGetByIdWithOrdersWithProducts() {
  19. SqlSession sqlSession = ();
  20. UserMapper userImpl = ();
  21. User user = (1L);
  22. (user);
  23. ();
  24. }

基本上面的就可以输出当前用户的信息和所有历史订单以及订单的商品详情