一文学会mybatisPlus分页对象IPage的使用

比较简单,不废话,直接上代码

首先引入maven依赖

  
      com.baomidou
      mybatis-plus-boot-starter
      3.3.0
  

自己封装的PageList


@ApiModel
@Data
public class PageList implements Serializable {
    private static final long serialVersionUID = 1412759446332294208L;

    @ApiModelProperty(value = "每页数量")
    private int pageSize;

    @ApiModelProperty(value = "当前页")
    private int curPage;

    @ApiModelProperty(value = "数据总条数")
    private long totalCount;


    @ApiModelProperty(value = "总页数")
    private int totalPage;


    @ApiModelProperty(value = "数据LIST")
    private List list = new ArrayList();

    @ApiModelProperty(value = "数据合计")
    private Object dataTotal;


    public PageList() {
    }

    public PageList(int curPage, int pageSize, long totalItems) {
        this.curPage = curPage;
        this.pageSize = pageSize;
        this.totalCount = totalItems;
        this.totalPage = (int) Math.ceil((double) totalItems / pageSize);
    }


    public PageList(PageInfo page) {
        this.list.addAll(page.getList());
        this.totalCount = (int) page.getTotal();
        this.pageSize = (int) page.getSize();
        this.curPage = (int) page.getPageNum();
        this.totalPage = page.getPages();
    }

}

Controller层

 	@Autowired
    private TestService testService;

    /**
     * 分页查询列表
     * @param page
     * @param pageSize
     * @return
     */
    @PostMapping(value = "getPageList")
    public PageList<TestVo> getPageList(Integer page,Integer pageSize) {
        IPage<TestVo> pageInfo = testService.getPageList(page,pageSize);
        PageList<TestVo> pageList = new PageList<TestVo>(new Long(pageInfo.getCurrent()).intValue(),
                new Long(pageInfo.getSize()).intValue(), pageInfo.getTotal());
        pageList.setList(pageInfo.getRecords());
        return pageList;
    }

Service层

	public interface TestService {
	    IPage<TestVo> getPageList(Integer page, Integer pageSize);
	}

ServiceImpl层

    @Autowired
    private TestMapper testMapper;
    
    @Override
    public IPage<TestVo> getPageList(Integer page, Integer pageSize) {
        Page<TestVo> page = new Page(page,pageSize);
        List<TestVo> result = testMapper.getPageList(page);
        page.setRecords(result);
        return page;
    }

mapper层代码暂时就不写在这了,都是sql语句,结合以上结构,添加自己业务的参数进去即可

PS:TestVo为自己要返回的实体类对象

你可能感兴趣的:(JAVA,mybatisPlus,mysql,mybatis)