關于QueryWrapper,實現(xiàn)MybatisPlus多表關聯(lián)查詢方式
QueryWrapper實現(xiàn)MybatisPlus多表關聯(lián)查詢
1.dao層接口使用Select注解寫SQL
重點:@Param("ew") Wrapper參數(shù)是必須,因為${ew.customSqlSegment} 底層其實就是where 條件,所以為了保證Wrapper不為空,service層代碼中的Wrapper至少需要有一個條件:1 = 1
@Override ? ? @Select("select a.code as code , b.name as name , b.barcode as barcode , ?a.ware_code as wareCode , c.name as wareName , a.qty as qty , a.oprice as oprice , a.total as total , " + ? ? ? ? ? ? " a.id as id , a.create_by as createBy , a.create_date as createDate , a.update_by as updateBy , a.update_date as updateDate , a.status as status , a.remarks as remarks ?" + ? ? ? ? ? ? "from sku_stock a , goods b , warehouse c " + ? ? ? ? ? ? "${ew.customSqlSegment} and a.code = b.code and a.ware_code = c.code") ? ? IPage<SkuStock> selectPage(IPage<SkuStock> page, @Param("ew")Wrapper<SkuStock> queryWrapper);
2.service層代碼示例
service父類封裝的findPage方法:
/** ? ? ?* 封裝findPage ? ? ?* @param entity ? ? ?* @param search ?Map中的key:";"為保留關鍵字,拆分數(shù)組,僅支持最大長度2的數(shù)組, ? ? ?* ? ? ? ? ? ? ? ?下標0:QueryWrapper查詢條件中的列名(支持多表關聯(lián)查詢的表別名 + 列名方式,需要dao層接口支持) ? ? ?* ? ? ? ? ? ? ? ?下標1: QueryWrapper中不同的查詢條件,eq:等于,ge:大于等..... todo:請自行完善Mybatis eq、ne、gt、lt、ge、le等 ? ? ?* ? ? ? ? ? ? ? ?Map中的value:QueryWrapper需要查詢的值 ? ? ?* @param args ?QueryWrapper中order by 排序數(shù)組 ? ? ?* @return ? ? ?*/ ? ? public IPage<T> findPage(T entity , Map<String , Object> search , String... args){ ? ? ? ? long current = 1L; ? ? ? ? long size = 10L; ? ? ? ? if (EmptyUtil.isNoEmpty(ReflexUtil.getFieldValue(entity , "page")) && (long) ReflexUtil.getFieldValue(entity , "page") != 0){ ? ? ? ? ? ? current = (long) ReflexUtil.getFieldValue(entity , "page"); ? ? ? ? } ? ? ? ? if (EmptyUtil.isNoEmpty(ReflexUtil.getFieldValue(entity , "limit")) && (long) ReflexUtil.getFieldValue(entity , "limit") != 0){ ? ? ? ? ? ? size = (long) ReflexUtil.getFieldValue(entity , "limit"); ? ? ? ? } ? ? ? ? ? QueryWrapper<T> queryWrapper; ? ? ? ? if (EmptyUtil.isNoEmpty(search)){ ? ? ? ? ? ? queryWrapper = new QueryWrapper<>(); ? ? ? ? ? ? for (Map.Entry<String , Object> entry:search.entrySet() ? ? ? ? ? ? ? ? ?) { ? ? ? ? ? ? ? ? String[] key = entry.getKey().split(";"); ? ? ? ? ? ? ? ? if (key.length > 1){ ? ? ? ? ? ? ? ? ? ? if (key[1].equals("eq")){ ? ? ? ? ? ? ? ? ? ? ? ? queryWrapper.eq(key[0] , entry.getValue()); ? ? ? ? ? ? ? ? ? ? }else if (key[1].equals("ge")){ ? ? ? ? ? ? ? ? ? ? ? ? queryWrapper.ge(key[0] , entry.getValue()); ? ? ? ? ? ? ? ? ? ? }else if (key[1].equals("lt")){ ? ? ? ? ? ? ? ? ? ? ? ? queryWrapper.lt(key[0] , entry.getValue()); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? }else { ? ? ? ? ? ? ? ? ? ? queryWrapper.like(entry.getKey() , entry.getValue()); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? }else { ? ? ? ? ? ? queryWrapper = new QueryWrapper<>(entity); ? ? ? ? } ? ? ? ? queryWrapper.orderByAsc(args); ? ? ? ? return super.page(new Page<T>(current , size) , queryWrapper); ? ? }
service實現(xiàn)類方法:
public IPage<SkuStock> findPage(SkuStock entity, String... args) { ? ? ? ? Map<String , Object> search = null; ? ? ? ? search = new HashedMap(); ? ? ? ? search.put("1;eq" , "1"); ? ? ? ? if (EmptyUtil.isNoEmpty(entity.getCode()) ? ? ? ? ? ? ? ? || EmptyUtil.isNoEmpty(entity.getWareCode()) ? ? ? ? ){ ? ? ? ? ? ? if (EmptyUtil.isNoEmpty(entity.getCode())){ ? ? ? ? ? ? ? ? search.put("code" , entity.getCode()); ? ? ? ? ? ? } ? ? ? ? ? ? if (EmptyUtil.isNoEmpty(entity.getWareCode())){ ? ? ? ? ? ? ? ? search.put("ware_code" , entity.getWareCode()); ? ? ? ? ? ? } ? ? ? ? }else { ? ? ? ? ? ? long limit = entity.getLimit(); ? ? ? ? ? ? long page = entity.getPage(); ? ? ? ? ? ? entity = new SkuStock(); ? ? ? ? ? ? entity.setLimit(limit); ? ? ? ? ? ? entity.setPage(page); ? ? ? ? } ? ? ? ? return super.findPage(entity , search , args); ? ? }
3.反射工具類
package org.bluedream.comm.utils; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * @ClassName ReflexUtil * @Description TODO * @Author foxsand * @Data 2021-06-09 15:17 * @Version */ public class ReflexUtil { /** * 返回 entity 對象的所有屬性,包含父類 * @param obj * @return */ public static List<Field> getObjectFields(Object obj){ Class clazz = obj.getClass(); List<Field> fieldList = new ArrayList<>() ; while (clazz != null) {//當父類為null的時候說明到達了最上層的父類(Object類). fieldList.addAll(Arrays.asList(clazz .getDeclaredFields())); clazz = clazz.getSuperclass(); //得到父類,然后賦給自己 } return fieldList; } public static List<Field> getObjectFields(Class<?> clazz){ List<Field> fieldList = new ArrayList<>() ; while (clazz != null){ fieldList.addAll(Arrays.asList(clazz .getDeclaredFields())); clazz = clazz.getSuperclass(); //得到父類,然后賦給自己 } return fieldList; } /** * 判斷 Class entity 是否存在名稱為 fieldName 的屬性 * @param fieldName * @param entity * @return */ public static Boolean isField(String fieldName , Object entity){ List<Field> fieldList = getObjectFields(entity); for (Field f1:fieldList ) { if (fieldName.equals(f1.getName())) return true; } return false; } /** * 返回 entity 對象中的所有方法,包含父類 * @param entity * @return */ public static List<Method> getObjectMethods(Object entity){ Class<?> clazz = entity.getClass(); List<Method> methods = new ArrayList<>(); while (clazz != null && clazz != Object.class) {//當父類為null的時候說明到達了最上層的父類(Object類). methods.addAll(Arrays.asList(clazz .getDeclaredMethods())); clazz = clazz.getSuperclass(); //得到父類,然后賦給自己 } return methods; } public static List<Method> getObjectMethods(Class<?> clazz){ List<Method> methods = new ArrayList<>(); while (clazz != null && clazz != Object.class) {//當父類為null的時候說明到達了最上層的父類(Object類). methods.addAll(Arrays.asList(clazz .getDeclaredMethods())); clazz = clazz.getSuperclass(); //得到父類,然后賦給自己 } return methods; } /** * 判斷 Class entity 是否存在名稱為 methodName 的方法 * @param methodName * @param entity * @return */ public static Boolean isMethod(String methodName , Object entity){ List<Method> methods = getObjectMethods(entity); for (Method m1:methods ) { if (methodName.equals(m1.getName())) return true; } return false; } /** * 循環(huán)向上轉(zhuǎn)型, 獲取對象的 DeclaredMethod * @param obj * @param methodName * @param parameterTypes 方法參數(shù)類型 * @return */ public static Method getDeclaredMethod(Object obj , String methodName , Class<?>...parameterTypes) { for (Class<?> clazz = obj.getClass(); clazz != Object.class && clazz != null; clazz = clazz.getSuperclass()) { try { return clazz.getDeclaredMethod(methodName, parameterTypes); } catch (Exception e) { // 這里甚么都不要做!并且這里的異常必須這樣寫,不能拋出去。 // 如果這里的異常打印或者往外拋,則就不會執(zhí)行clazz=clazz.getSuperclass(),最后就不會進入到父類中了 } } return null; } public static Object invoke(Object object, String methodName, Class<?>[] parameterTypes, Object[] parameters){ Method method = getDeclaredMethod(object, methodName, parameterTypes); try { if (method != null){ method.setAccessible(true); // 調(diào)用object 的 method 所代表的方法,其方法的參數(shù)是 parameters return method.invoke(object, parameters); } }catch (Exception e1){ e1.printStackTrace(); } return null; } /** * 循環(huán)向上轉(zhuǎn)型, 獲取對象的 DeclaredField * * @param object * : 子類對象 * @param fieldName * : 父類中的屬性名 * @return 父類中的屬性對象 */ public static Field getDeclaredField(Object object, String fieldName) { Field field = null; Class<?> clazz = object.getClass(); for (; clazz != Object.class && clazz != null; clazz = clazz.getSuperclass()) { try { field = clazz.getDeclaredField(fieldName); return field; } catch (Exception e) { // 這里甚么都不要做!并且這里的異常必須這樣寫,不能拋出去。 // 如果這里的異常打印或者往外拋,則就不會執(zhí)行clazz = clazz.getSuperclass(),最后就不會進入到父類中了 } } return null; } /** * 直接設置對象屬性值, 忽略 private/protected 修飾符, 也不經(jīng)過 setter * * @param object * : 子類對象 * @param fieldName * : 父類中的屬性名 * @param value * : 將要設置的值 */ public static void setFieldValue(Object object, String fieldName, Object value) { // 根據(jù) 對象和屬性名通過反射 調(diào)用上面的方法獲取 Field對象 Field field = getDeclaredField(object, fieldName); if (field != null){ // 抑制Java對其的檢查 field.setAccessible(true); try { // 將 object 中 field 所代表的值 設置為 value field.set(object, value); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } } /** * 直接讀取對象的屬性值, 忽略 private/protected 修飾符, 也不經(jīng)過 getter * * @param object * : 子類對象 * @param fieldName * : 父類中的屬性名 * @return : 父類中的屬性值 */ public static Object getFieldValue(Object object, String fieldName) { // 根據(jù) 對象和屬性名通過反射 調(diào)用上面的方法獲取 Field對象 Field field = getDeclaredField(object, fieldName); if (field != null){ // 抑制Java對其的檢查 field.setAccessible(true); try { // 獲取 object 中 field 所代表的屬性值 return field.get(object); } catch (Exception e) { e.printStackTrace(); } } return null; } }
4.判空工具類
package org.bluedream.comm.utils;? import java.util.Collection; import java.util.Map;? public class EmptyUtil { ? ? //Suppress default constructor for noninstantiability ? ? private EmptyUtil(){ ? ? ? ? throw new AssertionError(); ? ? } ? ? ? public static boolean isEmpty(Object object){ ? ? ? ? if (object == null){ ? ? ? ? ? ? return true; ? ? ? ? } ? ? ? ? if (object instanceof int[]){ ? ? ? ? ? ? return ((int[]) object).length == 0; ? ? ? ? } ? ? ? ? if (object instanceof double[]){ ? ? ? ? ? ? return ((double[]) object).length == 0; ? ? ? ? } ? ? ? ? if (object instanceof long[]){ ? ? ? ? ? ? return ((long[]) object).length == 0; ? ? ? ? } ? ? ? ? if (object instanceof byte[]){ ? ? ? ? ? ? return ((byte[]) object).length == 0; ? ? ? ? } ? ? ? ? if (object instanceof short[]){ ? ? ? ? ? ? return ((short[]) object).length == 0; ? ? ? ? } ? ? ? ? if (object instanceof float[]){ ? ? ? ? ? ? return ((float[]) object).length == 0; ? ? ? ? } ? ? ? ? if (object instanceof char[]){ ? ? ? ? ? ? return ((char[]) object).length == 0; ? ? ? ? } ? ? ? ? if (object instanceof Object[]){ ? ? ? ? ? ? return ((Object[]) object).length == 0; ? ? ? ? } ? ? ? ? if (object instanceof CharSequence) { ? ? ? ? ? ? return ((CharSequence) object).length() == 0; ? ? ? ? } ? ? ? ? if (object instanceof Collection ){ ? ? ? ? ? ? return ((Collection) object).isEmpty(); ? ? ? ? } ? ? ? ? if (object instanceof Map){ ? ? ? ? ? ? return ((Map) object).isEmpty(); ? ? ? ? } ? ? ? ? return false; ? ? } ? ? ? public static boolean isNoEmpty(Object object){ ? ? ? ? return !isEmpty(object); ? ? }? }
MybatisPlus QueryWrapper簡單用法
查詢方式 | 說明 |
setSqlSelect | 設置 SELECT 查詢字段 |
where | WHERE 語句,拼接 +?WHERE 條件 |
and | AND 語句,拼接 +?AND 字段=值 |
andNew | AND 語句,拼接 +?AND (字段=值) |
or | OR 語句,拼接 +?OR 字段=值 |
orNew | OR 語句,拼接 +?OR (字段=值) |
eq | 等于= |
allEq | 基于 map 內(nèi)容等于= |
ne | 不等于<> |
gt | 大于> |
ge | 大于等于>= |
lt | 小于< |
le | 小于等于<= |
like | 模糊查詢 LIKE |
notLike | 模糊查詢 NOT LIKE |
in | IN 查詢 |
notIn | NOT IN 查詢 |
isNull | NULL 值查詢 |
isNotNull | IS NOT NULL |
groupBy | 分組 GROUP BY |
having | HAVING 關鍵詞 |
orderBy | 排序 ORDER BY |
orderAsc | ASC 排序 ORDER BY |
orderDesc | DESC 排序 ORDER BY |
exists | EXISTS 條件語句 |
notExists | NOT EXISTS 條件語句 |
between | BETWEEN 條件語句 |
notBetween | NOT BETWEEN 條件語句 |
addFilter | 自由拼接 SQL |
last | 拼接在最后,例如:last(“LIMIT 1”) |
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Java?SpringBoot集成文件之如何使用POI導出Word文檔
這篇文章主要介紹了Java?SpringBoot集成文件之如何使用POI導出Word文檔,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的朋友可以參考一下2022-08-08SpringBoot使用 druid 連接池來優(yōu)化分頁語句
這篇文章主要介紹了SpringBoot使用 druid 連接池來優(yōu)化分頁語句,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-11-11SpringBoot整合Mybatis-plus的具體過程使用
這篇文章主要介紹了SpringBoot?整合mybatis+mybatis-plus的步驟,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06