springboot中使用mybatisplus自带插件实现分页

springboot中使用mybatisplus自带插件实现分页

1.导入mybatisplus分页依赖

<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-extension</artifactId>
<version>3.4.3.1</version>
</dependency>

2.添加MybatisPlusConfig配置类

@Configuration
@MapperScan("scan.your.mapper.package")
public class MybatisPlusConfig {
/**
* 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题(该属性会在旧插件移除后一同移除)
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));
return interceptor;
}
@Bean
public ConfigurationCustomizer configurationCustomizer() {
return configuration -> configuration.setUseDeprecatedExecutor(false);
}
}

3.测试分页

@Test
void testPage(){
//1、创建page对象
//传入两个参数:当前页 和 每页显示的记录数
Page<User> page = new Page<>(1,3);
//调用mp分页查询的方法,null为没有条件查询
userMapper.selectPage(page,null);
Page pageinfo = new Page(page,size);
//带条件查询
QueryWrapper<实体类> wrapper = new QueryWrapper<>();
Map<String, Object> pageMap = new HashMap<>();
pageMap.put("key", value);
wrapper.eq("name", value);
wrapper.allEq(pageMap);
bbooksMapper.selectPage(pageinfo, wrapper);
//通过page对象获取分页数据
System.out.println("当前页码:" + page.getCurrent());//获取当前页
System.out.println("数据的list集合:" + page.getRecords());//每页数据的list集合
System.out.println("每页显示的记录数:" + page.getSize());//每页显示的记录数
System.out.println("总记录数:" + page.getTotal());//总记录数
System.out.println("总页数:" + page.getPages());//总页数
System.out.println("是否有下一页:" + page.hasNext());//是否有下一页
System.out.println("是否有上一页:" + page.hasPrevious());//是否有上一页
}

图片:

posted @   面向CV工程师  阅读(387)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
点击右上角即可分享
微信分享提示