PageHelper插件實(shí)現(xiàn)一對(duì)多查詢(xún)時(shí)的分頁(yè)問(wèn)題
項(xiàng)目中經(jīng)常會(huì)使用到一對(duì)多的查詢(xún)場(chǎng)景,但是PageHelper對(duì)這種嵌套查詢(xún)的支持不夠,如果是一對(duì)多的列表查詢(xún),返回的分頁(yè)結(jié)果是不對(duì)的
參考Github上的說(shuō)明:https://github.com/pagehelper/Mybatis-PageHelper/blob/master/wikis/zh/Important.md
對(duì)于一對(duì)多的列表查詢(xún),有兩種方式解決
1、在代碼中處理。單獨(dú)修改分頁(yè)查詢(xún)的resultMap,刪除collection標(biāo)簽,然后在代碼中遍歷結(jié)果,查詢(xún)子集
2、使用mybatis提供的方法解決,具體如下
定義兩個(gè)resultMap,一個(gè)給分頁(yè)查詢(xún)使用,一個(gè)給其余查詢(xún)使用
<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:對(duì)應(yīng)JavaBean中的字段-->
<!--ofType:對(duì)應(yīng)JavaBean的類(lèi)型-->
<!--javaType:對(duì)應(yīng)返回值的類(lèi)型-->
<!--column:對(duì)應(yīng)數(shù)據(jù)庫(kù)column的字段,不是JavaBean中的字段-->
<!--select:對(duì)應(yīng)查詢(xún)子集的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>
需要分頁(yè)的查詢(xún),使用RichResultMap。先定義一個(gè)查詢(xún)子集的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>
不需要分頁(yè)的普通查詢(xún),使用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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Springboot GET和POST請(qǐng)求參數(shù)獲取方式小結(jié)
Spring Boot GET和POST請(qǐng)求參數(shù)獲取是開(kāi)發(fā)人員經(jīng)常需要解決的問(wèn)題,本文主要介紹了Springboot GET和POST請(qǐng)求參數(shù)獲取方式小結(jié),具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09
基于Spring?Cache實(shí)現(xiàn)Caffeine+Redis二級(jí)緩存
本文主要介紹了基于Spring?Cache實(shí)現(xiàn)Caffeine+Redis二級(jí)緩存,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
SpringBoot MongoDB 索引沖突分析及解決方法
這篇文章主要介紹了SpringBoot MongoDB 索引沖突分析及解決方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-11-11
Java?List集合取交集的8種不同實(shí)現(xiàn)方式總結(jié)
工作中經(jīng)常遇到需要取兩個(gè)集合之間的交集、差集情況,下面這篇文章主要給大家總結(jié)介紹了關(guān)于Java?List集合取交集的8種不同實(shí)現(xiàn)方式,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-04-04
Java入門(mén)交換數(shù)組中兩個(gè)元素的位置
在Java中,交換數(shù)組中的兩個(gè)元素是基本的數(shù)組操作,下面我們將詳細(xì)介紹如何實(shí)現(xiàn)這一操作,以及在實(shí)際應(yīng)用中這種技術(shù)的重要性,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09

