一 首先进行导包
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.1.10</version> </dependency>
二 本次所用到的mybatis是用逆向工程生成的查询语句,是java类,并不是XML文件
定义查询对象PageQuery.java
import java.util.List; public class PageQuery<T> { private int pageIndex = 0; private int pageSize = 20; private long totalCount; private T queryModel; private List<T> list; public PageQuery() { } get and set... }
使插件生效
@Configuration @MapperScan("com.xxx.mybatis.mapper") public class SpringConfig { @Bean public PageInterceptor pageInterceptor() { return new PageInterceptor(); } }
Service
public PageQuery<EquipmentOnlineStatus> query(PageQuery<EquipmentOnlineStatus> pageQuery) { log.info("recived pageQuery is {}", pageQuery); EquipmentOnlineStatus equipment = pageQuery.getQueryModel(); EquipmentOnlineStatusExample example = new EquipmentOnlineStatusExample(); EquipmentOnlineStatusExample.Criteria criteria = example.createCriteria(); if (StringUtils.isNotBlank(equipment.getEquipmentId())) { criteria.andEquipmentIdEqualTo(equipment.getEquipmentId()); } if (StringUtils.isNotBlank(equipment.getContractNo())) { criteria.andContractNoEqualTo(equipment.getContractNo()); } if (StringUtils.isNotBlank(equipment.getCubeNo())) { criteria.andCubeNoEqualTo(equipment.getCubeNo()); } if (StringUtils.isNotBlank(equipment.getTechnician())) { criteria.andTechnicianEqualTo(equipment.getTechnician()); } if (StringUtils.isNotBlank(equipment.getPhoneNumber())) { criteria.andPhoneNumberEqualTo(equipment.getPhoneNumber()); } // todo 需要排序时打开 // example.setOrderByClause("UpdateDate DESC"); PageRowBounds rowBounds = new PageRowBounds(pageQuery.getPageIndex() * pageQuery.getPageSize(), pageQuery.getPageSize()); List<EquipmentOnlineStatus> equipmentList = equipmentOnlineStatusMapper.selectByExampleWithRowbounds(example, rowBounds); pageQuery.setList(equipmentList); pageQuery.setTotalCount(rowBounds.getTotal()); return pageQuery;
请求的参数
{ "pageIndex":0, "pageSize":5, "queryModel":{ "equipmentId": null, "contractNo": null, "cubeNo": null, "technician": null, "phoneNumber": null, "email": null } }