如何详细实现SpringBoot结合Mybatis分页插件?

2026-06-08 17:47:17 398阅读 0评论 SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计790个文字,预计阅读时间需要4分钟。

本文主要介绍了SpringBoot结合Mybatis分页插件的使用,通过示例代码详细解析了实现过程。对初学者或工作者具有一定的参考价值,需要的朋友可参考以下内容:

如何详细实现SpringBoot结合Mybatis分页插件?

1. 引入分页插件包xml com.github.pagehelper pagehelper 5.1.2

2. 配置分页插件javaimport com.github.pagehelper.PageInterceptor;import org.apache.ibatis.plugin.Interceptor;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;

如何详细实现SpringBoot结合Mybatis分页插件?

import java.util.Properties;

@Configurationpublic class MybatisConfig { @Bean public Interceptor pageInterceptor() { PageInterceptor pageInterceptor=new PageInterceptor(); Properties properties=new Properties(); properties.setProperty(offsetAsPageNum, true); properties.setProperty(rowBoundsWithCount, true); properties.setProperty(reasonable, true); pageInterceptor.setProperties(properties); return pageInterceptor; }}

3. 使用分页插件javaimport com.github.pagehelper.PageHelper;import com.github.pagehelper.PageInfo;import org.springframework.stereotype.Service;

import java.util.List;

@Servicepublic class UserService {

public PageInfo listUsers(int pageNum, int pageSize) { PageHelper.startPage(pageNum, pageSize); List users=userMapper.selectAll(); PageInfo pageInfo=new PageInfo(users); return pageInfo; }}

以上是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的调用,直接看调用结果

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。

标签: 分页 插件 详细

本文共计790个文字,预计阅读时间需要4分钟。

本文主要介绍了SpringBoot结合Mybatis分页插件的使用,通过示例代码详细解析了实现过程。对初学者或工作者具有一定的参考价值,需要的朋友可参考以下内容:

如何详细实现SpringBoot结合Mybatis分页插件?

1. 引入分页插件包xml com.github.pagehelper pagehelper 5.1.2

2. 配置分页插件javaimport com.github.pagehelper.PageInterceptor;import org.apache.ibatis.plugin.Interceptor;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;

如何详细实现SpringBoot结合Mybatis分页插件?

import java.util.Properties;

@Configurationpublic class MybatisConfig { @Bean public Interceptor pageInterceptor() { PageInterceptor pageInterceptor=new PageInterceptor(); Properties properties=new Properties(); properties.setProperty(offsetAsPageNum, true); properties.setProperty(rowBoundsWithCount, true); properties.setProperty(reasonable, true); pageInterceptor.setProperties(properties); return pageInterceptor; }}

3. 使用分页插件javaimport com.github.pagehelper.PageHelper;import com.github.pagehelper.PageInfo;import org.springframework.stereotype.Service;

import java.util.List;

@Servicepublic class UserService {

public PageInfo listUsers(int pageNum, int pageSize) { PageHelper.startPage(pageNum, pageSize); List users=userMapper.selectAll(); PageInfo pageInfo=new PageInfo(users); return pageInfo; }}

以上是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的调用,直接看调用结果

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。

标签: 分页 插件 详细