Mybatis-Plus分页和根据日期查询数据

我目前使用的mybatis-plus是3.2.0版本,目前最新的版本是3.4.2。由于项目写了很多,不方便改成最新版。

最新版和本版本也有不少差距,但是均可忽略。

就本版本而言,主要讨论两个问题,一个是分页,一个是根据日期查询。

1.分页

首先按照官方文档,编写一个配置类:

|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @ClassName: * @author: flitsneak nonagon * @date: 2021/3/12 */ @Configuration public class MybatisPlusConfig { @Bean public PaginationInterceptor paginationInterceptor(){ PaginationInterceptor paginationInterceptor = new PaginationInterceptor(); return paginationInterceptor; } } |

接下来编写vo用来接收前端传来的参数,分别为起始页码和当前页还有分页条件,这里的分页条件为petId是个String。

|--------------------------------------------------------------------------------------------------------------------------------------------|
| @Data public class PetPageVO implements Serializable { private String petId; private Integer currPage; private Integer pageSize; } |

然后controller:

|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| /** * 分页查询宠物病例历史列表 * @param petPageVO * @return */ @PostMapping("/listPetExamByPetId") public R listPetExamByPetId(@RequestBody PetPageVO petPageVO){ String pageNo = petPageVO.getCurrPage().toString(); String pageSize = petPageVO.getPageSize().toString(); String petId = petPageVO.getPetId(); Map map = new HashMap(); map.put("page",pageNo); map.put("limit",pageSize); map.put("petId",petId); PageUtils pageUtils = petExamService.queryPage(map); return new R(true,StatusCode.PETEXAMLISTSUCCESS.getCode(),StatusCode.PETEXAMLISTSUCCESS.getMsg(),pageUtils); } |

这里要注意不要用Integer,会提示转换失败报错,我们可以看mybatis-plus底层是怎么做的:

|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| public class Query<T> { public IPage<T> getPage(Map<String, Object> params) { return this.getPage(params, null, false); } public IPage<T> getPage(Map<String, Object> params, String defaultOrderField, boolean isAsc) { //分页参数 long curPage = 1; long limit = 10; if(params.get(Constant.PAGE) != null){ curPage = Long.parseLong((String)params.get(Constant.PAGE)); } if(params.get(Constant.LIMIT) != null){ limit = Long.parseLong((String)params.get(Constant.LIMIT)); } //分页对象 Page<T> page = new Page<>(curPage, limit); //分页参数 params.put(Constant.PAGE, page); //排序字段 //防止SQL注入(因为sidx、order是通过拼接SQL实现排序的,会有SQL注入风险) String orderField = SQLFilter.sqlInject((String)params.get(Constant.ORDER_FIELD)); String order = (String)params.get(Constant.ORDER); //前端字段排序 if(StringUtils.isNotEmpty(orderField) && StringUtils.isNotEmpty(order)){ if(Constant.ASC.equalsIgnoreCase(order)) { return page.addOrder(OrderItem.asc(orderField)); }else { return page.addOrder(OrderItem.desc(orderField)); } } //没有排序字段,则不排序 if(StringUtils.isBlank(defaultOrderField)){ return page; } //默认排序 if(isAsc) { page.addOrder(OrderItem.asc(defaultOrderField)); }else { page.addOrder(OrderItem.desc(defaultOrderField)); } return page; } } |

可以看到这里的params.get(Constant.PAGE)如果不为空则读取参数curPage = Long.parseLong((String)params.get(Constant.PAGE)),这里有个转换,先转换为整形再转换为string。Constant.PAGE 其实就是page。

然后我们分页即可:

|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| @Service("PetExamService") public class PetExamServiceImpl extends ServiceImpl<PetExamDao, PetExamEntity> implements PetExamService { @Override public PageUtils queryPage(Map<String, Object> params) { LambdaQueryWrapper<PetExamEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>(); lambdaQueryWrapper.eq(PetExamEntity::getPetId,params.get("petId")) .select(); IPage<PetExamEntity> page = this.page( new Query<PetExamEntity>().getPage(params), lambdaQueryWrapper ); return new PageUtils(page); } } |

2.根据日期查询

日期的处理一向比较严肃,mybatis-plus没有提供函数处理,所以要使用apply进行编写对应函数。

本案例是接收前端传入的日期数组并查询数据。

controller层:

|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| /** * 根据日期范围查看宠物历史记录 * @param dateArrays * @return */ @PostMapping("/listPetHistoryExamByDateArray/{petId}") public R listPetHistoryExamByDateArray(@PathVariable String petId,@RequestBody String[] dateArrays){ LambdaQueryWrapper<PetExamEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>(); lambdaQueryWrapper.eq(PetExamEntity::getPetId,petId); for (int i = 0; i < dateArrays.length; i++) { lambdaQueryWrapper.apply("TO_DAYS(create_time) = TO_DAYS('"+dateArrays[i]+"')") .or(); } lambdaQueryWrapper.select(); List<PetExamEntity> list = petExamService.list(lambdaQueryWrapper); return new R(true,StatusCode.PETEXAMLISTBYHISTORYARRAYSUCCESS.getCode(),StatusCode.PETEXAMLISTBYHISTORYARRAYSUCCESS.getMsg(),list); } |

注意这里的apply里面传递的实际是sql,不要忘记加or否则是and连接。

相关推荐
揣晓丹4 分钟前
JAVA实战开源项目:校园失物招领系统(Vue+SpringBoot) 附源码
java·开发语言·vue.js·spring boot·开源
于过15 分钟前
Spring注解编程模型
java·后端
北随琛烬入16 分钟前
Spark(10)配置Hadoop集群-集群配置
java·hadoop·spark
顽疲22 分钟前
从零用java实现 小红书 springboot vue uniapp (11)集成AI聊天机器人
java·vue.js·spring boot·ai
霍徵琅29 分钟前
Groovy语言的物联网
开发语言·后端·golang
Yan-英杰38 分钟前
DeepSeek-R1模型现已登录亚马逊云科技
java·大数据·人工智能·科技·机器学习·云计算·deepseek
TDengine (老段)1 小时前
TDengine 中的日志系统
java·大数据·数据库·物联网·时序数据库·tdengine·iotdb
不想学习!!1 小时前
linux之进程控制
java·linux·服务器
独好紫罗兰1 小时前
洛谷题单3-P1420 最长连号-python-流程图重构
开发语言·python·算法
kingmax542120081 小时前
深入解析:使用Python爬取Bilibili视频
开发语言·python·音视频