接上一篇springCloud的尾。我们这里讲mybatis plus里的分页。
第一种情况,多表关联查询且需要分页。分页依然是使用mybatis plus分页,我们需要做的是去改变Page的结果集。所以我们在service实现类里改变结果集就可以,至于SQL就自定义就可以了,自定义SQL如何写这里不说了。相信用过mybatis的都会写。代码贴一下给大家看看。
controller层方法,入参记得带Page ,带入service实现类,因为我们需要在service实现类里做结果集封装。
public ResultVo queryMyItems(HttpServletRequest request,Userinfo user ,Page page) throws Exception{
ResultVo result = new ResultVo();
result.setError_no(ResultVo.ErrorCode.SUCCESS);
result.setError_info(ResultVo.ErrorMessage.SUCCESS);
try{
/**入参自己校验**/
Page<Map> mapPage = new Page<>(page.getCurrent(),page.getSize());
Map requestParam = new HashMap<>();
Page<Map> myItems= iUserinfoService.queryMyItems(mapPage,requestParam);
result.setResults(myItems);
}catch (Exception e){
result.setError_no(ResultVo.ErrorCode.FAILURE);
result.setError_info(ResultVo.ErrorMessage.FAILURE);
logger.error("调用接口" + request.getRequestURI() + "出现异常,接口入参:"+ RequestParamsToMapUtils.getParameterStringMap(request)
+"错误信息"+ e.toString());
}
return result;
}
接下来就是service实现类了,这里我们进行结果集封装,注意返回参数是Page对象。
public Page<Map> queryMyItems(Page<Map> mapPage, Map requestParam) {
List<Map> myItems = baseMapper.queryMyItems(mapPage,requestParam);
mapPage.setRecords(myItems);
return mapPage;
}
这就可以了。自定义的SQL我就不写了。
第二种情况就是我们只需要多表关联,那我们就自定SQL的写法就可以了。不用带入分页参数。
依旧附上源码地址源码地址