MyBatis中的limit分頁設(shè)置
MyBatis limit分頁設(shè)置
錯(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; } }
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
MyBatis-Plus實(shí)現(xiàn)多表聯(lián)查的方法實(shí)戰(zhàn)
這篇文章主要給大家介紹了關(guān)于MyBatis-Plus實(shí)現(xiàn)多表聯(lián)查的方法,MyBatis Plus是一款針對(duì)MyBatis框架的增強(qiáng)工具,它提供了很多方便的方法來實(shí)現(xiàn)多表聯(lián)查,需要的朋友可以參考下2023-07-07java調(diào)用FFmpeg實(shí)現(xiàn)視屏壓縮功能的詳細(xì)步驟
這篇文章主要介紹了java調(diào)用FFmpeg實(shí)現(xiàn)視屏壓縮功能,本文簡單的展示了java調(diào)用FFmpeg命令實(shí)現(xiàn)視屏的壓縮的詳細(xì)步驟,需要的朋友可以參考下2021-09-09Java?Stream函數(shù)式編程管道流結(jié)果處理
這篇文章主要為大家介紹了Java?Stream函數(shù)式編程管道流結(jié)果處理的示例過程解析需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-03-03URLConnection發(fā)送HTTP請(qǐng)求的方法_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了URLConnection發(fā)送HTTP請(qǐng)求的方法,主要介紹了如何通過Java(模擬瀏覽器)發(fā)送HTTP請(qǐng)求,有興趣的可以了解一下2017-07-07