需求:

一、一对一查询

查询订单信息,关联查询创建订单的用户信息;

orders--->user:一个订单只由一个用户创建,一对一
orders表 和 user表:
Mybatis学习(5)高级映射
1)使用resultType方法:
思路: 查询订单的同时,关联查询用户信息;创建Order的扩展类,包含订单信息、用户信息;
pojo -- Orders:
 1 package com.cy.po;
 2 
 3 import java.util.Date;
 4 import java.util.List;
 5 
 6 public class Orders {
 7     private Integer id;
 8 
 9     private Integer userId;
10 
11     private String number;
12 
13     private Date createtime;
14 
15     private String note;
16 
17     public Integer getId() {
18         return id;
19     }
20 
21     public void setId(Integer id) {
22         this.id = id;
23     }
24 
25     public Integer getUserId() {
26         return userId;
27     }
28 
29     public void setUserId(Integer userId) {
30         this.userId = userId;
31     }
32 
33     public String getNumber() {
34         return number;
35     }
36 
37     public void setNumber(String number) {
38         this.number = number == null ? null : number.trim();
39     }
40 
41     public Date getCreatetime() {
42         return createtime;
43     }
44 
45     public void setCreatetime(Date createtime) {
46         this.createtime = createtime;
47     }
48 
49     public String getNote() {
50         return note;
51     }
52 
53     public void setNote(String note) {
54         this.note = note == null ? null : note.trim();
55     }
56 }
Orders

pojo-- User:

 1 package com.cy.po;
 2 
 3 import java.util.Date;
 4 
 5 public class User {
 6     //属性名和数据库表的字段对应
 7         private int id;
 8         private String username;// 用户姓名
 9         private String sex;// 性别
10         private Date birthday;// 生日
11         private String address;// 地址
12         public int getId() {
13             return id;
14         }
15         public void setId(int id) {
16             this.id = id;
17         }
18         public String getUsername() {
19             return username;
20         }
21         public void setUsername(String username) {
22             this.username = username;
23         }
24         public String getSex() {
25             return sex;
26         }
27         public void setSex(String sex) {
28             this.sex = sex;
29         }
30         public Date getBirthday() {
31             return birthday;
32         }
33         public void setBirthday(Date birthday) {
34             this.birthday = birthday;
35         }
36         public String getAddress() {
37             return address;
38         }
39         public void setAddress(String address) {
40             this.address = address;
41         }
42         @Override
43         public String toString() {
44             return "------->> User [> sex
45                     + ", birthday=" + birthday + ", address=" + address + "]";
46         }
47 }
User

相关文章: