How to implement MyBatis limit paging settings
This article mainly introduces how to achieve MyBatis limit paging settings, the article introduces in great detail, has a certain reference value, interested friends must read it!
Incorrect writing: SELECT a WHERE 1 AND a.id IN # {id} AND a.status IN # {status} ORDER BY a.create_time desc LIMIT (# {pageNo}-1) * # {pageSize}, # {pageSize}; / / error
In MyBatis, variables that are not allowed by statements after LIMIT are not allowed to do arithmetic, and an error will be reported.
The correct way to write it is: SELECT a WHERE 1 AND a.id IN # {id} AND a.status IN # {status} ORDER BY a.create_time desc LIMIT ${(pageNo-1) * pageSize}, ${pageSize} (correct) correct writing two: (recommended) SELECT a WHERE 1 AND a.id IN # {id} AND a.status IN # {status} ORDER BY a.create_time desc LIMIT # {offSet}, # {limit}; (recommended, the code layer is controllable)
Analysis: to write method 2, you need to set two additional get functions in the request parameters, as follows:
@ Datapublic class QueryParameterVO {private List ids; private List statusList; / / Page number passed in from the front end private int pageNo; / / number of entries per page from 1 / / offset of the database private int offSet; / / size limit of the database private int limit; / / here rewrite the get method public int getOffSet () {return (pageNo-1) * pageSize;} public int getLimit () {return pageSize of offSet and limit }} the above is all the contents of the article "how to achieve MyBatis limit paging settings". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!