mybatis-plus 使用 sql 分页

#分页工具类


/**
* 分页参数处理
*/
public class PageUtil {
/**
* 分页返回数据封装
*
* @param page
* @return Map<String, Object>
* @author Hd
* @date 2021/12/18 12:47
*/
public static Map<String, Object> toPage(Page page) {
Map<String, Object> map = new LinkedHashMap<>(2);
map.put("pages", page.getPages());
map.put("current", page.getCurrent());
map.put("total", page.getTotal());
map.put("list", page.getRecords());
return map;
}

/**
* 分页参数转化
*
* @param orderItems
* @param sqlPrefix
* @return List<OrderItem>
*/
public static List<OrderItem> toOrderItem(List<OrderItem> orderItems, String sqlPrefix) {
//OrderItem(column=updated_time, asc=false)
return orderItems
.stream()
.map(item -> new OrderItem(sqlPrefix + "." + item.getColumn(), item.isAsc()))
.collect(Collectors.toList());
}

#代码Controller
public Result listPage(ProblemInfoQueryDto problemInfoQueryDto) {
return Result.success("查询成功", problemInfoService.listPage(problemInfoQueryDto));
}

#service

Map listPage(ProblemInfoQueryDto problemInfoQueryDto);

#serviceImpl
public Map<String, Object> listPage(ProblemInfoQueryDto problemInfoDto) {
IPage<ProblemInfoVo> page = new Page<>(problemInfoDto.getPage(), problemInfoDto.getSize());
QueryWrapper<ProblemInfo> wrap = new QueryWrapper<>();
wrap.orderByDesc("id");
return PageUtil.toPage(mapper.listPage(page, wrap));
}
#mapper
Page<ProblemInfoVo> listPage(IPage<ProblemInfoVo> page,@Param(Constants.WRAPPER) QueryWrapper<ProblemInfo> wrap);

#xml
SELECT
*
FROM
tb_problem_info ${ew.customSqlSegment}
posted @ 2023-02-27 08:55  Aoul  阅读(253)  评论(0编辑  收藏  举报