使用mybatis-generator生成的Example,快速分页查询模板代码
controller接口/*** 分页列表查询** @param pageNum页号* @param pageSize 页面大小* @param sortType 排序* @return BaseResult*/@GetMapping(value = "/getCustomerByPage")public BaseResult getCustomerByPage(@RequestParam("pa
·
controller接口
/**
* 分页列表查询
*
* @param pageNum 页号
* @param pageSize 页面大小
* @param sortType 排序
* @return BaseResult
*/
@GetMapping(value = "/getCustomerByPage")
public BaseResult getCustomerByPage(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize, String sortType) {
PageResult<Customer> pageResult = customerService.getCustomerByPage(pageNum, pageSize);
return new BaseResult(200, "查询成功", pageResult.getData(), pageResult.getTotalCount());
}
serviceImpl实现
@Override
public PageResult<Customer> getCustomerByPage(Integer pageNum, Integer pageSize, String name) {
CustomerExample example = new CustomerExample();
example.setOrderByClause(" update_time desc");
CustomerExample.Criteria criteria = example.createCriteria();
// TODO 处理查询逻辑
if (StringUtils.isNotEmpty(name)) {
criteria.andNameLike("%" + name + "%");
}
//计算总数
int totalCount = customerMapper.countByExample(example);
if (pageSize != null && pageNum != null) {
example.setLimitStart((pageNum - 1) * pageSize);
example.setLimitEnd(pageSize);
}
//分页查询
List<Customer> list = customerMapper.selectByExample(example);
return new PageResult<>(totalCount, list);
}
PageResult
@Data
@NoArgsConstructor
@AllArgsConstructor
public class PageResult<T> {
int totalCount;
List<T> data;
}
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐



所有评论(0)