提交 d8e5045b 编写于 作者: xignzi's avatar xignzi

兼容service.getById()方法返回dto对象为dto的代理对象,然后调用实体行为异常

兼容批量行为处理
上级 571d4ed0
...@@ -13,14 +13,16 @@ import javax.annotation.PostConstruct; ...@@ -13,14 +13,16 @@ import javax.annotation.PostConstruct;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType; import java.lang.reflect.ParameterizedType;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
public abstract class BaseServiceImpl<T extends BaseData, F extends BaseFilter> implements IBaseService<T, F> { public abstract class BaseServiceImpl<T extends BaseData, F extends BaseFilter> implements IBaseService<T, F> {
private IBootService runtimeService; private IBootService runtimeService;
@Override @Override
public IBootService getRuntimeService(){ public IBootService getRuntimeService() {
return runtimeService; return runtimeService;
} }
...@@ -30,27 +32,27 @@ public abstract class BaseServiceImpl<T extends BaseData, F extends BaseFilter> ...@@ -30,27 +32,27 @@ public abstract class BaseServiceImpl<T extends BaseData, F extends BaseFilter>
@PostConstruct @PostConstruct
protected void postConstruct() throws Exception { protected void postConstruct() throws Exception {
Class serviceClass=this.getClass(); Class serviceClass = this.getClass();
while (serviceClass!=null) { while (serviceClass != null) {
if(!ObjectUtils.isEmpty(serviceClass.getInterfaces())) { if (!ObjectUtils.isEmpty(serviceClass.getInterfaces())) {
Class interfaceClass=serviceClass.getInterfaces()[0]; Class interfaceClass = serviceClass.getInterfaces()[0];
if(!ObjectUtils.isEmpty(interfaceClass.getInterfaces())) { if (!ObjectUtils.isEmpty(interfaceClass.getInterfaces())) {
if(interfaceClass.getInterfaces()[0].isAssignableFrom(IBaseService.class) if (interfaceClass.getInterfaces()[0].isAssignableFrom(IBaseService.class)
||interfaceClass.getInterfaces()[0].isAssignableFrom(IMPService.class) || interfaceClass.getInterfaces()[0].isAssignableFrom(IMPService.class)
||interfaceClass.getInterfaces()[0].isAssignableFrom(IServiceApiService.class)) { || interfaceClass.getInterfaces()[0].isAssignableFrom(IServiceApiService.class)) {
serviceClass=interfaceClass; serviceClass = interfaceClass;
break; break;
} }
} }
} }
serviceClass = serviceClass.getSuperclass(); serviceClass = serviceClass.getSuperclass();
} }
if(!ObjectUtils.isEmpty(serviceClass.getGenericInterfaces())) { if (!ObjectUtils.isEmpty(serviceClass.getGenericInterfaces())) {
ParameterizedType ptype = (ParameterizedType) serviceClass.getGenericInterfaces()[0]; ParameterizedType ptype = (ParameterizedType) serviceClass.getGenericInterfaces()[0];
tClass = (Class) ptype.getActualTypeArguments()[0]; tClass = (Class) ptype.getActualTypeArguments()[0];
fClass = (Class) ptype.getActualTypeArguments()[1]; fClass = (Class) ptype.getActualTypeArguments()[1];
} }
runtimeService = new BootServiceImpl<T,F>(this); runtimeService = new BootServiceImpl<T, F>(this);
} }
...@@ -67,24 +69,41 @@ public abstract class BaseServiceImpl<T extends BaseData, F extends BaseFilter> ...@@ -67,24 +69,41 @@ public abstract class BaseServiceImpl<T extends BaseData, F extends BaseFilter>
private Object lockMethods = new Object(); private Object lockMethods = new Object();
private Map<String, Method> serviceMethods=new HashMap<>(); private Map<String, Method> serviceMethods = new HashMap<>();
public Method getServiceMethod(String name,Class clazz) { public Method getServiceMethod(String name, Class clazz) {
Method method = null; Method method = null;
synchronized (lockMethods) synchronized (lockMethods) {
{ String tag = name;
String tag=name; if (clazz.isArray())
if(clazz.isArray()) tag = tag + "List";
tag=tag+"List"; if (serviceMethods.containsKey(tag))
if(serviceMethods.containsKey(tag))
return serviceMethods.get(tag); return serviceMethods.get(tag);
try { try {
method=this.getClass().getMethod(name,clazz); method = this.getClass().getMethod(name, clazz);
} catch (NoSuchMethodException e) { } catch (NoSuchMethodException e) {
if (clazz.getSuperclass() != null) {
// method = getServiceMethod(name, clazz.getSuperclass());
try {
//只做一次父 防止mybatis.get返回的是代理dto
method = this.getClass().getMethod(name, clazz.getSuperclass());
} catch (NoSuchMethodException e2) {
}
}
//批量 List对象
if(List.class.isAssignableFrom(clazz)){
try {
method = this.getClass().getMethod(name, List.class);
} catch (NoSuchMethodException e2) {
}
}
} }
serviceMethods.put(tag,method); serviceMethods.put(tag, method);
} }
return method; return method;
......
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册