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ù)庫(kù)的偏移 private int offSet; // 數(shù)據(jù)庫(kù)的大小限制 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)文章
詳解Java從后臺(tái)重定向(redirect)到另一個(gè)項(xiàng)目的方法
這篇文章主要介紹了詳解Java從后臺(tái)重定向(redirect)到另一個(gè)項(xiàng)目的方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-04-04劍指Offer之Java算法習(xí)題精講字符串與二叉搜索樹
跟著思路走,之后從簡(jiǎn)單題入手,反復(fù)去看,做過之后可能會(huì)忘記,之后再做一次,記不住就反復(fù)做,反復(fù)尋求思路和規(guī)律,慢慢積累就會(huì)發(fā)現(xiàn)質(zhì)的變化2022-03-03springboot項(xiàng)目攔截前端請(qǐng)求中的特殊字符串(解決方案)
springboot項(xiàng)目中,需要對(duì)前端請(qǐng)求數(shù)據(jù)進(jìn)行過濾,攔截特殊字符,本文通過實(shí)例代碼給大家分享完美解決方案,感興趣的朋友一起看看吧2023-10-10Spring中@Configuration注解和@Component注解的區(qū)別詳解
這篇文章主要介紹了Spring中@Configuration注解和@Component注解的區(qū)別詳解,@Configuration 和 @Component 到底有何區(qū)別呢?我先通過如下一個(gè)案例,在不分析源碼的情況下,小伙伴們先來直觀感受一下這兩個(gè)之間的區(qū)別,需要的朋友可以參考下2023-09-09