Get the App
SLTechnology News&Howtos  ›  Development  › 

How to implement MyBatis limit paging settings

Shulou Source: shulou.com Published: 2022-06-01 12:58:41 09月17日 Update

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!

Tags: Writing method content data database method article error recommendation two code value interest function front end parameter variable size buddy partner more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Shulou Technology MariaDB Docker Shulou Information