基于SpringBoot项目MyBatis分页插件实现分页总结

前言

在使用Mybatis时,最头痛的就是写分页了,需要先写一个查询count的select语句,然后再写一个真正分页查询的语句,当查询条件多了之后,会发现真的不想花双倍的时间写 count 和 select,幸好我们有 pagehelper 分页插件,pagehelper 是一个强大实用的 MyBatis 分页插件,可以帮助我们快速的实现MyBatis分页功能,而且pagehelper有个优点是,分页和Mapper.xml完全解耦,并以插件的形式实现,对Mybatis执行的流程进行了强化,这有效的避免了我们需要直接写分页SQL语句来实现分页功能。
现在我把自己在项目中实现分页的方法总结如下:

1.导入Maven依赖




    com.github.pagehelper
    pagehelper-spring-boot-starter
    1.3.0

2.controller中分页方法

 /**
       * @Description: 分页查询
         * @Param: waterloggingPreventionDto
       * @return com.graphsafe.api.msg.RestMessage(封装的返回值信息)
       * @author songwp
       * @date 2021/11/2 15:00
       */
    @PostMapping(value = "getListForPage", produces = "application/json;charset=UTF-8")
    public RestMessage getListForPage(@RequestBody WaterloggingPreventionDto waterloggingPreventionDto){
        WaterloggingPreventionVo result = new WaterloggingPreventionVo();
        List list = waterloggingPreventionService.getListForPage(waterloggingPreventionDto);
        result.setWaterloggingPreventionList(list);
        if (null != waterloggingPreventionDto.getCount()){
            result.setTotal(waterloggingPreventionDto.getCount().intValue());
        }
        return new RestMessage(result);
    }

3.service业务方法的实现

@Override
    public List getListForPage(WaterloggingPreventionDto waterloggingPreventionDto) {
        List list = new ArrayList<>();
        if (waterloggingPreventionDto.getPage() != null && waterloggingPreventionDto.getLimit() != null){
            PageHelper.startPage(waterloggingPreventionDto.getPage(),waterloggingPreventionDto.getLimit());
            list = waterloggingPreventionMapper.getListForPage(waterloggingPreventionDto);
            PageInfo pageInfo = new PageInfo<>(list);
            waterloggingPreventionDto.setCount(pageInfo.getTotal());
        }else {
            list = waterloggingPreventionMapper.getListForPage(waterloggingPreventionDto);
        }
        return list;
    }

4.serviceye业务方法接口

/**
 * @Description: 
 * @ClassName: WaterloggingPreventionService
 * @Author: songwp
 * @Date: 2021/10/16 13:35
 */
public interface WaterloggingPreventionService {
    List getListForPage(WaterloggingPreventionDto waterloggingPreventionDto);
}

5.Mapper方法接口

/**
 * @Description: 
 * @ClassName: WaterloggingPreventionMapper
 * @Author: songwp
 * @Date: 2021/10/16 13:30
 */
@Mapper
public interface WaterloggingPreventionMapper {
    List getListForPage(WaterloggingPreventionDto waterloggingPreventionDto);

}

6.参数实体类

/**
 * @author songwp
 * @Description:
 * @date 2021/10/29:47
 */
@Data
public class WaterloggingPreventionDto {

    @ApiModelProperty(value = "名称")
    private String name;

    //分页
    @ApiModelProperty(value = "当前页码")
    private Integer page; //当前页第一页是0

    @ApiModelProperty(value = "每页条数")
    private Integer limit;//每页步长

    @ApiModelProperty(value = "总条数")
    private Long count;//总条数
}

7.post数据测试分页展示:

基于SpringBoot项目MyBatis分页插件实现分页总结_第1张图片
基于SpringBoot项目MyBatis分页插件实现分页总结_第2张图片

你可能感兴趣的:(基于SpringBoot项目MyBatis分页插件实现分页总结)