SSM16.4【Mybatis:通过注解实现Mybatis的复杂映射开发之一对多查询】

 

 

SSM16.4【Mybatis:通过注解实现Mybatis的复杂映射开发之一对多查询】

 

 SSM16.4【Mybatis:通过注解实现Mybatis的复杂映射开发之一对多查询】

 

 SSM16.4【Mybatis:通过注解实现Mybatis的复杂映射开发之一对多查询】

 

 SSM16.4【Mybatis:通过注解实现Mybatis的复杂映射开发之一对多查询】

SSM16.4【Mybatis:通过注解实现Mybatis的复杂映射开发之一对多查询】

 SSM16.4【Mybatis:通过注解实现Mybatis的复杂映射开发之一对多查询】

 SSM16.4【Mybatis:通过注解实现Mybatis的复杂映射开发之一对多查询】

 

 

 

 1 package com.haifei.domain;
 2 
 3 import java.util.Date;
 4 import java.util.List;
 5 
 6 public class User {
 7 
 8     private int id;
 9     private String username;
10     private String password;
11     private Date birthday;
12 
13     //描述的是当前用户具有的订单
14     private List<Order> orderList;
15     public List<Order> getOrderList() {
16         return orderList;
17     }
18     public void setOrderList(List<Order> orderList) {
19         this.orderList = orderList;
20     }
21 
22     public Date getBirthday() {
23         return birthday;
24     }
25 
26     public void setBirthday(Date birthday) {
27         this.birthday = birthday;
28     }
29 
30     public int getId() {
31         return id;
32     }
33 
34     public void setId(int id) {
35         this.id = id;
36     }
37 
38     public String getUsername() {
39         return username;
40     }
41 
42     public void setUsername(String username) {
43         this.username = username;
44     }
45 
46     public String getPassword() {
47         return password;
48     }
49 
50     public void setPassword(String password) {
51         this.password = password;
52     }
53 
54     //1:1
55    /* @Override
56     public String toString() {
57         return "User{" +
58                 "id=" + id +
59                 ", username='" + username + '\'' +
60                 ", password='" + password + '\'' +
61                 ", birthday=" + birthday +
62                 '}';
63     }*/
64    //1:n
65     @Override
66     public String toString() {
67         return "User{" +
68                 "id=" + id +
69                 ", username='" + username + '\'' +
70                 ", password='" + password + '\'' +
71                 ", birthday=" + birthday +
72                 ", orderList=" + orderList +
73                 '}';
74     }
75 }
 1 package com.haifei.domain;
 2 
 3 import java.util.Date;
 4 
 5 public class Order {
 6 
 7     private int id;
 8     private Date ordertime;
 9     private double total;
10 
11 
12     //当前订单属于哪一个用户
13     private User user;
14     public User getUser() {
15         return user;
16     }
17     public void setUser(User user) {
18         this.user = user;
19     }
20 
21     public int getId() {
22         return id;
23     }
24 
25     public void setId(int id) {
26         this.id = id;
27     }
28 
29     public Date getOrdertime() {
30         return ordertime;
31     }
32 
33     public void setOrdertime(Date ordertime) {
34         this.ordertime = ordertime;
35     }
36 
37     public double getTotal() {
38         return total;
39     }
40 
41     public void setTotal(double total) {
42         this.total = total;
43     }
44 
45 
46     @Override
47     public String toString() {
48         return "Order{" +
49                 "id=" + id +
50                 ", ordertime=" + ordertime +
51                 ", total=" + total +
52                 ", user=" + user +
53                 '}';
54     }
55 }

 

1 OrderMapper
2 
3 +
4 
5 @Select("select * from orders where uid=#{uid}")
6 public List<Order> findByUid(int uid);
 1 UserMapper
 2 
 3 +
 4 
 5     @Select("select * from user")
 6     @Results({
 7             @Result(column = "id", property = "id", id=true),
 8             @Result(column = "username", property = "username"),
 9             @Result(column = "password", property = "password"),
10             @Result(
11                     property = "orderList",
12                     column = "id", //查询出来user的id 作为 orders的uid参数传入 进行进一步查询订单信息
13                     javaType = List.class,
14                     many = @Many(select = "com.haifei.mapper.OrderMapper.findByUid")
15             )
16     })
17     public List<User> findUserAndOrderAll();
 1 package com.haifei.test;
 2 
 3 import com.haifei.domain.User;
 4 import com.haifei.mapper.UserMapper;
 5 import org.apache.ibatis.io.Resources;
 6 import org.apache.ibatis.session.SqlSession;
 7 import org.apache.ibatis.session.SqlSessionFactory;
 8 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
 9 import org.junit.Before;
10 import org.junit.Test;
11 
12 import java.io.IOException;
13 import java.io.InputStream;
14 import java.util.List;
15 
16 public class MybatisTest3 {
17 
18     private UserMapper mapper;
19 
20     @Before
21     public void before() throws IOException {
22         InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
23         SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
24         SqlSession sqlSession = sqlSessionFactory.openSession(true); //true-->事务自动提交
25         mapper = sqlSession.getMapper(UserMapper.class);
26     }
27 
28     /**
29      * 一对多查询
30      */
31     @Test
32     public void testFindUserAndOrderAll(){
33         List<User> userList = mapper.findUserAndOrderAll();
34         for (User user : userList) {
35             System.out.println(user);
36         }
37         /*
38         User{id=1, username='zhangsan', password='123', birthday=Sat Jul 24 20:00:29 CST 2021, orderList=[Order{id=1, ordertime=Thu Jan 01 08:00:02 CST 1970, total=3000.0, user=null}, Order{id=3, ordertime=Thu Jan 01 08:00:02 CST 1970, total=344.0, user=null}]}
39         User{id=2, username='lisi', password='345', birthday=Sat Jul 24 20:00:29 CST 2021, orderList=[Order{id=2, ordertime=Thu Jan 01 08:00:02 CST 1970, total=1000.0, user=null}, Order{id=4, ordertime=Thu Jan 01 08:00:02 CST 1970, total=455.0, user=null}]}
40         User{id=3, username='tom', password='abc', birthday=Sat Jul 24 20:00:29 CST 2021, orderList=[Order{id=5, ordertime=Thu Jan 01 08:00:02 CST 1970, total=100.0, user=null}]}
41         User{id=4, username='lucy', password='def', birthday=Sat Jul 24 20:00:29 CST 2021, orderList=[]}
42         User{id=5, username='sam', password='sam', birthday=Sat Jul 24 20:00:29 CST 2021, orderList=[]}
43         User{id=9, username='11', password='11', birthday=Sat Jul 24 20:00:29 CST 2021, orderList=[]}
44          */
45     }
46 
47 }

 

上一篇:【一遍过!!!】1014 Waiting in Line (30 分)(题意+分析)


下一篇:剑指offer面试题35:复杂链表的复制(Java 实现)