MyBatis limit分頁(yè)設(shè)置的實(shí)現(xiàn)
<select 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之后的語(yǔ)句不允許的變量不允許進(jìn)行算數(shù)運(yùn)算,會(huì)報(bào)錯(cuò)。
正確的寫(xiě)法一:<select 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> 正確的寫(xiě)法二:(推薦)
<select 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>
分析:方法二的寫(xiě)法,需要再請(qǐng)求參數(shù)中額外設(shè)置兩個(gè)get函數(shù),如下:
@Datapublic class QueryParameterVO { private List<String> ids; private List<Integer> statusList; // 前端傳入的頁(yè)碼 private int pageNo; // 從1開(kāi)始 // 每頁(yè)的條數(shù) private int pageSize; // 數(shù)據(jù)庫(kù)的偏移 private int offSet; // 數(shù)據(jù)庫(kù)的大小限制 private int limit; // 這里重寫(xiě)offSet和limit的get方法 public int getOffSet() { return (pageNo-1)*pageSize; } public int getLimit() { return pageSize; }}
到此這篇關(guān)于MyBatis limit分頁(yè)設(shè)置的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)MyBatis limit分頁(yè)內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. SQL2000管理SQL7服務(wù)器出現(xiàn)TIMEOUT問(wèn)題的解決2. Mysql入門(mén)系列:MYSQL服務(wù)器內(nèi)部安全性-安全數(shù)據(jù)目錄訪(fǎng)問(wèn)3. 導(dǎo)出錯(cuò)誤編碼的mysql數(shù)據(jù)庫(kù)4. Mysql入門(mén)系列:在MYSQL結(jié)果集上執(zhí)行計(jì)算5. MySql導(dǎo)出后再導(dǎo)入數(shù)據(jù)時(shí)出錯(cuò)問(wèn)題6. mysql命令行客戶(hù)端結(jié)果分頁(yè)瀏覽7. Mysql入門(mén)系列:MYSQL圖像數(shù)據(jù)的處理8. Oracle面試問(wèn)題-技術(shù)篇9. SQLite學(xué)習(xí)手冊(cè)(SQLite在線(xiàn)備份)10. Oracle災(zāi)難防護(hù)的關(guān)鍵技術(shù)
