zoukankan      html  css  js  c++  java
  • springboot mybatis 使用 pageHelper

    pagehelper 做分页用

    1.引入依赖包

    <!--PageHelper-->
            <dependency>
                <groupId>com.github.pagehelper</groupId>
                <artifactId>pagehelper</artifactId>
                <version>5.1.3</version>
            </dependency>
            <dependency>
                <groupId>com.github.pagehelper</groupId>
                <artifactId>pagehelper-spring-boot-autoconfigure</artifactId>
                <version>1.2.3</version>
            </dependency>
            <dependency>
                <groupId>com.github.pagehelper</groupId>
                <artifactId>pagehelper-spring-boot-starter</artifactId>
                <version>1.2.3</version>
            </dependency>
    

    2.配置pagehelper

    pagehelper:
      helperDialect: mysql
      reasonable: false  #开启优化,如果开启优化,在分页页码结果没有数据的时候,会显示有数据的页码数据
      supportMethodsArguments: true #是否支持接口参数来传递分页参数,默认false
      pageSizeZero: false #pageSize=0 返回所有
      params: count=countSql
    

    3.引用

     
    @Service
    public class SysUserServiceImpl implements SysUserService {
    
        @Resource
        SysUserMapper sysUserMapper;
    
        //@Cacheable(cacheNames = "listSysUser")//缓存
        @Override
        public PageResult listSysUser(Integer pageSize, Integer pageNum) {
    
            PageHelper.startPage(pageNum,pageSize);
    
            List<SysUser> userList = sysUserMapper.selectByExample(null);
            PageInfo<SysUser> pageInfo = new PageInfo<SysUser>(userList);
    
            return new PageResult(pageInfo);
        }
    }
    

    4.重新封装结果

    由于我需要的参数不多(list,pageNum,pages,total),所以用PageResult重新封装了一下结果

    • PageResult 类:
    
    @Data
    public class PageResult extends ResultBase {
    
        private long total;
        private int pageNum;
        private int pages;
        private List list;
    
        public PageResult(PageInfo pageInfo){
            this.setCode(1);
            this.total = pageInfo.getTotal();
            this.pageNum = pageInfo.getPageNum();
            this.pages = pageInfo.getPages();
            this.list = pageInfo.getList();
        }
    }
    
    
    • ResultBase 类:
    
    @Getter
    @Setter
    public class ResultBase {
    
        private int code;
        private String message;
    
        public  ResultBase success(){
            this.code = 1;
            return this;
        }
    
        public  ResultBase success(String msg){
            this.message = msg;
            this.code = 1;
            return this;
        }
    
        public  ResultBase fail(String msg){
            this.message = msg;
            this.code = 0;
            return this;
        }
    }
    
  • 相关阅读:
    ubuntu18 任务栏调到底部
    十六, Oracle约束
    十五, 数据导出导入
    十四, 角色
    十三,权限
    十二, 系统默认参数
    十一,函数
    PL/SQL 九九乘法表
    十,事务和锁
    九, 表查询 三
  • 原文地址:https://www.cnblogs.com/monkay/p/11170243.html
Copyright © 2011-2022 走看看