SpringBoot 使用使用Mybatis分页插件实现详解分页插件实现详解
主要介绍了SpringBoot 使用Mybatis分页插件实现详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一
定的参考学习价值,需要的朋友可以参考下
这篇文章主要介绍了SpringBoot 使用Mybatis分页插件实现详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参
考学习价值,需要的朋友可以参考下
1、导入分页插件包和、导入分页插件包和jpa包包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
2、增加分页配置、增加分页配置
# 主键自增回写方法,默认值MYSQL,详细说明请看文档
mapper:
identity: MYSQL
# 设置 insert 和 update 中,是否判断字符串类型!=''
not-empty: true
# 枚举按简单类型处理
enum-as-simple-type: true
######### 分页插件 ##########
pagehelper:
helper-dialect: mysql
params:
count: countSql
reasonable: false
support-methods-arguments: true
配置说明:
mapper.enum-as-simple-type: 枚举按简单类型处理,如果有枚举字段则需要加上该配置才会做映射
mapper.not-empty: 设置以后,会去判断 insert 和 update 中符串类型!=''“
pagehelper.reasonable: 分页合理化参数,默认值为false。当该参数设置为 true 时,pageNum<=0 时会查询第一页,
pageNum>pages(超过总数时),会查询最后一页。默认false 时,直接根据参数进行查询。
support-methods-arguments: 支持通过 Mapper 接口参数来传递分页参数,默认值false,分页插件会从查询方法的参数值中,自
动根据上面 params 配置的字段中取值,查找到合适的值时就会自动分页。
3、使用插件进行分页查询、使用插件进行分页查询
public PageInfo<User> selectByUsername(String username,int limit, int page){
PageHelper.startPage(page, limit).setOrderBy("id desc");
PageInfo<User> userPageInfo = new PageInfo<>(this.userMapper.selectByuserName(username));
return userPageInfo;
}
4、测试、测试
此处不在写Controller类及中间Service的调用,直接看调用结果
评论0