mybatis-- 分页插件(pageHelper)

pageHelper插件的优点和弊端

1.优点:快速,方便,简洁的查询结构,后端只需要提供一个select查询返回数据,插件自动给你的分页

java代码框架

@GetMapping("/ceshi")
public BaseResponse ceshi(Model model, @RequestParam(required = false, defaultValue = "1", value = "pn") Integer pn, String 请求参数(可有可无,限情况而定)) {
BaseResponse response = new BaseResponse();
try {
PageHelper.startPage(pn, 6); //pn--页数默认为1,定值---每页的数量 //注意:改分页后必须跟一条sql,是根据这条sql limit 0,6,
List<实体> date = service.select(请求参数);
PageInfo<实体> p = new PageInfo<>(date);
model.addAttribute("deptList", date);
model.addAttribute("page", p);
response.setData(p);
response.setSuccess(true);
return response;
} catch (Exception e) {
response.setErrorMsg(ErrorCode.SYSTEM_ERROR.getDesc());
}
return response;
}

2.弊端:

PageHelper.startPage(pn, 6);
只会针对那一条sql进行limit ,第二条及其之后的sql不会limit
结论:针对弊端可将多条sql进行一个整合,整合为一条sql,
建议:
1.可能会用到sql函数:(select * from table1) union all (select * from table2) 注意:前后sql结构必须一致
  2.select *,count(distincf 条件字段) from table group by 条件字段 出现*,count(字段) 后面必须跟grounp by 字段
根据指定条件对100+条数据的其中几条数据进行字段筛选

 

posted @ 2019-07-19 10:29  up-zyn  阅读(852)  评论(0编辑  收藏  举报