MyBatis复杂映射开发之一对多查询
本文介绍了如何进行一对多查询模型,在用户和订单表关系中,展示了通过 SQL 查询用户及其订单信息,并通过 Java 代码实现了在 MyBatis 中查询所有用户信息以及用户关联的订单信息。
# 一对多查询模型
用户和订单表的关系为,一个用户有多个订单,一个订单只能属于一个用户。
一对多查询需求:查询多有用户,与此同时查询用户具有的订单信息。
# 一对多查询语句
对应的 sql 语句
select u.*,o.ordertime,o.total,o.uid from user u left join orders o on u.id = o.uid;
1
查询结果如下:
id | username | password | birthday | ordertime | total | uid |
---|---|---|---|---|---|---|
1 | lucy | 123 | 2022-03-17 17:15:56 | 2022-03-17 17:15:33 | 3000 | 1 |
1 | lucy | 123 | 2022-03-17 17:15:56 | 2022-03-17 17:15:33 | 4000 | 1 |
2 | tom | 123 | 2022-03-17 17:15:56 | 2022-03-17 17:15:33 | 5000 | 2 |
# 代码实现
# 修改 User 实体
/**
* 用户信息
*
* @name: User
* @author: terwer
* @date: 2022-03-17 17:41
**/
public class User {
private Integer id;
private String username;
// 代表当前用户具备那些订单
private List<Order> orderList;
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 List<Order> getOrderList() {
return orderList;
}
public void setOrderList(List<Order> orderList) {
this.orderList = orderList;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", orderList=" + orderList +
'}';
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 创建 UserMapper
/**
* 用户映射
*
* @name: UserMapper
* @author: terwer
* @date: 2022-03-28 00:03
**/
public interface UserMapper {
// 查询所有用户信息以及用户关联的订单信息
public List<User> findAll();
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# 配置 UserMapper.xml
<?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="com.terwergreen.mapper.UserMapper">
<resultMap id="userMap" type="com.terwergreen.pojo.User">
<result property="id" column="id"></result>
<result property="username" column="username"></result>
<collection property="orderList" ofType="com.terwergreen.pojo.Order">
<result property="id" column="uid"></result>
<result property="orderTime" column="ordertime"></result>
<result property="total" column="total"></result>
</collection>
</resultMap>
<!-- resultMap:手动配置实体属性与表字段的映射关系 -->
<select id="findAll" resultMap="userMap">
select u.*,o.ordertime,o.total,o.uid from user u left join orders o on u.id = o.uid
</select>
</mapper>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 测试
@Test
public void test2() throws IOException {
InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
List<User> userList = userMapper.findAll();
for (User user : userList) {
System.out.println(user);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
执行结果
文章更新历史
2024/05/13 同步文章到其他平台
2022/05/08 feat: 初稿
编辑 (opens new window)
上次更新: 2024/06/12, 03:06:32