mybatis嵌套查询子集合只有一条数据

我们再用mybatis做嵌套查询时,有时会遇到子集合只有1条数据的情况,例如下这样:

数据库查询结果

xml

XML 复制代码
    <resultMap id="userMap" type="com.springboot.demo.test.entity.User">
        <id column="uid" property="uid"/>
        <result column="username" property="username"/>
        <result column="tel" property="tel"/>
        <result column="age" property="age"/>
        <collection property="userRoleList" ofType="com.springboot.demo.test.entity.UserRole">
            <result column="roleid" property="roleid"/>
        </collection>
    </resultMap>

    <select id="selectUser" resultMap="userMap">
        SELECT
            a.uid,
            a.username,
            a.tel,
            a.age,
            b.roleid
        FROM
            user a
            LEFT JOIN user_role b ON a.uid = b.userid
    </select>

返回结果

XML 复制代码
[{
		"uid": 33,
		"username": "jon",
		"tel": "123",
		"age": 23,
		"userRoleList": [{
			"roleid": 1
		}]
	},
	{
		"uid": 31,
		"username": "xiaomi",
		"tel": "110",
		"age": 20,
		"userRoleList": [{
			"roleid": 13
		}]
	},
	{
		"uid": 35,
		"username": "WANG",
		"tel": "222",
		"age": 33,
		"userRoleList": [{
			"roleid": 1
		}]
	},
	{
		"uid": 34,
		"username": "xiaocai",
		"tel": "111",
		"age": 32,
		"userRoleList": [{
			"roleid": 1
		}]
	}
]

很明显,这不是我们期望结果。如果遇到这种情况,可以在子集合的映射里面把id放进去,这样mybatis就会避免上述的情况,如下

XML 复制代码
    <resultMap id="userMap" type="com.springboot.demo.test.entity.User">
        <id column="uid" property="uid"/>
        <result column="username" property="username"/>
        <result column="tel" property="tel"/>
        <result column="age" property="age"/>
        <collection property="userRoleList" ofType="com.springboot.demo.test.entity.UserRole">
            <id column="urid" property="urid"/>
            <result column="roleid" property="roleid"/>
        </collection>
    </resultMap>

    <select id="selectUser" resultMap="userMap">
        SELECT
            a.uid,
            a.username,
            a.tel,
            a.age,
            b.roleid,
            b.urid
        FROM
            user a
            LEFT JOIN user_role b ON a.uid = b.userid
    </select>

结果

XML 复制代码
[{
		"uid": 33,
		"username": "jon",
		"tel": "123",
		"age": 23,
		"userRoleList": [{
				"urid": 47,
				"roleid": 9
			},
			{
				"urid": 48,
				"roleid": 1
			}
		]
	},
	{
		"uid": 31,
		"username": "xiaomi",
		"tel": "110",
		"age": 20,
		"userRoleList": [{
				"urid": 81,
				"roleid": 9
			},
			{
				"urid": 82,
				"roleid": 10
			},
			{
				"urid": 83,
				"roleid": 16
			},
			{
				"urid": 84,
				"roleid": 1
			},
			{
				"urid": 85,
				"roleid": 13
			}
		]
	},
	{
		"uid": 35,
		"username": "WANG",
		"tel": "222",
		"age": 33,
		"userRoleList": [{
				"urid": 86,
				"roleid": 11
			},
			{
				"urid": 87,
				"roleid": 1
			}
		]
	},
	{
		"uid": 34,
		"username": "xiaocai",
		"tel": "111",
		"age": 32,
		"userRoleList": [{
			"urid": 92,
			"roleid": 1
		}]
	}
]
相关推荐
计算机毕设定制辅导-无忧学长6 小时前
西门子 PLC 与 Modbus 集成:S7-1500 RTU/TCP 配置指南(一)
服务器·数据库·tcp/ip
程序员柳7 小时前
基于微信小程序的校园二手交易平台、微信小程序校园二手商城源代码+数据库+使用说明,layui+微信小程序+Spring Boot
数据库·微信小程序·layui
梦在深巷、7 小时前
MySQL/MariaDB数据库主从复制之基于二进制日志的方式
linux·数据库·mysql·mariadb
IT乌鸦坐飞机7 小时前
ansible部署数据库服务随机启动并创建用户和设置用户有完全权限
数据库·ansible·centos7
IT_10247 小时前
Spring Boot项目开发实战销售管理系统——数据库设计!
java·开发语言·数据库·spring boot·后端·oracle
祁思妙想8 小时前
八股学习(三)---MySQL
数据库·学习·mysql
惊骇世俗王某人9 小时前
1.MySQL之如何定位慢查询
数据库·mysql
秦歌6669 小时前
向量数据库-Milvus快速入门
数据库·milvus
Edingbrugh.南空10 小时前
Flink SQLServer CDC 环境配置与验证
数据库·sqlserver·flink
码不停蹄的玄黓11 小时前
MySQL分布式ID冲突详解:场景、原因与解决方案
数据库·分布式·mysql·id冲突