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
		}]
	}
]
相关推荐
声声codeGrandMaster1 小时前
django之账号管理功能
数据库·后端·python·django
Elastic 中国社区官方博客2 小时前
使用 LangGraph 和 Elasticsearch 构建强大的 RAG 工作流
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索
AscendKing2 小时前
mongo客户端操作mongodb记录
数据库·mongodb
千千寰宇2 小时前
[设计模式/Java] 设计模式之解释器模式【27】
数据库·设计模式
BXCQ_xuan2 小时前
Typecho博客网站头部SEO优化完整指南
运维·服务器·数据库·php·web
施嘉伟3 小时前
Oracle 11g RAC手动打补丁详细步骤
数据库·oracle
my_realmy4 小时前
SQL 查询进阶:WHERE 子句与连接查询详解
java·开发语言·数据库·sql
游王子4 小时前
Milvus(7):Schema、主字段和自动识别
数据库·milvus
forestsea5 小时前
MySQL 调优
数据库·mysql·性能优化
松树戈6 小时前
PostgreSQL使用LIKE右模糊没有走索引分析&验证
数据库·postgresql