Mybatis之分页插件使用

1.分页插件简介

pagehelper源码
都说这是史上最好用的分页插件,支持多种数据库以多种方式分页。

2.分页插件的使用

2.1导入maven依赖

<dependency>
    <groupId>com.github.pagehelpergroupId>
    <artifactId>pagehelperartifactId>
    <version>最新版本version>
dependency>

2.2 添加配置

1.在mybatis的config配置文件中添加拦截器


<plugins>
        <plugin interceptor="com.github.pagehelper.PageHelper">
            <property name="dialect" value="mysql"/>
            
            
            
            <property name="offsetAsPageNum" value="true"/>
            
            
            <property name="rowBoundsWithCount" value="true"/>
            
            
            <property name="pageSizeZero" value="true"/>
            
            
            
            <property name="reasonable" value="false"/>
            
            
            
            
            <property name="params" value="pageNum=start;pageSize=limit;"/>
        plugin>
    plugins>

2.或者在spring配置中添加

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  
  <property name="plugins">
    <array>
      <bean class="com.github.pagehelper.PageInterceptor">
        <property name="properties">
          
          <value>
            params=value1
          value>
        property>
      bean>
    array>
  property>
bean>

这两种方式不能同时用

3.在代码中的使用

3.1设置一个基础的请求类

public class BaseRequest implements Serializable {

    private static final long serialVersionUID = 1193444819529643410L;

    private Integer pageNum;//页数
    private Integer pageSize;//每页行数
    private Boolean count;//是否查询总条数

    public Integer getPageNum() {
        return pageNum;
    }

    public void setPageNum(Integer pageNum) {
        this.pageNum = pageNum;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Boolean getCount() {
        return count;
    }

    public void setCount(Boolean count) {
        this.count = count;
    }

    @Override
    public String toString() {
        return "BaseRequest{" +
                "pageNum=" + pageNum +
                ", pageSize=" + pageSize +
                '}';
    }
}

3.2 设置一个基础的PageService 接口

让每个service 去实现这个接口来设置分页的初始值

public interface PageService {

    default void setDefaultPageInfo(BaseRequest baseRequest) {
        if (null != baseRequest) {
            baseRequest.setPageNum(null == baseRequest.getPageNum() ? Constants.PAGE_NUM : baseRequest.getPageNum());
            baseRequest
                    .setPageSize(null == baseRequest.getPageSize() ? Constants.PAGE_SIZE : baseRequest.getPageSize());
            baseRequest.setCount(null == baseRequest.getCount() ? Boolean.TRUE : baseRequest.getCount());
        } else {
            baseRequest = new BaseRequest();
            baseRequest.setPageNum(Constants.PAGE_NUM);
            baseRequest.setPageSize(Constants.PAGE_SIZE);
            baseRequest.setCount(Boolean.TRUE);
        }
        PageHelper.startPage(baseRequest.getPageNum(), baseRequest.getPageSize(),baseRequest.getCount());
    }

}

3.3 如果做了数据转换这用来复制属性值(可选)

数据模型entity 只对应数据库表中的字段,
出参与入参 都是数据传输对象 dto , 从数据库中查出来的是entity而 接口返回的是dto 所要BeanUtils.copyProperties复制属性,和pageutils.copyProperties 复制分页属性

public class PageUtils {

    public static void copyProperties(PageInfo source, PageInfo des) {
        des.setEndRow(source.getEndRow());
        des.setFirstPage(source.getFirstPage());
        des.setHasNextPage(source.isHasNextPage());
        des.setHasPreviousPage(source.isHasPreviousPage());
        des.setIsFirstPage(source.isIsFirstPage());
        des.setIsLastPage(source.isIsLastPage());
        des.setNavigatepageNums(source.getNavigatepageNums());
        des.setNavigatePages(source.getNavigatePages());
        des.setNextPage(source.getNextPage());
        des.setOrderBy(source.getOrderBy());
        des.setPageNum(source.getPageNum());
        des.setPages(source.getPages());
        des.setPageSize(source.getPageSize());
        des.setPrePage(source.getPrePage());
        des.setSize(source.getSize());
        des.setStartRow(source.getStartRow());
        des.setTotal(source.getTotal());
    }
}

4.使用示例

在OrderService实现类中

import com.github.pagehelper.PageInfo;
import com.javxuan.common.util.PageUtils;
import com.javxuan.order.entity.Order;
import com.javxuan.order.response.OrderDto;
import com.javxuan.order.service.PageService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;

public class OrderServcieImpl implements IOrderServcie, PageService {

    @Autowired
    IOrderMapper orderMapper;

    @GetMapping("/order")
    public PageInfo list(OrderRequest orderRequest){
        //设置默认分页信息 PageService的方法
        setDefaultPageInfo(orderRequest);

        //查出order列表
        List orderList = orderMapper.selectList();
        //将entity的列表分页
        PageInfo orderPageInfo = new PageInfo<>(orderList);

        //连续显示5页与上面的二选一
        //PageInfo orderPageInfo = new PageInfo<>(orderList,5);

        //定义一个数据传输对象dtoList
        List dtoList = new ArrayList<>();
        if(null==orderList || orderList.size<=0){
            return null;
        }

        //给dtoList 加值
        for(Order order:orderList){
            OrderDto dto = new OrderDto();
            //将entity 的属性值 复制给dto上
            BeanUtils.copyProperties(order, dto);
            dtoList.add(dto);
        }

        //给dto 分页
        PageInfo dtoPageInfo = new PageInfo<>(dtoList);

        //连续显示5页 与上面的二选一
        //PageInfo orderPageInfo = new PageInfo<>(orderList,5);

        //将entity的分页信息复制给dtoPageInfo上
        PageUtils.copyProperties(orderPageInfo, dtoPageInfo);
        return dtoPageInfo;
    }
}

你可能感兴趣的:(Mybatis,Mybatis)