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连接。

相关推荐
zwjapple1 分钟前
typescript里面正则的使用
开发语言·javascript·正则表达式
小五Five3 分钟前
TypeScript项目中Axios的封装
开发语言·前端·javascript
小曲程序3 分钟前
vue3 封装request请求
java·前端·typescript·vue
前端每日三省4 分钟前
面试题-TS(八):什么是装饰器(decorators)?如何在 TypeScript 中使用它们?
开发语言·前端·javascript
凡人的AI工具箱18 分钟前
15分钟学 Go 第 60 天 :综合项目展示 - 构建微服务电商平台(完整示例25000字)
开发语言·后端·微服务·架构·golang
陈王卜20 分钟前
django+boostrap实现发布博客权限控制
java·前端·django
小码的头发丝、21 分钟前
Spring Boot 注解
java·spring boot
java亮小白199726 分钟前
Spring循环依赖如何解决的?
java·后端·spring
飞滕人生TYF32 分钟前
java Queue 详解
java·队列
chnming198733 分钟前
STL关联式容器之map
开发语言·c++