PageHelper插件
我们在正常的查询业务之中,只需要加上一行代码就可以实现分页的数据的封装处理
实现原理
PageHelper方法使用了静态的ThreadLocal参数,分页参数和线程是绑定的。内部流程是ThreadLocal中设置了分页参数(pageIndex,pageSize),之后在查询执行的时候,获取当线程中的分页参数,执行查询的时候通过拦截器在sql语句中添加分页参数,之后实现分页查询,查询结束后在 finally 语句中清除ThreadLocal中的查询参数
使用方法
1.调用PageHelper方法:PageHelper.startPage(pageNum, pageSize)
(pageNum:第几页,pageSize页面大小)
底层代码原理是DataSource中sql limit
2. MyBatis 查询方法
注意:只要你可以保证在PageHelper方法调用后紧跟 MyBatis 查询方法,这就是安全的。因为PageHelper在finally代码段中自动清除了ThreadLocal存储的对象。
MyBatis整合PageHelper插件
第一步:导入pageHelper的jar包
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.4</version>
</dependency>
第二步:在mybatsi的总的配置文件中配置分页插件,说明示例如下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<plugins>
<!-- com.github.pagehelper为PageHelper类所在包名 -->
<plugin interceptor="com.github.pagehelper.PageHelper">
<!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库 -->
<property name="dialect" value="mysql" />
<property name="rowBoundsWithCount" value="true" />
</plugin>
</plugins>
</configuration>
第三步:使用
package cn.gxm.pagehelper;
import java.util.List;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cn.gxm.mapper.TbItemMapper;
import cn.gxm.pojo.TbItem;
import cn.gxm.pojo.TbItemExample;
/**
* 测试mybatsi的插件pagehelper的使用
* @author xiaomi
*
*/
public class TestPageHelper {
@Test
public void testPagehelper() {
//1)在mybatsi的总的配置文件中配置分页插件
//2)在使用mybatis查询之前使用pagehelper的静态方法,设置开始页码(从1开始),每页多少条
PageHelper.startPage(1,10);
//3)使用Mybatis查询信息,放在list中
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-dao.xml");
TbItemMapper tbItemMapper = applicationContext.getBean(TbItemMapper.class);
TbItemExample example = new TbItemExample();
List<TbItem> list = tbItemMapper.selectByExample(example);
//4)使用pageInfo接收mybatis查询的结果集合
PageInfo<TbItem> info = new PageInfo<>(list);
//5)info中会获取原本的数据(本来查询的结果)
System.out.println("原本查询的所有数据总条数"+info.getTotal());
//6)而此时原本使用Mybatis查询信息,放在list中的数据已经是截取之后的了
System.err.println(list.size());
}
//输出如下:
//10
//原本查询的所有数据总条数3096
}
添加PageHelper启动器依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.12</version>
</dependency>
EmpController
package com.mrshun.controller;
import com.mrshun.pojo.Emp;
import com.mrshun.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
/**
* @Auther: Mr顺
* @Date: 2021/9/26 - 09 - 26 - 15:12
* @Description: com.mrshun.controller
* @version: 1.0
*/
@Controller
@RequestMapping("/emp")
public class EmpConroller {
@Autowired
private EmpService empService;
@RequestMapping("findAll")
@ResponseBody
public List<Emp> findAll(){
return empService.findAll();
}
@RequestMapping("findByPage/{pageNum}/{pageSize}")
@ResponseBody
public List<Emp> findByPage(@PathVariable("pageNum") Integer pageNum,@PathVariable("pageSize") Integer pageSize){
return empService.findByPage(pageNum,pageSize);
}
}
Service层代码编写
package com.mrshun.service.impl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mrshun.mapper.EmpMapper;
import com.mrshun.pojo.Emp;
import com.mrshun.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @Auther: Mr顺
* @Date: 2021/9/26 - 09 - 26 - 15:13
* @Description: com.mrshun.service.impl
* @version: 1.0
*/
@Service
public class EmpServiceImpl implements EmpService {
@Autowired
private EmpMapper empMapper;
@Override
public List<Emp> findAll() {
return empMapper.findAll();
}
@Override
public List<Emp> findByPage(Integer pageNum, Integer pageSize) {
Page<Emp> page=PageHelper.startPage(pageNum,pageSize);//下面是依据这种规则进行分页
List<Emp> list =empMapper.findAll();
// 方式1
System.out.println("当前页:"+page.getPageNum());
System.out.println("总页数"+page.getPages());
System.out.println("页大小:"+page.getPageSize());
System.out.println("总记录数:"+page.getTotal());
System.out.println("当前页数据"+page.getResult());
// 方式2
PageInfo<Emp> pi =new PageInfo<>(list);
System.out.println("当前页"+pi.getPageNum());
System.out.println("总页数"+pi.getPages());
System.out.println("页大小"+pi.getSize());
System.out.println("总记录数"+pi.getTotal());
System.out.println("当前页数据"+pi.getList());
return list;
}
}