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
		}]
	}
]
相关推荐
邂逅you10 小时前
用python操作mysql之pymysql库基本操作
数据库·python·mysql
心 一10 小时前
接口安全测试实战:从数据库错误泄露看如何构建安全防线
数据库·安全
点灯小铭11 小时前
基于单片机的PID调节脉动真空灭菌器上位机远程监控设计
数据库·单片机·嵌入式硬件·毕业设计·课程设计
小高Baby@11 小时前
Redis Key的设计
数据库·redis·缓存
q_191328469512 小时前
基于RuoYi框架+Mysql的汽车进销存后台管理系统
数据库·vue.js·spring boot·mysql·汽车·个人开发·若依
wuyunhang12345612 小时前
MySQL----锁
数据库·mysql
悟能不能悟12 小时前
springboot在DTO使用service,怎么写
java·数据库·spring boot
达瓦里氏12312 小时前
重排反应是什么?从分子变化到四大关键特征解析
数据库·学习·化学
电话交换机IPPBX-3CX12 小时前
Grafana图表与电话交换机的结合
数据库·mysql·grafana·ip pbx·电话交换机
IT 小阿姨(数据库)12 小时前
PostgreSQL REST API 介绍
运维·数据库·sql·postgresql·centos