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
		}]
	}
]
相关推荐
代码的余温1 小时前
SQL性能优化全攻略
数据库·mysql·性能优化
手把手入门3 小时前
★CentOS:MySQL数据备份
数据库·mysql·adb
SelectDB4 小时前
5000+ 中大型企业首选的 Doris,在稳定性的提升上究竟花了多大的功夫?
大数据·数据库·apache
路多辛4 小时前
Golang database/sql 包深度解析(二):连接池实现原理
数据库·sql·golang
SimonKing4 小时前
Mybatis批量插入,形式不同性能也不同
数据库·后端·程序员
杰克尼5 小时前
MYSQL-175. 组合两个表
数据库·mysql
DemonAvenger5 小时前
MySQL索引原理深度解析与优化策略实战
数据库·mysql·性能优化
189228048616 小时前
NY270NY273美光固态闪存NY277NY287
服务器·网络·数据库·科技·性能优化
星霜笔记9 小时前
Docker 部署 MariaDB+phpMyAdmin+Nextcloud 完整教程
运维·数据库·docker·容器·mariadb
wyiyiyi15 小时前
【Web后端】Django、flask及其场景——以构建系统原型为例
前端·数据库·后端·python·django·flask