首页 归档 关于 文件 Github
×

MyBatis返回多个结果集

2021-03-15 17:58:37
MyBatis
  • MyBatis
本文总阅读量(次):
本文字数统计(字):691
本文阅读时长(分):3

MyBatis在查询时可以将单一的结果集并列查询组成多结果集;
如:多重List<Map<String,Object>>格式,Object可再次为 List<Map<String,Object>>结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[
{
"userName": "张三",
"userCode": "RBXLWCzGcqDncROd90Jd",
"userRole": [
{
"roleCode": "test1",
"userCode": "RBXLWCzGcqDncROd90Jd"
},
{
"roleCode": "test2",
"userCode": "RBXLWCzGcqDncROd90Jd"
}
]
}
]

使用

xxMapper.xml:
xml中以userCode为连接,collection的property属性值为二级key

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
<resultMap id="testResultMap" type="java.util.Map">
<id property="userCode" column="user_Code" />
<result property="userName" column="user_Name" />
<result property="userRemark" column="user_Remark" />
<collection property="userRole" javaType="list" ofType="map">
<id property="roleCode" column="role_Code" />
<result property="userCode" column="user_Code" />
<result property="roleName" column="role_Name" />
<result property="roleRemark" column="role_Remark" />
</collection>
</resultMap>
<select id="selectTestListResultMap" parameterType="map" resultMap="testResultMap">
SELECT
test_user.user_Code,
test_user.user_Name,
test_user.user_Remark,
test_role.role_Code,
test_role.role_Name,
test_role.role_Remark,
test_role.user_Code
FROM
test_user
LEFT JOIN test_role ON test_role.user_Code = test_user.user_Code
WHERE
test_user.user_Code IN (
SELECT
test_role.user_Code
FROM
test_role
WHERE
test_role.user_Code = test_user.user_Code
GROUP BY
test_role.user_Code
)
</select>

xxMapper.java:

1
2
3
4
5
6
/**
* 查询列表信息
* @param params 参数
* @return 返回
*/
List<Map<String, Object>> selectTestListResultMap(@Param("params") Map<String, Object> params);

结果集:

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
[
{
"userRemark": "备注用户",
"userName": "张三",
"userRole": [
{
"roleRemark": "备注权限",
"roleCode": "test1",
"roleName": "a",
"userCode": "RBXLWCzGcqDncROd90Jd"
},
{
"roleRemark": "备注权限",
"roleCode": "test2",
"roleName": "b",
"userCode": "RBXLWCzGcqDncROd90Jd"
}
],
"userCode": "RBXLWCzGcqDncROd90Jd"
},
{
"userRemark": "备注用户",
"userName": "李四",
"userRole": [
{
"roleRemark": "备注权限",
"roleCode": "test3",
"roleName": "c",
"userCode": "TALKA0I56vYUp2cVaczZ"
},
{
"roleRemark": "备注权限",
"roleCode": "test4",
"roleName": "d",
"userCode": "TALKA0I56vYUp2cVaczZ"
},
{
"roleRemark": "备注权限",
"roleCode": "test5",
"roleName": "e",
"userCode": "TALKA0I56vYUp2cVaczZ"
}
],
"userCode": "TALKA0I56vYUp2cVaczZ"
},
{
"userRemark": "备注用户",
"userName": "王五",
"userRole": [
{
"roleRemark": "备注权限",
"roleCode": "test6",
"roleName": "f",
"userCode": "EMbvTsuedRjItaD0aZzq"
},
{
"roleRemark": "备注权限",
"roleCode": "test7",
"roleName": "g",
"userCode": "EMbvTsuedRjItaD0aZzq"
}
],
"userCode": "EMbvTsuedRjItaD0aZzq"
}
]

表:

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
-- ----------------------------
-- Table structure for test_role
-- ----------------------------
DROP TABLE IF EXISTS `test_role`;
CREATE TABLE `test_role` (
`tr_PKeyId` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`role_Code` varchar(32) DEFAULT NULL COMMENT '角色代码',
`role_Name` varchar(32) DEFAULT NULL COMMENT '角色名称',
`user_Code` varchar(32) DEFAULT NULL COMMENT '用户代码',
`role_Remark` varchar(100) DEFAULT NULL COMMENT '备注',
PRIMARY KEY (`tr_PKeyId`)
) ENGINE=InnoDB AUTO_INCREMENT=25 DEFAULT CHARSET=utf8 COMMENT='角色表';

-- ----------------------------
-- Records of test_role
-- ----------------------------
INSERT INTO `test_role` VALUES ('16', 'test1', 'a', 'RBXLWCzGcqDncROd90Jd', '备注权限');
INSERT INTO `test_role` VALUES ('19', 'test2', 'b', 'RBXLWCzGcqDncROd90Jd', '备注权限');
INSERT INTO `test_role` VALUES ('20', 'test3', 'c', 'TALKA0I56vYUp2cVaczZ', '备注权限');
INSERT INTO `test_role` VALUES ('21', 'test4', 'd', 'TALKA0I56vYUp2cVaczZ', '备注权限');
INSERT INTO `test_role` VALUES ('22', 'test5', 'e', 'TALKA0I56vYUp2cVaczZ', '备注权限');
INSERT INTO `test_role` VALUES ('23', 'test6', 'f', 'EMbvTsuedRjItaD0aZzq', '备注权限');
INSERT INTO `test_role` VALUES ('24', 'test7', 'g', 'EMbvTsuedRjItaD0aZzq', '备注权限');

-- ----------------------------
-- Table structure for test_user
-- ----------------------------
DROP TABLE IF EXISTS `test_user`;
CREATE TABLE `test_user` (
`tu_PKeyId` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`user_Name` varchar(32) DEFAULT NULL COMMENT '用户名称',
`user_Code` varchar(32) DEFAULT NULL COMMENT '用户代码',
`user_Remark` varchar(100) DEFAULT NULL COMMENT '备注',
PRIMARY KEY (`tu_PKeyId`)
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8 COMMENT='用户表';

-- ----------------------------
-- Records of test_user
-- ----------------------------
INSERT INTO `test_user` VALUES ('10', '张三', 'RBXLWCzGcqDncROd90Jd', '备注用户');
INSERT INTO `test_user` VALUES ('11', '李四', 'TALKA0I56vYUp2cVaczZ', '备注用户');
INSERT INTO `test_user` VALUES ('12', '王五', 'EMbvTsuedRjItaD0aZzq', '备注用户');
INSERT INTO `test_user` VALUES ('13', '赵六', 'GosZVJuSCA70TmeRnBJO', '备注用户');
完
集合操作 - List,Set,Map
hexo博客的一些内置写法

本文标题:MyBatis返回多个结果集

文章作者:十二

发布时间:2021-03-15 17:58:37

最后更新:2022-03-24 16:33:43

原始链接:https://www.zhuqiaolun.com/2021/03/1615802317183/1615802317183/

许可协议:署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

头像

十二

我想起那天夕阳下的奔跑,那是我逝去的青春。

分类

  • Blog4
  • ElasticSearch13
  • Git2
  • Go-FastDfs2
  • IDEA2
  • J-Package6
  • J-Tools21
  • Java2
  • JavaFx6
  • Kafka4
  • Linux2
  • Logger6
  • Maven5
  • MyBatis6
  • MyCat3
  • MySql2
  • Nginx5
  • OceanBase1
  • RabbitMq4
  • Redis6
  • SVN1
  • SpringBoot16
  • Tomcat6
  • WebService2
  • Windows2
  • kubernetes10

归档

  • 一月 20261
  • 十二月 20253
  • 八月 20252
  • 六月 20251
  • 二月 20251
  • 十二月 20244
  • 八月 202416
  • 六月 20241
  • 九月 20231
  • 八月 20231
  • 七月 20232
  • 八月 20222
  • 三月 202214
  • 二月 20224
  • 十一月 20211
  • 七月 20215
  • 六月 20213
  • 五月 20213
  • 四月 20211
  • 三月 202116
  • 二月 20212
  • 一月 20211
  • 十一月 202014
  • 十月 20201
  • 九月 202014
  • 八月 20205
  • 七月 20204
  • 六月 20208
  • 五月 20208

作品

我的微信 我的文件

网站信息

本站运行时间统计: 载入中...
本站文章字数统计:103.2k
本站文章数量统计:139
© 2026 十二  |  鄂ICP备18019781号-1  |  鄂公网安备42118202000044号
驱动于 Hexo  | 主题 antiquity  |  不蒜子告之 阁下是第个访客
首页 归档 关于 文件 Github