MyBatisPlus查询对象怎么转QueryWrapper工具类
今天小编给大家分享一下MyBatisPlus查询对象怎么转QueryWrapper工具类的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。
成都创新互联公司是一家专注于网站设计制作、成都网站制作与策划设计,太仓网站建设哪家好?成都创新互联公司做网站,专注于网站建设十载,网设计领域的专业建站公司;建站业务涵盖:太仓等地区。太仓做网站价格咨询:18982081108
技术背景
在使用MyBatisPlus技术实际项目中,尤其是后台管理系统之类的项目中,经常会出现大量的需要将查询对象转换成QueryWrapper的应用场景,这时候就需要编写出现大量的转换代码,比如:
待转换的查询实体类
@Getter @Setter @ToString(callSuper = true) public class MemberQuery extends BaseQuery { /** * 昵称 */ private String nickname; /** * 性别:1女 2男 3未知 */ private Integer gender; /** * 生日:下限 */ private LocalDate start; /** * 生日:上限 */ private LocalDate end; /** * 电话 */ private String tel; /** * 外键:角色编号 */ private Long roleId; /** * 状态:1未激活 2激活 */ private Integer state; @Builder public MemberQuery(Integer pageNum, Integer pageSize, String nickname, Integer gender, LocalDate start, LocalDate end, String tel, Long roleId, Integer state) { super(pageNum, pageSize); this.nickname = nickname; this.gender = gender; this.start = start; this.end = end; this.tel = tel; this.roleId = roleId; this.state = state; } }
上面查询类引用的BaseQuery封装了分页信息,具体代码如下:
@Setter @ToString @AllArgsConstructor public class BaseQuery { /** * 页码 */ private Integer pageNum; /** * 页面大小 */ private Integer pageSize; public Integer getPageNum() { return pageNum == null ? 1 : pageNum; } public Integer getPageSize() { return pageSize == null ? GlobalConst.PAGE_SIZE : pageSize; } }
在Controller中进行转换
@PostMapping("/list") public ResultBean list(@RequestBody MemberQuery memberQuery) { IPagepage = new Page<>(memberQuery.getPageNum(), memberQuery.getPageSize()); LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); if (memberQuery.getNickname() != null) { queryWrapper.like(Member::getNickname, memberQuery.getNickname()); } if (memberQuery.getTel() != null) { queryWrapper.like(Member::getTel, memberQuery.getTel()); } if (memberQuery.getGender() != null) { queryWrapper.eq(Member::getGender, memberQuery.getGender()); } if (memberQuery.getRoleId() != null) { queryWrapper.eq(Member::getRoleId, memberQuery.getRoleId()); } if (memberQuery.getState() != null) { queryWrapper.eq(Member::getState, memberQuery.getState()); } if (memberQuery.getStart() != null) { queryWrapper.ge(Member::getBirth,memberQuery.getStart()); } if(memberQuery.getEnd() != null){ queryWrapper.le(Member::getBirth,memberQuery.getEnd()); } memberService.page(page, queryWrapper); PageBean pageBean = PageBean.init(page); return ResultBeanUtil.success().buildData("pageBean",pageBean); }
上面大量的转换代码,不仅写起来烦琐,而且也不优雅。此时可以采用注解+反射技术编写一个工具类:将Query对象转换成Wrapper,具体代码如下:
优化方案
表示关系运算的接口
public interface Logic { String and = "AND"; String or = "OR"; }
表示逻辑运算的枚举
Between
@Target({ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Between { String logic() default Logic.and; }
EQ
@Target({ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface EQ { String logic() default Logic.and; String value() default ""; }
IN
@Target({ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface IN { String logic() default Logic.and; String value() default ""; }
Like
@Target({ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Like { String logic() default "AND"; String value() default ""; }
NE
@Target({ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface NE { String logic() default Logic.and; String value() default ""; }
封装区间的Bean
@Getter @Setter @NoArgsConstructor @AllArgsConstructor public class RangeBean{ /** * 下限 */ private T start; /** * 上限 */ private T end; }
具体转换的工具类
public class QueryWrapperUtil{ /** * 将查询对象转换成MyBatisPlus中的QueryWrapper * @param query * @param * @return */ public static QueryWrapper build(Object query) { QueryWrapper queryWrapper = new QueryWrapper<>(); //获取Query类中都有哪些属性 final Field[] fields = query.getClass().getDeclaredFields(); for (Field field : fields) { field.setAccessible(true); java.lang.Object fieldValue = null; try { fieldValue = field.get(query); } catch (IllegalAccessException e) { e.printStackTrace(); } if (fieldValue != null) { //获取Query类属性上的注解 final Annotation[] annotations = field.getAnnotations(); for (Annotation annotation : annotations) { //获取Query类属性的名称 final String fieldName = field.getName(); //获取Query类对应的表字段名 final String columnName = StringUtil.hump2underscore(fieldName); //获取Query类属性上的注解类型的字节码 final Class extends Annotation> annotationType = annotation.annotationType(); if (EQ.class == annotationType) { EQ eq = (EQ) annotation; fun(eq.logic(), queryWrapper); queryWrapper.eq(columnName, fieldValue); } else if (Like.class == annotationType) { Like like = (Like) annotation; fun(like.logic(), queryWrapper); queryWrapper.like(columnName, fieldValue); } else if (IN.class == annotationType) { IN in = (IN) annotation; fun(in.logic(), queryWrapper); queryWrapper.in(columnName, fieldValue); } else if (NE.class == annotationType) { NE ne = (NE) annotation; fun(ne.logic(), queryWrapper); queryWrapper.ne(columnName, fieldValue); } else if (Between.class == annotationType) { Between between = (Between) annotation; fun(between.logic(), queryWrapper); RangeBean rangeBean = (RangeBean) fieldValue; queryWrapper.between(columnName, rangeBean.getStart(), rangeBean.getEnd()); } } } } return queryWrapper; } /** * 添加or关系 * @param logic * @param queryWrapper * @param */ private static void fun(String logic, QueryWrapper queryWrapper) { if ("or".equalsIgnoreCase(logic)) { queryWrapper.or(); } } }
优化方案:工具类应用
@PostMapping("/list") public ResultBean list(@RequestBody MemberQuery memberQuery) { IPagepage = new Page<>(memberQuery.getPageNum(), memberQuery.getPageSize()); QueryWrapper queryWrapper = QueryWrapperUtil.build(memberQuery); memberService.page(page, queryWrapper); PageBean pageBean = PageBean.init(page); return ResultBeanUtil.success().addData("pageBean",pageBean); }
以上就是“MyBatisPlus查询对象怎么转QueryWrapper工具类”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注创新互联行业资讯频道。
文章题目:MyBatisPlus查询对象怎么转QueryWrapper工具类
URL标题:http://azwzsj.com/article/picpjd.html