如何实现MyBatis limit分页设置
这篇文章主要介绍如何实现MyBatis limit分页设置,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
错误的写法:<selectid="queryMyApplicationRecord"parameterType="MyApplicationRequest"resultMap="myApplicationMap">SELECTa.*,FROMtb_useraWHERE1=1<iftest="ids!=nullandids.size()!=0">ANDa.idIN<foreachcollection="ids"item="id"index="index"open="("close=")"separator=",">#{id}</foreach></if><iftest="statusList!=nullandstatusList.size()!=0">ANDa.statusIN<foreachcollection="statusList"item="status"index="index"open="("close=")"separator=",">#{status}</foreach></if>ORDERBYa.create_timedescLIMIT(#{pageNo}-1)*#{pageSize},#{pageSize};//错误</select>
在MyBatis中LIMIT之后的语句不允许的变量不允许进行算数运算,会报错。
正确的写法一:<selectid="queryMyApplicationRecord"parameterType="MyApplicationRequest"resultMap="myApplicationMap">SELECTa.*,FROMtb_useraWHERE1=1<iftest="ids!=nullandids.size()!=0">ANDa.idIN<foreachcollection="ids"item="id"index="index"open="("close=")"separator=",">#{id}</foreach></if><iftest="statusList!=nullandstatusList.size()!=0">ANDa.statusIN<foreachcollection="statusList"item="status"index="index"open="("close=")"separator=",">#{status}</foreach></if>ORDERBYa.create_timedescLIMIT${(pageNo-1)*pageSize},${pageSize};(正确)</select>正确的写法二:(推荐)
<selectid="queryMyApplicationRecord"parameterType="MyApplicationRequest"resultMap="myApplicationMap">SELECTa.*,FROMtb_useraWHERE1=1<iftest="ids!=nullandids.size()!=0">ANDa.idIN<foreachcollection="ids"item="id"index="index"open="("close=")"separator=",">#{id}</foreach></if><iftest="statusList!=nullandstatusList.size()!=0">ANDa.statusIN<foreachcollection="statusList"item="status"index="index"open="("close=")"separator=",">#{status}</foreach></if>ORDERBYa.create_timedescLIMIT#{offSet},#{limit};(推荐,代码层可控)</select>
分析:方法二的写法,需要再请求参数中额外设置两个get函数,如下:
@DatapublicclassQueryParameterVO{privateList<String>ids;privateList<Integer>statusList;//前端传入的页码privateintpageNo;//从1开始//每页的条数privateintpageSize;//数据库的偏移privateintoffSet;//数据库的大小限制privateintlimit;//这里重写offSet和limit的get方法publicintgetOffSet(){return(pageNo-1)*pageSize;}publicintgetLimit(){returnpageSize;}}
以上是“如何实现MyBatis limit分页设置”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。