您好,登錄后才能下訂單哦!
今天小編給大家分享一下MyBatisPlus查詢對象怎么轉QueryWrapper工具類的相關知識點,內容詳細,邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。
在使用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; } }
@PostMapping("/list") public ResultBean list(@RequestBody MemberQuery memberQuery) { IPage<Member> page = new Page<>(memberQuery.getPageNum(), memberQuery.getPageSize()); LambdaQueryWrapper<Member> 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<Member> 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 ""; }
@Getter @Setter @NoArgsConstructor @AllArgsConstructor public class RangeBean<T> { /** * 下限 */ private T start; /** * 上限 */ private T end; }
public class QueryWrapperUtil<T> { /** * 將查詢對象轉換成MyBatisPlus中的QueryWrapper * @param query * @param <T> * @return */ public static <T> QueryWrapper<T> build(Object query) { QueryWrapper<T> 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 <T> */ private static <T> void fun(String logic, QueryWrapper<T> queryWrapper) { if ("or".equalsIgnoreCase(logic)) { queryWrapper.or(); } } }
@PostMapping("/list") public ResultBean list(@RequestBody MemberQuery memberQuery) { IPage<Member> page = new Page<>(memberQuery.getPageNum(), memberQuery.getPageSize()); QueryWrapper<Member> queryWrapper = QueryWrapperUtil.build(memberQuery); memberService.page(page, queryWrapper); PageBean<Member> pageBean = PageBean.init(page); return ResultBeanUtil.success().addData("pageBean",pageBean); }
以上就是“MyBatisPlus查詢對象怎么轉QueryWrapper工具類”這篇文章的所有內容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會為大家更新不同的知識,如果還想學習更多的知識,請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。