1.pom.xml导入依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
</dependency>
2.创建Page实体
/**
* 分页对象
* @param <T>
*/
public class Page <T> implements Serializable{
//当前默认为第一页
public static final Integer pageNum = 1;
//默认每页显示条件
public static final Integer pageSize = 20;
//判断当前页是否为空或是小于1
public static Integer cpn(Integer pageNum){
if(null == pageNum || pageNum < 1){
pageNum = 1;
}
return pageNum;
}
// 页数(第几页)
private long currentpage;
// 查询数据库里面对应的数据有多少条
private long total;// 从数据库查处的总记录数
// 每页查5条
private int size;
// 下页
private int next;
private List<T> list;
// 最后一页
private int last;
private int lpage;
private int rpage;
//从哪条开始查
private long start;
//全局偏移量
public int offsize = 2;
public Page() {
super();
}
/****
*
* @param currentpage
* @param total
* @param pagesize
*/
public void setCurrentpage(long currentpage,long total,long pagesize) {
//可以整除的情况下
long pagecount = total/pagesize;
//如果整除表示正好分N页,如果不能整除在N页的基础上+1页
int totalPages = (int) (total%pagesize==0? total/pagesize : (total/pagesize)+1);
//总页数
this.last = totalPages;
//判断当前页是否越界,如果越界,我们就查最后一页
if(currentpage>totalPages){
this.currentpage = totalPages;
}else{
this.currentpage=currentpage;
}
//计算start
this.start = (this.currentpage-1)*pagesize;
}
//上一页
public long getUpper() {
return currentpage>1? currentpage-1: currentpage;
}
//总共有多少页,即末页
public void setLast(int last) {
this.last = (int) (total%size==0? total/size : (total/size)+1);
}
/****
* 带有偏移量设置的分页
* @param total
* @param currentpage
* @param pagesize
* @param offsize
*/
public Page(long total,int currentpage,int pagesize,int offsize) {
this.offsize = offsize;
initPage(total, currentpage, pagesize);
}
/****
*
* @param total 总记录数
* @param currentpage 当前页
* @param pagesize 每页显示多少条
*/
public Page(long total,int currentpage,int pagesize) {
initPage(total,currentpage,pagesize);
}
/****
* 初始化分页
* @param total
* @param currentpage
* @param pagesize
*/
public void initPage(long total,int currentpage,int pagesize){
//总记录数
this.total = total;
//每页显示多少条
this.size=pagesize;
//计算当前页和数据库查询起始值以及总页数
setCurrentpage(currentpage, total, pagesize);
//分页计算
int leftcount =this.offsize, //需要向上一页执行多少次
rightcount =this.offsize;
//起点页
this.lpage =currentpage;
//结束页
this.rpage =currentpage;
//2点判断
this.lpage = currentpage-leftcount; //正常情况下的起点
this.rpage = currentpage+rightcount; //正常情况下的终点
//页差=总页数和结束页的差
int topdiv = this.last-rpage; //判断是否大于最大页数
/***
* 起点页
* 1、页差<0 起点页=起点页+页差值
* 2、页差>=0 起点和终点判断
*/
this.lpage=topdiv<0? this.lpage+topdiv:this.lpage;
/***
* 结束页
* 1、起点页<=0 结束页=|起点页|+1
* 2、起点页>0 结束页
*/
this.rpage=this.lpage<=0? this.rpage+(this.lpage*-1)+1: this.rpage;
/***
* 当起点页<=0 让起点页为第一页
* 否则不管
*/
this.lpage=this.lpage<=0? 1:this.lpage;
/***
* 如果结束页>总页数 结束页=总页数
* 否则不管
*/
this.rpage=this.rpage>last? this.last:this.rpage;
}
public long getNext() {
return currentpage<last? currentpage+1: last;
}
public void setNext(int next) {
this.next = next;
}
public long getCurrentpage() {
return currentpage;
}
public long getTotal() {
return total;
}
public void setTotal(long total) {
this.total = total;
}
public long getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public long getLast() {
return last;
}
public long getLpage() {
return lpage;
}
public void setLpage(int lpage) {
this.lpage = lpage;
}
public long getRpage() {
return rpage;
}
public void setRpage(int rpage) {
this.rpage = rpage;
}
public long getStart() {
return start;
}
public void setStart(long start) {
this.start = start;
}
public void setCurrentpage(long currentpage) {
this.currentpage = currentpage;
}
/**
* @return the list
*/
public List<T> getList() {
return list;
}
/**
* @param list the list to set
*/
public void setList(List<T> list) {
this.list = list;
}
public static void main(String[] args) {
//总记录数
//当前页
//每页显示多少条
int cpage =17;
Page page = new Page(1001,cpage,50,7);
System.out.println("开始页:"+page.getLpage()+"__当前页:"+page.getCurrentpage()+"__结束页"+page.getRpage()+"____总页数:"+page.getLast());
}
}
3.创建PageResult实体
public class PageResult<T> {
private Long total;//总记录数
private List<T> rows;//记录
public PageResult(Long total, List<T> rows) {
this.total = total;
this.rows = rows;
}
public PageResult() {
}
public Long getTotal() {
return total;
}
public void setTotal(Long total) {
this.total = total;
}
public List<T> getRows() {
return rows;
}
public void setRows(List<T> rows) {
this.rows = rows;
}
}
4.分页Controller编写
/***
* 分页搜索实现
* @param searchMap
* @param page 当前页
* @param size 每页显示几条(后端可定死)
* @return
*/
@GetMapping(value = "/search/{page}/{size}" )
public Result findPage(@RequestParam Map searchMap, @PathVariable int page, @PathVariable int size){
Page<Order> pageList = orderService.findPage(searchMap, page, size);
PageResult pageResult=new PageResult(pageList.getTotal(),pageList.getResult());
return new Result(true,StatusCode.OK,"查询成功",pageResult);
}
5.分页service
/**
* 条件+分页查询
* @param searchMap 查询条件
* @param page 页码
* @param size 页大小
* @return 分页结果
*/
@Override
public Page<Order> findPage(Map<String,Object> searchMap, int page, int size){
PageHelper.startPage(page,size);
Example example = createExample(searchMap);
return (Page<Order>)orderMapper.selectByExample(example);
}
/**
* 构建查询对象
* @param searchMap
* @return
*/
private Example createExample(Map<String, Object> searchMap){
Example example=new Example(Order.class);
Example.Criteria criteria = example.createCriteria();
if(searchMap!=null){
// 订单id
if(searchMap.get("id")!=null && !"".equals(searchMap.get("id"))){
criteria.andEqualTo("id",searchMap.get("id"));
}
// 支付类型,1、在线支付、0 货到付款
if(searchMap.get("payType")!=null && !"".equals(searchMap.get("payType"))){
criteria.andEqualTo("payType",searchMap.get("payType"));
}
// 物流名称
if(searchMap.get("shippingName")!=null && !"".equals(searchMap.get("shippingName"))){
criteria.andLike("shippingName","%"+searchMap.get("shippingName")+"%");
}
// 物流单号
if(searchMap.get("shippingCode")!=null && !"".equals(searchMap.get("shippingCode"))){
criteria.andLike("shippingCode","%"+searchMap.get("shippingCode")+"%");
}
// 用户名称
if(searchMap.get("username")!=null && !"".equals(searchMap.get("username"))){
criteria.andLike("username","%"+searchMap.get("username")+"%");
}
// 买家留言
if(searchMap.get("buyerMessage")!=null && !"".equals(searchMap.get("buyerMessage"))){
criteria.andLike("buyerMessage","%"+searchMap.get("buyerMessage")+"%");
}
// 是否评价
if(searchMap.get("buyerRate")!=null && !"".equals(searchMap.get("buyerRate"))){
criteria.andLike("buyerRate","%"+searchMap.get("buyerRate")+"%");
}
// 收货人
if(searchMap.get("receiverContact")!=null && !"".equals(searchMap.get("receiverContact"))){
criteria.andLike("receiverContact","%"+searchMap.get("receiverContact")+"%");
}
// 收货人手机
if(searchMap.get("receiverMobile")!=null && !"".equals(searchMap.get("receiverMobile"))){
criteria.andLike("receiverMobile","%"+searchMap.get("receiverMobile")+"%");
}
// 收货人地址
if(searchMap.get("receiverAddress")!=null && !"".equals(searchMap.get("receiverAddress"))){
criteria.andLike("receiverAddress","%"+searchMap.get("receiverAddress")+"%");
}
// 订单来源:1:web,2:app,3:微信公众号,4:微信小程序 5 H5手机页面
if(searchMap.get("sourceType")!=null && !"".equals(searchMap.get("sourceType"))){
criteria.andEqualTo("sourceType",searchMap.get("sourceType"));
}
// 交易流水号
if(searchMap.get("transactionId")!=null && !"".equals(searchMap.get("transactionId"))){
criteria.andLike("transactionId","%"+searchMap.get("transactionId")+"%");
}
// 订单状态
if(searchMap.get("orderStatus")!=null && !"".equals(searchMap.get("orderStatus"))){
criteria.andEqualTo("orderStatus",searchMap.get("orderStatus"));
}
// 支付状态
if(searchMap.get("payStatus")!=null && !"".equals(searchMap.get("payStatus"))){
criteria.andEqualTo("payStatus",searchMap.get("payStatus"));
}
// 发货状态
if(searchMap.get("consignStatus")!=null && !"".equals(searchMap.get("consignStatus"))){
criteria.andEqualTo("consignStatus",searchMap.get("consignStatus"));
}
// 是否删除
if(searchMap.get("isDelete")!=null && !"".equals(searchMap.get("isDelete"))){
criteria.andEqualTo("isDelete",searchMap.get("isDelete"));
}
// 数量合计
if(searchMap.get("totalNum")!=null ){
criteria.andEqualTo("totalNum",searchMap.get("totalNum"));
}
// 金额合计
if(searchMap.get("totalMoney")!=null ){
criteria.andEqualTo("totalMoney",searchMap.get("totalMoney"));
}
// 优惠金额
if(searchMap.get("preMoney")!=null ){
criteria.andEqualTo("preMoney",searchMap.get("preMoney"));
}
// 邮费
if(searchMap.get("postFee")!=null ){
criteria.andEqualTo("postFee",searchMap.get("postFee"));
}
// 实付金额
if(searchMap.get("payMoney")!=null ){
criteria.andEqualTo("payMoney",searchMap.get("payMoney"));
}
}
return example;
}
6.分页mapper
public interface OrderMapper extends Mapper<Order> {
}