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

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

数据库查询结果

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>

返回结果

[{
		"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就会避免上述的情况,如下

    <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>

结果

[{
		"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
		}]
	}
]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis 中的嵌套查询是指在一个 SQL 查询语句中嵌套执行另一个 SQL 查询语句。这使得我们可以在一个查询中获取更复杂的数据结果。 在 MyBatis 中,可以通过两种方式实现嵌套查询:使用嵌套结果映射和使用嵌套查询语句。 1. 嵌套结果映射: - 在主查询的结果映射中,使用 `<association>` 或 `<collection>` 元素嵌套定义关联对象或集合对象。 - 在关联对象或集合对象的结果映射中,使用 `<select>` 元素定义需要执行的嵌套查询语句。 - 使用 `select` 属性指定需要执行的嵌套查询语句的 ID。 - 在主查询中,使用 `resultMap` 属性指定主查询结果集的映射。 下面是一个示例,展示如何在 MyBatis 中进行嵌套查询: ```xml <resultMap id="userResultMap" type="User"> <id property="id" column="id"/> <result property="username" column="username"/> <association property="profile" resultMap="profileResultMap"/> </resultMap> <resultMap id="profileResultMap" type="Profile"> <id property="id" column="id"/> <result property="email" column="email"/> </resultMap> <select id="getUserWithProfile" resultMap="userResultMap"> SELECT u.id, u.username, p.id, p.email FROM user u INNER JOIN profile p ON u.profile_id = p.id </select> ``` 2. 嵌套查询语句: - 在主查询的 SQL 语句中使用`select` 关键字执行嵌套查询。 - 使用 `<foreach>` 元素构建需要传递给嵌套查询的参数。 - 在嵌套查询中,使用 `#{}` 占位符引用传递的参数。 下面是一个示例,展示如何在 MyBatis 中使用嵌套查询语句: ```xml <select id="getUserWithOrders" resultMap="userResultMap"> SELECT u.id, u.username, (SELECT COUNT(*) FROM orders o WHERE o.user_id = u.id) AS order_count FROM user u </select> ``` 这就是 MyBatis嵌套查询的使用方法。你可以根据具体的业务需求选择适合的方式来实现嵌套查询功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值