目的: 学会分页功能,学会分页方法
场景: 将下面的数据进行分页:
文章目录
Mybatis 单独使用分页(没有整合)
这部分的内容,没有整合,也就是纯粹在 Mybatis 中如何使用;整合的在后面
1. PageHelper 插件
在数据库中,我们知道,分页查询是在 sql 中使用 limit 语句。现在也可以在 xml 中使用,然后调方法的时候将参数传进去即可。
现在就开始介绍如何在 Mybatis 中使用这个插件:
1、引入依赖:
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>6.1.0</version>
</dependency>
2、在配置文件 config.xml 中配置:
<configuration>
<plugins>
<!-- 添加分页拦截器查询 -->
<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>
</configuration>
3、使用步骤如下:
public class Test{
public static void main(String[] args){
// 加载配置文件
InputStream inputStream = Test.class.getClassLoader().getResourceAsStream("config.xml");
SqlSessionFactoryBuilder factoryBuilder = new SqlSessionFactoryBuilder();
SqlSessionFactory factory = factoryBuilder.build(inputStream);
SqlSession sqlSession = factory.openSession();
// 获取mapper:根据自己的mapper修改
AccountRepository repository =
sqlSession.getMapper(AccountRepository.class);
//使用步骤:
//1. 一定要在查询前设置:第一个参数为当前页(从1开始),第二个参数为页的大小
PageHelper.startPage(1,3);
//2. 调查询所有的sql即可:根据自己的代码修改实体类
List<Account> accountList = repository.findAll();
for (Account account : accountList) {
System.out.println(account);
}
// 关闭连接
sqlSession.close();
}
}
发现1: 上面只是将结果分页了,看不到任何其它数据,所以这里再设置下:
// 除了PageInfo,还可以用 Page:Page<Account> page = (Page<Account>) accountList;下面使用PageInfo
PageHelper.startPage(1,3);
//2. 调查询所有的sql即可:根据自己的代码修改实体类
List<Account> accountList = repository.findAll();
//3. 保存分页其它信息,需要PageInfo对象
PageInfo<Account> pageInfo = new PageInfo<>(accountList);
System.out.println(pageInfo);
// 拿到结果中的数据信息
for (Account account : pageInfo.getList()) {
System.out.println(account);
}
// 关闭连接
sqlSession.close();
发现2: PageHelper 是物体分页,也就是真分页(也就是有 limit)。还有的是逻辑分页,也就是假分页(把所有数据都查出来了,然后再分页给你)
Spring Boot 整合 Mybatis Plus 使用分页
这里介绍整合 MP 如何使用分页
1、在实现分页查询时,需要写一个配置类,这样系统会自动实现分页的操作:
@Bean
public PaginationInnerInterceptor page(){
return new PaginationInnerInterceptor();
}
2、先看看查询的方法:
3、详细介绍分析:
1. selectPage 方法实现分页
@Test
void pageSelect(){
//创建一个Page对象: 第一个参数:当前页 第二个参数:每页的记录数
Page<User> page = new Page<>(1,2);
//直接调方法:selectPage,返回的是Page对象的结果
Page<User> result = userMapper.selectPage(page, null);
System.out.println(result.getSize()); //拿到 每页的记录数
System.out.println(result.getCurrent()); //拿到 当前页
System.out.println(result.getPages()); //拿到 总页数
System.out.println(result.getTotal()); // 拿到 总记录数
result.getRecords().forEach(System.out::println); //拿到 数据,并输出
}
问题: 运行发现分页不生效,原因:配置类没有生效。
解决: 可以按照乐观锁的配置类来排除错误:先将这 PaginationInnerInterceptor
换成 PaginationInterceptor
,发现还是不行。
所以正确的配置类为:
@Configuration
public class PageConfig {
//旧版本写法,对于新版本不生效
@Bean
public PaginationInnerInterceptor page(){
return new PaginationInnerInterceptor();
}
//Mybatis Plus版本不同,有的需要将PaginationInnerInterceptor写成PaginationInterceptor
//3.4.0后新版本写法
@Bean
public MybatisPlusInterceptor page(){
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return mybatisPlusInterceptor;
}
}
2. selectMapsPage 方法实现分页
结果被 Map 集合封装了
@Test
void pageSelect(){
Page<Map<String,Object>> page1 = new Page<>(1,2);
Page<Map<String, Object>> result1= userMapper.selectMapsPage(page1, null);
result1.getRecords().forEach(System.out::println);
}
至此!文章结束,后续有啥关于分页的,继续更新