mybatis-plus分页嵌套list查询

358 阅读1分钟

开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第1天,点击查看活动详情

mybatis-plus官网

分页插件不会用先看看官网的教程

image.png

分页一对多嵌套查询

主要原理是在resultMap中添加collection

第一种

<resultMap id="testPage" type="对应关系为一的类路径">
    <id column="id" jdbcType="BIGINT" property="id" />
     <result column="type" jdbcType="INTEGER" property="type" />
    <collection property="嵌套的list属性的名称" ofType="对应关系为多的类路径" javaType="java.util.List">
        <id column="id" jdbcType="BIGINT" property="id" />
        <result column="type" jdbcType="INTEGER" property="type" />
    </collection>
</resultMap>
<select id="testPage" resultMap="testPage">
    SELECT *
    FROM
    test_a ta,test_b tb
    <where>
        tb.id = tb.ta_id 
    </where>
</select>

第二种

<resultMap id="testPage" type="对应关系为一的类路径">
    <id column="id" jdbcType="BIGINT" property="id" />
     <result column="type" jdbcType="INTEGER" property="type" />
    <collection column="{parentId=id}" property="嵌套的list属性的名称" ofType="对应关系为多的类路径" setlect="test1Page" javaType="java.util.List">
        <id column="id" jdbcType="BIGINT" property="id" />
        <result column="type" jdbcType="INTEGER" property="type" />
    </collection>
</resultMap>
<select id="testPage" resultMap="testPage">
    SELECT *
    FROM
    test_a ta
</select>

<select id="test1Page" resultType="对应关系为多的类路径">
    SELECT *
    FROM
    test_b where id=#{parentId}
</select>

注意collection的property是接收对象里的list的属性名,这样就可以查询出嵌套list的分页了