分页插件 PageHelper 使用

https://github.com/pagehelper/Mybatis-PageHelper

SSM

https://github.com/abel533/Mybatis-Spring

  1. pom.xml 引入 依赖

    1
    2
    3
    4
    5
    6
    <!-- 分页插件-->
    <dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
    <version>5.1.2</version>
    </dependency>
  2. spring 配置拦截器com.github.pagehelper.PageInterceptor

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="mapperLocations" value="classpath:mapping/*.xml" />
    <property name="databaseIdProvider" ref="databaseIdProvider" />

    <!-- 插件配置 -->
    <property name="plugins">
    <array>
    <bean class="com.github.pagehelper.PageInterceptor">
    <property name="properties">
    <!-- 什么都不配,使用默认的配置 -->
    <value></value>
    </property>
    </bean>
    </array>
    </property>
    </bean>
  3. 使用

    • Controller 文件

      1
      2
      3
      4
      5
      6
      7
      8
      9
      //分页条件
      Integer page = StringEx.tInteger(request.getParameter("page"));
      Integer rows = StringEx.tInteger(request.getParameter("rows"));

      PageInfo<WxuserDto> pageInfo = this.wxuserService.selectWxuserByPage(wxuserDto, page, rows);
      Map<String, Object> map = new HashMap<>();
      map.put("total", pageInfo.getTotal());
      map.put("rows", pageInfo.getList());
      this.writeJson(map, request, response);
    • ServiceImpl 文件

      1
      2
      3
      4
      5
      //放到 DAO 接口执行代码上面一行位置
      PageHelper.startPage(page,rows);

      List<Wxuser> list = wxuserDao.selectWxuserByPage(map);
      PageInfo<Wxuser> pageInfo = new PageInfo<>(list);

Spring Boot

https://github.com/abel533/MyBatis-Spring-Boot