国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

MyBatis limit分頁設置的實現

瀏覽:63日期:2023-10-19 14:30:10
錯誤的寫法:

<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>

在MyBatis中LIMIT之后的語句不允許的變量不允許進行算數運算,會報錯。

正確的寫法一:

<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> 正確的寫法二:(推薦)

<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>

分析:方法二的寫法,需要再請求參數中額外設置兩個get函數,如下:

@Datapublic class QueryParameterVO { private List<String> ids; private List<Integer> statusList; // 前端傳入的頁碼 private int pageNo; // 從1開始 // 每頁的條數 private int pageSize; // 數據庫的偏移 private int offSet; // 數據庫的大小限制 private int limit; // 這里重寫offSet和limit的get方法 public int getOffSet() { return (pageNo-1)*pageSize; } public int getLimit() { return pageSize; }}

到此這篇關于MyBatis limit分頁設置的實現的文章就介紹到這了,更多相關MyBatis limit分頁內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Mybatis 數據庫
相關文章:
主站蜘蛛池模板: 淮安市| 丹江口市| 舞钢市| 镇平县| 江北区| 称多县| 上饶县| 五峰| 杭州市| 兴山县| 讷河市| 隆子县| 安泽县| 五原县| 金平| 治县。| 平遥县| 武强县| 长丰县| 轮台县| 石棉县| 蒙山县| 崇左市| 攀枝花市| 讷河市| 龙门县| 文登市| 靖远县| 香格里拉县| 凯里市| 大厂| 长顺县| 皮山县| 高州市| 建宁县| 道孚县| 山东省| 敦化市| 湖北省| 邵阳县| 交城县|