Mybatis使用collection映射一對(duì)多查詢分頁問題詳解
更新時(shí)間:2025年12月04日 09:20:17 作者:小柒v
本文通過修改SQL查詢語句,解決了在分頁條件下關(guān)聯(lián)查詢多表時(shí),多的一端數(shù)據(jù)不正確的問題,從而實(shí)現(xiàn)了正常的展示
Mybatis使用collection映射一對(duì)多查詢分頁
場景
頁面展示列表,需要關(guān)聯(lián)查詢另外1張表多的字段,分頁。

/**
* 標(biāo)簽
*/
private List<BasicResidentTags> tags;
@Data
@TableName("basic_resident_tags")
public class BasicResidentTags{
private static final long serialVersionUID=1L;
/**
* 標(biāo)簽id
*/
@TableId(value = "id",type = IdType.AUTO)
private Integer id;
/**
* 名稱
*/
private String name;
/**
* 顏色
*/
private String color;
/**
* 居民id
*/
private Integer residentId;
}
原來的sql這樣寫
<!--一對(duì)多映射-->
<resultMap id="many" type="com.vkl.basic.domain.vo.admin.BasicResidentListVo">
<id property="residentId" column="resident_id"/>
<result property="name" column="name"/>
<result property="sex" column="sex"/>
<result property="houseHolder" column="house_holder"/>
<result property="residentName" column="resident_name"/>
<result property="outsider" column="outsider"/>
<result property="room" column="room"/>
<collection property="tags" ofType="com.vkl.basic.domain.BasicResidentTags">
<id column="tagsId" property="id"></id>
<result column="tagsName" property="name"></result>
<result column="color" property="color"></result>
<result column="tid" property="residentId"></result>
</collection>
</resultMap>
<select id="selectPageList" resultType="com.vkl.basic.domain.vo.admin.BasicResidentListVo" resultMap="many"
parameterType="com.vkl.basic.domain.bo.admin.BasicResidentAdminBo">
select b.resident_id,b.`name`,b.sex,b.house_holder,b.resident_name,b.outsider,b.room,
t.id as tagsId,t.`name` as tagsName,t.color,t.resident_id as tid
from basic_resident b LEFT JOIN basic_resident_tags t
on b.resident_id = t.resident_id
where del_flg = '0'
<if test="param.name != null and param.name != ''">
and b.name like concat('%',#{param.name},'%')
or
b.resident_name like concat('%',#{param.name},'%')
</if>
<if test="param.tags != null and param.tags != ''">
and t.name = #{param.tags}
</if>
order by b.resident_id
limit #{query.pageNum},#{query.pageSize}
</select>

正常查詢tags有兩條
加上分頁條件,多的一端只有一條數(shù)據(jù)。

修改之后的sql
分頁滿足正常展示多的一端。
<!--一對(duì)多映射-->
<resultMap id="many" type="com.vkl.basic.domain.vo.admin.BasicResidentListVo">
<id property="residentId" column="resident_id"/>
<result property="name" column="name"/>
<result property="sex" column="sex"/>
<result property="houseHolder" column="house_holder"/>
<result property="residentName" column="resident_name"/>
<result property="outsider" column="outsider"/>
<result property="room" column="room"/>
<collection property="tags" ofType="com.vkl.basic.domain.BasicResidentTags"
column="tid" select="selectTagsByResidentId">
</collection>
</resultMap>
<!--主查詢條件-->
<select id="selectPageList" resultType="com.vkl.basic.domain.vo.admin.BasicResidentListVo" resultMap="many"
parameterType="com.vkl.basic.domain.bo.admin.BasicResidentAdminBo">
select b.resident_id,b.`name`,b.sex,b.house_holder,b.resident_name,b.outsider,b.room,
t.id as tagsId,t.`name` as tagsName,t.color,t.resident_id as tid
from basic_resident b LEFT JOIN basic_resident_tags t
on b.resident_id = t.resident_id
where del_flg = '0'
<if test="param.name != null and param.name != ''">
and b.name like concat('%',#{param.name},'%')
or
b.resident_name like concat('%',#{param.name},'%')
</if>
<if test="param.tags != null and param.tags != ''">
and t.name = #{param.tags}
</if>
order by b.resident_id
limit #{query.pageNum},#{query.pageSize}
</select>
<!--子查詢-->
<select id="selectTagsByResidentId" resultType="com.vkl.basic.domain.BasicResidentTags">
select * from basic_resident_tags where resident_id=#{tid}
</select>
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- MyBatis中Collection和Association的底層實(shí)現(xiàn)原理分析
- MyBatis中<collection>標(biāo)簽的多種用法
- MyBatis中高級(jí)多表查詢(ResultMap、association、collection)詳解
- mybatis collection關(guān)聯(lián)查詢多個(gè)參數(shù)方式
- MyBatis嵌套查詢collection報(bào)錯(cuò):org.apache.ibatis.exceptions.TooManyResultsException
- MyBatis使用嵌套查詢collection和association的實(shí)現(xiàn)
- mybatis?resultMap之collection聚集兩種實(shí)現(xiàn)方式
相關(guān)文章
Java實(shí)現(xiàn)企業(yè)微信消息推送功能的詳細(xì)步驟
這篇文章主要介紹了Java實(shí)現(xiàn)企業(yè)微信消息推送功能,本文圖文實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04
Spring MVC接受表單自動(dòng)封裝特性實(shí)例解析
這篇文章主要介紹了Spring MVC接受表單自動(dòng)封裝特性實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02
JAVA實(shí)現(xiàn)較完善的布隆過濾器的示例代碼
這篇文章主要介紹了JAVA實(shí)現(xiàn)較完善的布隆過濾器的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-10-10
dubbo服務(wù)調(diào)用者的三種調(diào)用過程
文章主要介紹了在Dubbo框架中進(jìn)行服務(wù)調(diào)用的幾種常見方法,包括使用注解、在XML中注冊(cè)服務(wù)實(shí)例以及泛化調(diào)用2026-01-01

