今天小編給大家分享一下MyBatisPlus查詢對(duì)象怎么轉(zhuǎn)QueryWrapper工具類的相關(guān)知識(shí)點(diǎn),內(nèi)容詳細(xì),邏輯清晰,相信大部分人都還太了解這方面的知識(shí),所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。
成都創(chuàng)新互聯(lián)公司是一家專注于網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站制作與策劃設(shè)計(jì),太倉網(wǎng)站建設(shè)哪家好?成都創(chuàng)新互聯(lián)公司做網(wǎng)站,專注于網(wǎng)站建設(shè)十載,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:太倉等地區(qū)。太倉做網(wǎng)站價(jià)格咨詢:18982081108
在使用MyBatisPlus技術(shù)實(shí)際項(xiàng)目中,尤其是后臺(tái)管理系統(tǒng)之類的項(xiàng)目中,經(jīng)常會(huì)出現(xiàn)大量的需要將查詢對(duì)象轉(zhuǎn)換成QueryWrapper的應(yīng)用場(chǎng)景,這時(shí)候就需要編寫出現(xiàn)大量的轉(zhuǎn)換代碼,比如:
@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; /** * 外鍵:角色編號(hào) */ private Long roleId; /** * 狀態(tài):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) { 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); }
上面大量的轉(zhuǎn)換代碼,不僅寫起來煩瑣,而且也不優(yōu)雅。此時(shí)可以采用注解+反射技術(shù)編寫一個(gè)工具類:將Query對(duì)象轉(zhuǎn)換成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{ /** * 下限 */ private T start; /** * 上限 */ private T end; }
public class QueryWrapperUtil{ /** * 將查詢對(duì)象轉(zhuǎn)換成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類對(duì)應(yīng)的表字段名 final String columnName = StringUtil.hump2underscore(fieldName); //獲取Query類屬性上的注解類型的字節(jié)碼 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關(guān)系 * @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查詢對(duì)象怎么轉(zhuǎn)QueryWrapper工具類”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會(huì)為大家更新不同的知識(shí),如果還想學(xué)習(xí)更多的知識(shí),請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。