Commit aed50eb7 authored by 22管33's avatar 22管33

增加refers和includes处理的方法

parent f86bfab5
......@@ -3,7 +3,10 @@ package com.haomostudio.SeekTruthBackend.service.HmUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.base.CaseFormat;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;
......@@ -213,4 +216,93 @@ public class MybatisExampleHelper {
}});
}});
}
/**
*
* @param m java反射类中的方法
* @param superior 实体类对象
* @param className 类名(用于反射时对应的ServiceImpl类)
* @param map 用于存储数据的Map
* @param table 关联表的名字
*/
public static void dealSearchIncludes(Method m,Object superior,Object className,Map<String,Object> map,String table,String packageName){
try {
if (m != null){
//外联表的主键值
Object id = m.invoke(superior);
//根据主键取出整条数据
if (id != null){
WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
//反射类名
Class onwClass = Class.forName(packageName+"."+className+"ServiceImpl");
Object impl = onwClass.newInstance();
//获取Method的对象
String get = "get";
if (className.equals("CrmUser")){
get = "getUserById";
}
Method m1 = MybatisExampleHelper.getMethod(impl, get);
if (m1 != null){
//外联表的整条数据
Object obj2 = m1.invoke(wac.getBean(CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, table)+"Service"),id);
//取出来的数据返回
map.put(table.toString(),obj2);
}
}
}
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
}
public static void dealSearchRefers(Object id,String table,Map<String,List<Object>> map,Object obj,Object className,String packageName){
try {
//根据主键取出整条数据
if (id != null) {
WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
//DivStattisTabService service = (DivStattisTabService)
//反射类名
Class onwClass = Class.forName(packageName+"."+className+"ServiceImpl");
Object impl = onwClass.newInstance();
//获取Method的对象
Method m1 = MybatisExampleHelper.getMethodNumber(impl, "getListWithPagingAndFilter",5);
//filters的字符串拼接
StringBuffer buffer = new StringBuffer();
buffer.append("{");
buffer.append("\""+table+"\"");
buffer.append(":{");
buffer.append("\""+obj.toString()+"\"");
buffer.append(":{");
buffer.append("\"equalTo\": \""+id.toString()+"\"");
buffer.append("} } }");
//执行getListWithPagingAndFilter方法的参数数组
Object[] methodObject = new Object[]{1,1000,"id","asc",buffer.toString()};
if (m1 != null){
//外联表的整条数据
List<Object> obj2 = (List<Object>)m1.invoke(wac.getBean(CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, table)+"Service"),1,1000,"id","asc",buffer.toString());
//取出来的数据返回
map.put(table.toString(),obj2);
}
}
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment