如何使用springboot和mybatis-Puls的IPage,Page实现分页查询

1controller层

@RestController
@RequestMapping("/xxxx")
@Api(tags="xxxx")
public class xxxController {
    @Autowired
    private xxxService xxxService;

    @GetMapping("/xxxx")
    @ApiOperation("分页")
    @ApiImplicitParams({
        @ApiImplicitParam(name ="page", value = "当前页码,从1开始", paramType = "query", required = true, dataType="int") ,
        @ApiImplicitParam(name ="limit", value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
        @ApiImplicitParam(name ="xxxName", value = "xxxid", paramType = "query", dataType="String"),
        @ApiImplicitParam(name ="status", value = "状态", paramType = "query", dataType="int")
    })
//PageResult为自己封装的分页返回工具类
    public Result<PageResult> page(@ApiIgnore @RequestParam Map<String, Object> params){
        IPage<xxxPageDto> page = xxxService.page(params);

        PageResult pageResult = new PageResult(page);

        return new Result<PageResult>().ok(pageResult);
    }
}

2.xxxMapper层


public interface xxxMapper extends BaseMapper<xxx> {

    //IPage<xxx> iPage一定要注意加上
    IPage<xxxPageDto> page(IPage<xxx> iPage,@Param("params") Map<String, Object> params);
}

使用Mybatis-Plus进行数据分页,就需要注意加上IPage<xxx> iPage。否者会报找不到IPage。

将与xxxMapper对应的xxxMapper.xml文件查询出来的数据,根据dto里的属性,与查询出来的数据字段一一对应,封装到IPage<xxxPageDto>里,然后返回给xxxServiceImpl层,然后再进行一次封装,最后把数据返回给前端。

3.xxxService层

public interface xxxService extends IService<xxx> {


    IPage<xxxPageDto> page(Map<String, Object> params);

}

4.xxxServiceImpl层

@Service
public class xxxServiceImpl extends ServiceImpl<xxxMapper,xxx> implements xxxService {

    @Autowired
    private xxxMapper xxxMapper;

    @Override
    public IPage<xxxPageDto> page(Map<String, Object> params) {


        //先将Object数据转为String,再通过Integer的parseInt方法将字符串转换为int
        int page = Integer.parseInt((String)params.get("page"));
        int limit = Integer.parseInt((String)params.get("limit"));
      
        IPage<xxx> iPage = new Page<>(page,limit);
        IPage<xxxPageDto> pageDate = xxxMapper.page(iPage,params);
        return pageDate;
    }
}

Page是IPage的实现类,IPage无法实例。

注意点:

1.注意在xxxMapper层加上IPage<xxx> iPage参数

2.注意在xxxServiceImpl层上加上IPage<xxx> iPage = new Page<>();

PageResult工具类

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;
import java.util.List;

@ApiModel("分页对象")
public class PageResult implements Serializable {
  private static final long serialVersionUID = 1L;
  /**
   * 总记录数
   */
  @ApiModelProperty("总记录数")
  private int totalCount;
  /**
   * 每页记录数
   */
  @ApiModelProperty("每页显示多少条")
  private int pageSize;
  /**
   * 总页数
   */
  @ApiModelProperty("总页数")
  private int totalPage;
  /**
   * 当前页数
   */
  @ApiModelProperty("当前是第几页")
  private int currPage;
  /**
   * 列表数据
   */
  @ApiModelProperty("当前页的数据")
  private List<?> list;
  
  /**
   * 分页
   * @param list        列表数据
   * @param totalCount  总记录数
   * @param pageSize    每页记录数
   * @param currPage    当前页数
   */
  public PageResult(List<?> list, int totalCount, int pageSize, int currPage) {
     this.list = list;
     this.totalCount = totalCount;
     this.pageSize = pageSize;
     this.currPage = currPage;
     this.totalPage = (int)Math.ceil((double)totalCount/pageSize);
  }

  /**
   * 分页
   */
  public PageResult(IPage<?> page) {
     this.list = page.getRecords();
     this.totalCount = (int)page.getTotal();
     this.pageSize = (int)page.getSize();
     this.currPage = (int)page.getCurrent();
     this.totalPage = (int)page.getPages();
  }

  public int getTotalCount() {
     return totalCount;
  }

  public void setTotalCount(int totalCount) {
     this.totalCount = totalCount;
  }

  public int getPageSize() {
     return pageSize;
  }

  public void setPageSize(int pageSize) {
     this.pageSize = pageSize;
  }

  public int getTotalPage() {
     return totalPage;
  }

  public void setTotalPage(int totalPage) {
     this.totalPage = totalPage;
  }

  public int getCurrPage() {
     return currPage;
  }

  public void setCurrPage(int currPage) {
     this.currPage = currPage;
  }

  public List<?> getList() {
     return list;
  }

  public void setList(List<?> list) {
     this.list = list;
  }
  
}

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值