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;
    }
}
posted @ 2019-07-11 15:22  luck-monkey  阅读(218)  评论(0编辑  收藏  举报