MyBatisPlusConfig is a pagination plugin
/** * Configure the paging plug-in *@return page
*/
@Bean
public PaginationInterceptor paginationInterceptor(a){
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
// Enable count join optimization for only part of the left join
paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
return paginationInterceptor;
}
Copy the code
1. Principle of paging implementation
The Mybatis- Plus paging plug-in uses IPage for paging. The IPage internals are based on interceptors, which intercept methods and their parameters. Check whether it is a query operation. If it is a query operation, it will enter the processing logic of paging. Once paging logic is processed, the interceptor determines whether an implementation class for an IPage object exists by reflecting the method’s parameters. If it does not exist, no paging is performed. If it does exist, the parameter is assigned to the IPage object and then the paging operation is completed by concatenating the SQL.
2. Unified result set
1. Create a return code definition class
public class ResultCode {
/** * succeeded **/
public final static int OK = 20000;
/** * failed **/
public final static int ERROR = 20001;
/** * The username or password is incorrect **/
public final static int LOGIN_ERROR = 20002;
/** ** Permissions are insufficient **/
public final static int ACCESS_ERROR = 20003;
/** * Remote call failed **/
public final static int REMOTE_ERROR = 20004;
/** * repeat the operation **/
public final static int REPEAT_ERROR = 20005;
}
Copy the code
2. Create a result set class
@Data
@apiModel (value = "ApiModel ")
public class R implements Serializable {
public final static String OK_MSG = "Request successful";
public final static String FAIL_MSG = "Request failed";
@apiModelProperty (value = "successful ")
private boolean success;
@apiModelProperty (value = "return code ")
private Integer code;
@apiModelProperty (value = "return message ")
private String message;
@apiModelProperty (value = "return data ")
private Object data;
@apiModelProperty (value = "ApiModelProperty ")
private Long total;
@apiModelProperty (value = "pagination ")
private PageInfo pageInfo;
@Data
public static class PageInfo {
@apiModelProperty (" current page ")
protected int currentPage;
@APIModelProperty (" page size ")
protected int pageSize;
@APIModelProperty (" Total records ")
protected long totalCount;
@APIModelProperty (" Total pages ")
protected long totalPage;
public PageInfo(a) {}@ConstructorProperties({"currentPage", "pageSize", "totalCount", "totalPage"})
public PageInfo(int currentPage, int pageSize, long totalCount, long totalPage) {
this.currentPage = currentPage;
this.pageSize = pageSize;
this.totalCount = totalCount;
this.totalPage = totalPage; }}private R(a){}
private R(int code, String msg, Object data) {
this.code = code;
this.message = msg;
if (data instanceofPage<? >) { Page<? > page = (Page<? >) data;this.total = page.getTotal();
this.data = page.getRecords();
this.pageInfo = new PageInfo((int)page.getCurrent(), (int)page.getSize(), page.getTotal(), page.getPages());
} else {
this.data = data; }}public static R ok(a){
R r = new R();
r.setSuccess(true);
r.setCode(ResultCode.OK);
r.setMessage("Success");
return r;
}
public static R ok(Object data) {
return new R(ResultCode.OK, OK_MSG, data);
}
public static R ok(String msg, Object data) {
return new R(ResultCode.OK, msg, data);
}
public static R error(a){
R r = new R();
r.setSuccess(false);
r.setCode(ResultCode.ERROR);
r.setMessage("Failure");
return r;
}
public static R error(String msg) {
return new R(ResultCode.ERROR, msg, null);
}
public static R error(int errorCode, String msg) {
return new R(errorCode, msg, null);
}
public R message(String message){
this.setMessage(message);
return this;
}
public R code(Integer code){
this.setCode(code);
return this;
}
public R data(Object data){
this.setData(data);
return this; }}Copy the code
Third, write a paging interface
1. Write the query class first
The code is as follows:
@Data
public class MemberQueryVo extends BasePageEntity{
@apiModelProperty (value = "username ")
private String userName;
}
Copy the code
2. The service layer
First define a query paging interface, in the implementation class to do the relevant processing
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper.Member> implements MemberService {
@Override
public IPage<Member> listMemberPage(MemberQueryVo queryVo) {
IPage<Member> page = new Page<>(queryVo.getCurrentPage(),queryVo.getCurrentPage());
// Conditional query
LambdaQueryWrapper<Member> queryWrapper = new LambdaQueryWrapper<Member>();
if (StringUtils.isNotBlank(queryVo.getUserName())) {
queryWrapper.like(Member::getUserName, queryVo.getUserName());
}
returnbaseMapper.selectPage(page,queryWrapper); }}Copy the code
3. The controller layer
Write paging interface, the code is as follows:
@apiOperation (value = "ApiOperation ")
@GetMapping(value = "/getPage")
public R listPage(MemberQueryVo queryVo){
IPage<Member> page = memberService.listMemberPage(queryVo);
return R.ok(page);
}
Copy the code
4. Interface test
The API interface page generated by Swagger is directly tested. When the parameter of the current page and each page is passed as 1, the total number of returned paging information is two and only one data is returned. The paging succeeded.When the conditional query is performed, the corresponding data is successfully queried.
conclusion
Thank you for reading. That’s what I’m going to talk about today. This article briefly explains how to configure paging plug-ins and how paging works. If there is a deficiency, purely limited ability, please forgive me.