基于QueryWrapper,实现MybatisPlus多表关联查询 /2023/知乎推荐

您所在的位置:网站首页 mybatisplus联表wrapper 基于QueryWrapper,实现MybatisPlus多表关联查询 /2023/知乎推荐

基于QueryWrapper,实现MybatisPlus多表关联查询 /2023/知乎推荐

2023-03-13 20:02| 来源: 网络整理| 查看: 265

strong>版权声明:请勿用于任何商业用途的文章转载,转载请说明出处!strong>

strong>1.dao层接口使用Select注解写SQL:strong>

重点:strong>@Param(“ew”) Wrapper参数是必须,因为${ew.customSqlSegment} 底层其实就是where 条件,所以为了保证Wrapper不为空,service层代码中的Wrapper至少需要有一个条件:1 = 1strong>

@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'>status as status'>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 selectPage(IPage page, @Param("ew")Wrapper queryWrapper);

strong>2.service层代码示例:strong>

strong>service父类封装的findPage方法strong>:

/** * 封装findPage * @param entity * @param search Map中的key:";"为保留关键字,拆分数组,仅支持最大长度2的数组, * 下标0:QueryWrapper查询条件中的列名(支持多表关联查询的表别名 + 列名方式,需要dao层接口支持) * 下标1: QueryWrapper中不同的查询条件,eq:等于,ge:大于等..... todo:请自行完善Mybatis eq、ne、gt、lt、ge、le等 * Map中的value:QueryWrapper需要查询的值 * @param args QueryWrapper中order by'>order by 排序数组 * @return */ public IPage findPage(T entity , Map 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 queryWrapper; if (EmptyUtil.isNoEmpty(search)){ queryWrapper = new QueryWrapper(); for (Map.Entry entry:search.entrySet() ) { String[] key = entry.getKey().split(";"); if (key.length > 1){ if (key[1].equals'>equals("eq")){ queryWrapper.eq(key[0] , entry.getValue()); }else if (key[1].equals'>equals("ge")){ queryWrapper.ge(key[0] , entry.getValue()); }else if (key[1].equals'>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(current , size) , queryWrapper); }

strong>service实现方法'>类方法:strong>

public IPage findPage(SkuStock entity, String... args) { Map 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); }

strong>3.反射工具类:strong>

package'>package org.bluedream.comm.utils; port'>import java.lang.reflect.Field; port'>import java.lang.reflect.Method; port'>import java.util.ArrayList; port'>import java.util.Arrays; port'>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 getObjectFields(Object obj){ Class clazz = obj.Class'>getClass(); List fieldList = new ArrayList() ; while (clazz != null) {//当父类为null的时候说明到达了最上层的父类(Object类). fieldList.addAll(Arrays.asList(clazz .getDeclaredFields())); clazz = clazz.getSuperclass(); //得到父类,然后赋给自己 } return fieldList; } public static List getObjectFields(Class clazz){ List 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 fieldList = getObjectFields(entity); for (Field f1:fieldList ) { if (fieldName.equals'>equals(f1.getName())) return true; } return false; } /** * 返回 entity 对象中的所有方法,包含父类 * @param entity * @return */ public static List getObjectMethods(Object entity){ Class clazz = entity.Class'>getClass(); List methods'>methods = new ArrayList(); while (clazz != null && clazz != Object.class) {//当父类为null的时候说明到达了最上层的父类(Object类). methods'>methods.addAll(Arrays.asList(clazz .getDeclaredMethods())); clazz = clazz.getSuperclass(); //得到父类,然后赋给自己 } return methods'>methods; } public static List getObjectMethods(Class clazz){ List methods'>methods = new ArrayList(); while (clazz != null && clazz != Object.class) {//当父类为null的时候说明到达了最上层的父类(Object类). methods'>methods.addAll(Arrays.asList(clazz .getDeclaredMethods())); clazz = clazz.getSuperclass(); //得到父类,然后赋给自己 } return methods'>methods; } /** * 判断 Class entity 是否存在名称为 methodName 的方法 * @param methodName * @param entity * @return */ public static Boolean isMethod(String methodName , Object entity){ List methods'>methods = getObjectMethods(entity); for (Method m1:methods'>methods ) { if (methodName.equals'>equals(m1.getName())) return true; } return false; } /** * 循环转型'>向上转型, 获取对象的 DeclaredMethod * @param obj * @param methodName * @param parameterTypes 方法参数类型 * @return */ public static Method getDeclaredMethod(Object obj , String methodName , Class...parameterTypes) { for (Class clazz = obj.Class'>getClass(); clazz != Object.class && clazz != null; clazz = clazz.getSuperclass()) { try { return clazz.getDeclaredMethod(methodName, parameterTypes); } catch (Exception'>Exception e) { // 这里甚么都不要做!并且这里的异常必须这样写,不能抛出去。 // 如果这里的异常打印或者往外抛,则就不会执行clazz=clazz.getSuperclass(),最后就不会进入到父类中了 } } return null; } public static Object invoke(Object object, String methodName, Class[] parameterTypes, Object[] parameters'>parameters){ Method method = getDeclaredMethod(object, methodName, parameterTypes); try { if (method != null){ method.setAccessible(true); // 调用object 的 method 所代表的方法,其方法的参数是 parameters'>parameters return method.invoke(object, parameters'>parameters); } }catch (Exception'>Exception e1){ e1.printStackTrace(); } return null; } /** * 循环转型'>向上转型, 获取对象的 DeclaredField * * @param object * : 子类对象 * @param fieldName * : 父类中的属性名 * @return 父类中的属性对象 */ public static Field getDeclaredField(Object object, String fieldName) { Field field = null; Class clazz = object.Class'>getClass(); for (; clazz != Object.class && clazz != null; clazz = clazz.getSuperclass()) { try { field = clazz.getDeclaredField(fieldName); return field; } catch (Exception'>Exception e) { // 这里甚么都不要做!并且这里的异常必须这样写,不能抛出去。 // 如果这里的异常打印或者往外抛,则就不会执行clazz = clazz.getSuperclass(),最后就不会进入到父类中了 } } return null; } /** * 直接设置对象属性值'>属性值, 忽略 private/protected 修饰符, 也不经过 setter * * @param object * : 子类对象 * @param fieldName * : 父类中的属性名 * @param value * : 将要设置的值 */ public static void setFieldValue(Object object, String fieldName, Object value) { // 根据 对象和属性名通过反射 调用上面的方法获取 Field对象 Field field = getDeclaredField(object, fieldName); if (field != null){ // 抑制Java对其的检查 field.setAccessible(true); try { // 将 object 中 field 所代表的值 设置为 value field.set(object, value); } catch (IllegalArgumentException'>Exception e) { e.printStackTrace(); } catch (IllegalAccessException'>Exception e) { e.printStackTrace(); } } } /** * 直接读取对象的属性值'>属性值, 忽略 private/protected 修饰符, 也不经过 getter * * @param object * : 子类对象 * @param fieldName * : 父类中的属性名 * @return : 父类中的属性值'>属性值 */ public static Object getFieldValue(Object object, String fieldName) { // 根据 对象和属性名通过反射 调用上面的方法获取 Field对象 Field field = getDeclaredField(object, fieldName); if (field != null){ // 抑制Java对其的检查 field.setAccessible(true); try { // 获取 object 中 field 所代表的属性值'>属性值 return field.get(object); } catch (Exception'>Exception e) { e.printStackTrace(); } } return null; } }

strong>4.判空工具类:strong>

package'>package org.bluedream.comm.utils; port'>import java.util.Collection; port'>import java.util.Map; public class EmptyUtil { //Suppress default structor'>constructor for noninstantiability private EmptyUtil(){ throw new AssertionError(); } public static boolean isEmpty(Object object){ if (object == null){ return true; } if (object instanceof'>instanceof int[]){ return ((int[]) object).length == 0; } if (object instanceof'>instanceof double[]){ return ((double[]) object).length == 0; } if (object instanceof'>instanceof long[]){ return ((long[]) object).length == 0; } if (object instanceof'>instanceof byte[]){ return ((byte[]) object).length == 0; } if (object instanceof'>instanceof short[]){ return ((short[]) object).length == 0; } if (object instanceof'>instanceof float[]){ return ((float[]) object).length == 0; } if (object instanceof'>instanceof char[]){ return ((char[]) object).length == 0; } if (object instanceof'>instanceof Object[]){ return ((Object[]) object).length == 0; } if (object instanceof'>instanceof CharSequence) { return ((CharSequence) object).length() == 0; } if (object instanceof'>instanceof Collection ){ return ((Collection) object).isEmpty(); } if (object instanceof'>instanceof Map){ return ((Map) object).isEmpty(); } return false; } public static boolean isNoEmpty(Object object){ return !isEmpty(object); } }


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3