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

 

猜你喜欢

转载自www.cnblogs.com/liconglong/p/11693782.html