当前位置: 代码网 > it编程>编程语言>Java > Mybatis使用collection映射一对多查询分页问题详解

Mybatis使用collection映射一对多查询分页问题详解

2025年12月04日 Java 我要评论
mybatis使用collection映射一对多查询分页场景页面展示列表,需要关联查询另外1张表多的字段,分页。/** * 标签 */ private list<basic

mybatis使用collection映射一对多查询分页

场景

页面展示列表,需要关联查询另外1张表多的字段,分页。

/**
     * 标签
     */
    private list<basicresidenttags> tags;
@data
@tablename("basic_resident_tags")
public class basicresidenttags{

    private static final long serialversionuid=1l;

    /**
     * 标签id
     */
    @tableid(value = "id",type = idtype.auto)
    private integer id;
    /**
     * 名称
     */
    private string name;
    /**
     * 颜色
     */
    private string color;
    /**
     * 居民id
     */
    private integer residentid;

}

原来的sql这样写

<!--一对多映射-->
    <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有两条

加上分页条件,多的一端只有一条数据。

修改之后的sql

分页满足正常展示多的一端。

<!--一对多映射-->
    <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>

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com