SpringBoot 使用Mybatis分页插件实现详解

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

SpringBoot 使用Mybatis分页插件实现详解

  2021-04-02 我要评论

这篇文章主要介绍了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的调用,直接看调用结果

您可能感兴趣的文章:

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们