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;
}
}