使用MyBatis-Plus实现分页
- 配置MyBatis-Plus分页插件
@Configuration
public class MyBatisPlusConfig {
// 最新版
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
- 在Controller类中实现分页,用JSON返回给前端
@GetMapping("/queryAllProcess")
public JSONObject queryAllProcess(Long current,Long size){
JSONObject json = new JSONObject();
if (current==null || size==null){
json.put("success",0);
return json;
}
Page<Process> page = processService.page(new Page<Process>(current, size));
if (page==null){
json.put("success",0);
return json;
}
current = page.getCurrent(); //当前页
List<Process> processes = page.getRecords(); //数据
long total = page.getTotal(); //总数据条数
size = page.getSize(); //每页显示多少条数据
long pages = page.getPages(); //页数
json.put("success",1);
json.put("current",current);
json.put("total",total);
json.put("size",size);
json.put("pages",pages);
json.put("processes",processes);
return json;
}