SpringBoot 整合Mybatis Plus 封装分页工具
自定义注解#
import java.lang.annotation.*; /** * @Author:chenyanbin */ @Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface MybatisPlusPageAnnotation { /** * 对应数据库字段名称 * * @return */ String value() default ""; /** * 对应数据库字段描述 * * @return */ String description() default ""; enum CompareRule { eq, //等于 = like, //LIKE '%值%' likeLeft, //LIKE '%值' likeRight, //LIKE '值%' gt, //大于 lt, //小于 ge, //大于等于 le //小于等于 } enum IfRule { isNotBlank, isBlank, isEmpty } /** * 比较规则 * <pre> * LIKE '%值%' * </pre> * * @return */ CompareRule compareRule() default CompareRule.eq; /** * 判断规则 * <pre> * if (StringUtils.isNotBlank(policyNo)) { * * } * </pre> * <pre> * StringUtils.isNotBlank(null) = false * StringUtils.isNotBlank("") = false * StringUtils.isNotBlank(" ") = false * StringUtils.isNotBlank("bob") = true * StringUtils.isNotBlank(" bob ") = true * </pre> * <pre> * StringUtils.isBlank(null) = true * StringUtils.isBlank("") = true * StringUtils.isBlank(" ") = true * StringUtils.isBlank("bob") = false * StringUtils.isBlank(" bob ") = false * </pre> * <pre> * StringUtils.isEmpty(null) = true * StringUtils.isEmpty("") = true * StringUtils.isEmpty(" ") = false * StringUtils.isEmpty("bob") = false * StringUtils.isEmpty(" bob ") = false * </pre> * * @return */ IfRule ifRule() default IfRule.isNotBlank; }
工具类#
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.isoftstone.insurance.lixiang.api.vas.annotation.MybatisPlusPageAnnotation; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import java.lang.reflect.Field; /** * @Author:chenyanbin */ @Slf4j public class MybatisPlusPageUtils { public static void buildQueryWrapper(QueryWrapper queryWrapper, Object data) { try { Field[] fields = data.getClass().getDeclaredFields(); for (Field f : fields) { MybatisPlusPageAnnotation annotation = f.getAnnotation(MybatisPlusPageAnnotation.class); if (annotation == null) { continue; } f.setAccessible(true); String objValue = String.valueOf(f.get(data)); if ("null".equalsIgnoreCase(objValue)) { objValue = ""; } MybatisPlusPageAnnotation.IfRule ifRule = annotation.ifRule(); MybatisPlusPageAnnotation.CompareRule compareRule = annotation.compareRule(); String fieldValue = annotation.value(); switch (ifRule) { case isNotBlank: if (StringUtils.isNotBlank(objValue)) { handlerValues(queryWrapper, annotation, objValue, fieldValue); } break; case isBlank: if (StringUtils.isBlank(objValue)) { handlerValues(queryWrapper, annotation, objValue, fieldValue); } break; case isEmpty: if (StringUtils.isEmpty(objValue)) { handlerValues(queryWrapper, annotation, objValue, fieldValue); } break; default: break; } f.setAccessible(false); } } catch (Exception e) { log.error("MybatisPlusPageUtils buildqueryWrapper queryWrapper={},error={}", queryWrapper, e.getMessage()); } } private static void handlerValues(QueryWrapper queryWrapper, MybatisPlusPageAnnotation annotation, String objValue, String fieldValue) { switch (annotation.compareRule()) { case eq: queryWrapper.eq(fieldValue, objValue); break; case like: queryWrapper.like(fieldValue, objValue); break; case likeLeft: queryWrapper.likeLeft(fieldValue, objValue); break; case likeRight: queryWrapper.likeRight(fieldValue, objValue); break; case gt: queryWrapper.gt(fieldValue, objValue); break; case lt: queryWrapper.lt(fieldValue, objValue); break; case ge: queryWrapper.ge(fieldValue, objValue); break; case le: queryWrapper.le(fieldValue, objValue); break; default: break; } } }
request类#
import lombok.Data; /** * @Author:chenyanbin */ @Data public class OrderPageRequest { @MybatisPlusPageAnnotation(value = "order_code", description = "订单号", compareRule = MybatisPlusPageAnnotation.CompareRule.like) private String orderCode; @MybatisPlusPageAnnotation(value = "order_source", description = "订单来源", compareRule = MybatisPlusPageAnnotation.CompareRule.like) private String orderSource; @MybatisPlusPageAnnotation(value = "order_status", description = "订单状态") private String orderStatus; @MybatisPlusPageAnnotation(value = "policy_no", description = "保单号", compareRule = MybatisPlusPageAnnotation.CompareRule.like) private String policyNo; @MybatisPlusPageAnnotation(value = "product_name", description = "商品名称", compareRule = MybatisPlusPageAnnotation.CompareRule.like) private String productName; @MybatisPlusPageAnnotation(value = "item_name", description = "服务项目名称", compareRule = MybatisPlusPageAnnotation.CompareRule.like) private String itemName; @MybatisPlusPageAnnotation(value = "appoint_name", description = "预约人姓名", compareRule = MybatisPlusPageAnnotation.CompareRule.like) private String appointName; @MybatisPlusPageAnnotation(value = "appoint_certi_code", description = "预约人证件号码") private String appointCertiCode; @MybatisPlusPageAnnotation(value = "use_name", description = "使用人姓名", compareRule = MybatisPlusPageAnnotation.CompareRule.like) private String useName; @MybatisPlusPageAnnotation(value = "use_certi_code", description = "使用人证件号码") private String useCertiCode; @MybatisPlusPageAnnotation(value = "created_date", description = "创建时间-开始", compareRule = MybatisPlusPageAnnotation.CompareRule.ge) private String start; @MybatisPlusPageAnnotation(value = "created_date", description = "创建时间-结束", compareRule = MybatisPlusPageAnnotation.CompareRule.le) private String end; private int size; private int current; }
使用#
QueryWrapper<VasAppointment> wrapper = new QueryWrapper<VasAppointment>(); OrderPageRequest request = new OrderPageRequest(); request.setOrderCode(orderCode); request.setOrderSource(orderSource); request.setOrderStatus(orderStatus); request.setPolicyNo(policyNo); request.setProductName(productName); request.setItemName(itemName); request.setAppointName(appointName); request.setAppointCertiCode(appointCertiCode); request.setUseName(useName); request.setUseCertiCode(useCertiCode); request.setStart(start); request.setEnd(end); request.setSize(pageReq.getSize()); request.setCurrent(pageReq.getCurrent()); MybatisPlusPageUtils.buildQueryWrapper(wrapper, request); Page<VasAppointment> pageInfo = new Page<>(pageReq.getCurrent(), pageReq.getSize()); Page<VasAppointment> sourcePage = appointmentMapper.selectPage(pageInfo, wrapper);
分类:
MyBatis
, Spring Boot
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
2020-06-24 SpringCloud面试题及答案