本文共 2488 字,大约阅读时间需要 8 分钟。
Layui分页查询实践指南
前言
近期在同事的代码中观察到了一种高效的分页查询处理方式,采用Layui配合Mybatis-Plus实现,适用于多表联查场景。这一方法不仅逻辑清晰,还简化了业务层的条件判断逻辑。
Layui分页查询VO对象
分页查询的核心数据类为LayuiData,定义如下:
@Data@AllArgsConstructor@NoArgsConstructorpublic class LayuiData { private Integer code; // Request code private Long count; // Total number private String msg; // Description private Object data; //查询结果}
Controller逻辑
/getClientList
接口的参数解析和分页处理:
@GetMapping("/getClientList")@ResponseBodypublic LayuiData getAll( @RequestParam(name = "page", required = true, defaultValue = "1") int num, @RequestParam(name = "limit", required = true, defaultValue = "10") int size, String keyWord, String registerTime) { IPage
Service层逻辑
findClientPage
的构建:
IPage
Mapper映射逻辑
findClientPage
的 Mapper 方法:
IPage > findClientPage( @Param(Constants.WRAPPER) QueryWrapper > wrapper) { return "'" + ew.customSqlSegment + "'";}
XML数据库映射
主查询文件中使用ew工具:
总结
这种方式避免了繁琐的条件判断逻辑,将业务逻辑集中在业务层,留下清晰的参数处理逻辑,便于扩展和维护。在需要多表查询的情况下,尤其适用。运用Mybatis-Plus的灵活性和高效性,极大地简化了代码结构,更清晰地实现了业务需求。
本文旨在分享一种高效的分页查询处理方式,适用于Layui以及需要动态参数处理的场景。
转载地址:http://mphtz.baihongyu.com/