创建一个实体继承两个实体之一,另一个实体作为属性
实体1.
order
package pojo; import java.util.Date; public class Order {
private Integer id; private Integer userId; private String number; private Date createtime; private String note; public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public Integer getUserId() {
return userId;
} public void setUserId(Integer userId) {
this.userId = userId;
} public String getNumber() {
return number;
} public void setNumber(String number) {
this.number = number == null ? null : number.trim();
} public Date getCreatetime() {
return createtime;
} public void setCreatetime(Date createtime) {
this.createtime = createtime;
} public String getNote() {
return note;
} public void setNote(String note) {
this.note = note == null ? null : note.trim();
} @Override
public String toString() {
return "Order [id=" + id + ", userId=" + userId + ", number=" + number + ", createtime=" + createtime
+ ", note=" + note + "]";
} }
实体2user
package pojo; import java.util.Date; public class User { private Integer id;
private String username;// 鐢ㄦ埛濮撳悕
private String sex;// 鎬у埆
private Date birthday;// 鐢熸棩
private String address;// 鍦板潃
private String uuid2; public String getUuid2() {
return uuid2;
} public void setUuid2(String uuid2) {
this.uuid2 = uuid2;
} public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getSex() {
return sex;
} public void setSex(String sex) {
this.sex = sex;
} public Date getBirthday() {
return birthday;
} public void setBirthday(Date birthday) {
this.birthday = birthday;
} public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
} @Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", sex=" + sex + ", birthday=" + birthday + ", address="
+ address + ", uuid2=" + uuid2 + "]";
} }
创建实体OrderUser
package pojo; import java.util.Date; public class OrderUser extends Order {
private String username;// 鐢ㄦ埛濮撳悕
private String address;// 鍦板潃
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "OrderUser [username=" + username + ", address=" + address + ", getId()=" + getId() + ", getUserId()="
+ getUserId() + ", getNumber()=" + getNumber() + ", getCreatetime()=" + getCreatetime() + ", getNote()="
+ getNote() + "]";
} }
mapper文件使用
resultType
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="mapper.OrderMapper"> <!-- 引用resultMap -->
<select id="getOrderList" resultMap="order_List_map">
select id,
user_id,
number,
createtime,
note
from order1
</select>
<!-- 一对一关联查询 -->
<select id="getOrderUser" resultType="orderuser">
select
o.id,
o.user_id userId,
o.number,
o.createtime,
o.note,
u.username,
u.address
from order1 o left join user u on u.id = o.user_id
</select>
</mapper>
测试
@Test
public void getOrderUser() {
SqlSession openSession = SqlSessionFactoryUtil.getSqlSessionFactory().openSession();
//获得接口实现类
OrderMapper mapper = openSession.getMapper(OrderMapper.class);
List<OrderUser> orderUser = mapper.getOrderUser();
for (OrderUser orderUser2 : orderUser) {
System.out.println(orderUser2);
}
openSession.close();
}