PageHelper插件實現(xiàn)一對多查詢時的分頁問題
項目中經(jīng)常會使用到一對多的查詢場景,但是PageHelper對這種嵌套查詢的支持不夠,如果是一對多的列表查詢,返回的分頁結(jié)果是不對的
參考Github上的說明:https://github.com/pagehelper/Mybatis-PageHelper/blob/master/wikis/zh/Important.md
對于一對多的列表查詢,有兩種方式解決
1、在代碼中處理。單獨修改分頁查詢的resultMap,刪除collection標簽,然后在代碼中遍歷結(jié)果,查詢子集
2、使用mybatis提供的方法解決,具體如下
定義兩個resultMap,一個給分頁查詢使用,一個給其余查詢使用
<resultMap id="BaseMap" type="com.xx.oo.Activity">
<id column="id" property="id" jdbcType="INTEGER"/>
....
</resultMap>
<resultMap id="ResultMap" type="com.xx.oo.Activity" extends="BaseMap">
<collection property="templates" ofType="com.xx.oo.Template">
<id column="pt_id" property="id" jdbcType="INTEGER"/>
<result column="pt_title" property="title" jdbcType="VARCHAR"/>
</collection>
</resultMap>
<resultMap id="RichResultMap" type="com.xx.oo.Activity" extends="BaseMap">
<!--property:對應(yīng)JavaBean中的字段-->
<!--ofType:對應(yīng)JavaBean的類型-->
<!--javaType:對應(yīng)返回值的類型-->
<!--column:對應(yīng)數(shù)據(jù)庫column的字段,不是JavaBean中的字段-->
<!--select:對應(yīng)查詢子集的sql-->
<collection property="templates" ofType="com.xx.oo.Template" javaType="java.util.List" column="id" select="queryTemplateById">
<id column="pt_id" property="id" jdbcType="INTEGER"/>
<result column="pt_title" property="title" jdbcType="VARCHAR"/>
</collection>
</resultMap>
<resultMap id="template" type="com.xx.oo.Template">
<id column="pt_id" property="id" jdbcType="INTEGER"/>
<result column="pt_title" property="title" jdbcType="VARCHAR"/>
</resultMap>
需要分頁的查詢,使用RichResultMap。先定義一個查詢子集的sql
<!--這里的#{id}參數(shù)就是collection中定義的column字段-->
<select id="queryTemplateById" parameterType="java.lang.Integer" resultMap="template">
select id pt_id, title pt_title
from t_activity_template where is_delete=0 and activity_id = #{id}
order by sort_number desc
</select>
<select id="queryByPage" parameterType="com.xx.oo.ActivityPageRequest" resultMap="RichResultMap">
SELECT t.*,t1.real_name creator_name
FROM t_activity t
left join user t1 on t1.user_id = t.creator
<where>
t.is_delete = 0
<if test="criteria != null and criteria.length()>0">AND (t.activity_name like concat("%",#{criteria},"%"))</if>
</where>
ORDER BY t.id desc
</select>
不需要分頁的普通查詢,使用ResultMap
<select id="queryById" parameterType="java.lang.Integer" resultMap="ResultMap">
SELECT t.*, t6.id pt_id, t1.title pt_title
FROM t_activity t
left join t_activity_template t1 on t.id=t6.activity_id and t1.is_delete=0
WHERE t.is_delete = 0 AND t.id = #{id}
</select>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Springboot GET和POST請求參數(shù)獲取方式小結(jié)
Spring Boot GET和POST請求參數(shù)獲取是開發(fā)人員經(jīng)常需要解決的問題,本文主要介紹了Springboot GET和POST請求參數(shù)獲取方式小結(jié),具有一定的參考價值,感興趣的可以了解一下2023-09-09
基于Spring?Cache實現(xiàn)Caffeine+Redis二級緩存
本文主要介紹了基于Spring?Cache實現(xiàn)Caffeine+Redis二級緩存,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03
SpringBoot MongoDB 索引沖突分析及解決方法
這篇文章主要介紹了SpringBoot MongoDB 索引沖突分析及解決方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-11-11
Java?List集合取交集的8種不同實現(xiàn)方式總結(jié)
工作中經(jīng)常遇到需要取兩個集合之間的交集、差集情況,下面這篇文章主要給大家總結(jié)介紹了關(guān)于Java?List集合取交集的8種不同實現(xiàn)方式,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2024-04-04

