MyBatis limit分頁設(shè)置的實(shí)現(xiàn)
錯(cuò)誤的寫法:
<select id="queryMyApplicationRecord" parameterType="MyApplicationRequest" resultMap="myApplicationMap">
SELECT
a.*,
FROM
tb_user a
WHERE 1=1
<if test="ids != null and ids.size()!=0">
AND a.id IN
<foreach collection="ids" item="id" index="index"
open="(" close=")" separator=",">
#{id}
</foreach>
</if>
<if test="statusList != null and statusList.size()!=0">
AND a.status IN
<foreach collection="statusList" item="status" index="index"
open="(" close=")" separator=",">
#{status}
</foreach>
</if>
ORDER BY a.create_time desc
LIMIT (#{pageNo}-1)*#{pageSize},#{pageSize}; // 錯(cuò)誤
</select>
在MyBatis中LIMIT之后的語句不允許的變量不允許進(jìn)行算數(shù)運(yùn)算,會(huì)報(bào)錯(cuò)。
正確的寫法一:
<select id="queryMyApplicationRecord" parameterType="MyApplicationRequest" resultMap="myApplicationMap">
SELECT
a.*,
FROM
tb_user a
WHERE 1=1
<if test="ids != null and ids.size()!=0">
AND a.id IN
<foreach collection="ids" item="id" index="index"
open="(" close=")" separator=",">
#{id}
</foreach>
</if>
<if test="statusList != null and statusList.size()!=0">
AND a.status IN
<foreach collection="statusList" item="status" index="index"
open="(" close=")" separator=",">
#{status}
</foreach>
</if>
ORDER BY a.create_time desc
LIMIT ${(pageNo-1)*pageSize},${pageSize}; (正確)
</select>
正確的寫法二:(推薦)
<select id="queryMyApplicationRecord" parameterType="MyApplicationRequest" resultMap="myApplicationMap">
SELECT
a.*,
FROM
tb_user a
WHERE 1=1
<if test="ids != null and ids.size()!=0">
AND a.id IN
<foreach collection="ids" item="id" index="index"
open="(" close=")" separator=",">
#{id}
</foreach>
</if>
<if test="statusList != null and statusList.size()!=0">
AND a.status IN
<foreach collection="statusList" item="status" index="index"
open="(" close=")" separator=",">
#{status}
</foreach>
</if>
ORDER BY a.create_time desc
LIMIT #{offSet},#{limit}; (推薦,代碼層可控)
</select>
分析:方法二的寫法,需要再請(qǐng)求參數(shù)中額外設(shè)置兩個(gè)get函數(shù),如下:
@Data
public class QueryParameterVO {
private List<String> ids;
private List<Integer> statusList;
// 前端傳入的頁碼
private int pageNo; // 從1開始
// 每頁的條數(shù)
private int pageSize;
// 數(shù)據(jù)庫的偏移
private int offSet;
// 數(shù)據(jù)庫的大小限制
private int limit;
// 這里重寫offSet和limit的get方法
public int getOffSet() {
return (pageNo-1)*pageSize;
}
public int getLimit() {
return pageSize;
}
}
到此這篇關(guān)于MyBatis limit分頁設(shè)置的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)MyBatis limit分頁內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring?Boot項(xiàng)目集成Knife4j接口文檔的實(shí)例代碼
Knife4j就相當(dāng)于是swagger的升級(jí)版,對(duì)于我來說,它比swagger要好用得多<BR>,這篇文章主要介紹了Spring?Boot項(xiàng)目集成Knife4j接口文檔的示例代碼,需要的朋友可以參考下2021-12-12
Java?詳解Collection集合之ArrayList和HashSet
本章具體介紹了ArrayList和HashSet兩種集合的基本使用方法和區(qū)別,圖解穿插代碼實(shí)現(xiàn)。?JAVA成仙路從基礎(chǔ)開始講,后續(xù)會(huì)講到JAVA高級(jí),中間會(huì)穿插面試題和項(xiàng)目實(shí)戰(zhàn),希望能給大家?guī)韼椭?/div> 2022-03-03
關(guān)于Java從本地文件復(fù)制到網(wǎng)絡(luò)文件上傳
這篇文章主要介紹了關(guān)于Java從本地文件復(fù)制到網(wǎng)絡(luò)文件上傳,File?和?IO?流其實(shí)是很相似的,都是將文件從一個(gè)地方轉(zhuǎn)移到另一個(gè)地方,這也是流的特點(diǎn)之一,需要的朋友可以參考下2023-04-04
SpringBoot整合GRPC微服務(wù)遠(yuǎn)程通信的實(shí)現(xiàn)示例
本文主要介紹了SpringBoot整合GRPC微服務(wù)遠(yuǎn)程通信的實(shí)現(xiàn)示例,包含gRPC的工作原理,以及如何在Spring Boot應(yīng)用中集成gRPC,具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02
詳細(xì)聊一聊JavaWeb中的Request和Response
這篇文章主要給大家介紹了關(guān)于JavaWeb中Request和Response的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-02-02
Springboot使用sharedingjdbc實(shí)現(xiàn)分庫分表
這篇文章主要介紹了Springboot使用sharedingjdbc實(shí)現(xiàn)分庫分表,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
Java案例實(shí)現(xiàn)不重復(fù)的隨機(jī)數(shù)
這篇文章主要介紹了Java案例實(shí)現(xiàn)不重復(fù)的隨機(jī)數(shù),通過創(chuàng)建Set集合對(duì)象,可以使用HashSet也可以使用TreeSet,區(qū)別在于TreeSet是排序后的,創(chuàng)建隨機(jī)數(shù)對(duì)象,獲取一個(gè)隨機(jī)數(shù)去重等操作,需要的朋友可以參考一下2022-04-04
解決IDEA2020.2插件lombok報(bào)錯(cuò)問題(親測(cè)有效)
這篇文章主要介紹了解決IDEA2020.2插件lombok報(bào)錯(cuò)問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08最新評(píng)論

