refactor: optimize MapperHandler

This commit is contained in:
开源海哥 2023-07-30 20:01:35 +08:00
parent a6b8eece5c
commit ce4a935adb
2 changed files with 30 additions and 5 deletions

View File

@ -47,6 +47,7 @@ public class Mappers {
/** /**
* 通过 entity Class 获取 Mapper 对象 * 通过 entity Class 获取 Mapper 对象
*
* @param entityClass * @param entityClass
* @param <Entity> * @param <Entity>
* @return mapper 对象 * @return mapper 对象
@ -75,8 +76,8 @@ public class Mappers {
} }
static class MapperHandler implements InvocationHandler { static class MapperHandler implements InvocationHandler {
private Class<?> mapperClass; private Class<?> mapperClass;
private final SqlSessionFactory sqlSessionFactory = FlexGlobalConfig.getDefaultConfig().getSqlSessionFactory(); private final SqlSessionFactory sqlSessionFactory = FlexGlobalConfig.getDefaultConfig().getSqlSessionFactory();
private final ExecutorType executorType = FlexGlobalConfig.getDefaultConfig().getConfiguration().getDefaultExecutorType(); private final ExecutorType executorType = FlexGlobalConfig.getDefaultConfig().getConfiguration().getDefaultExecutorType();
@ -91,9 +92,15 @@ public class Mappers {
@Override @Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
try (SqlSession sqlSession = openSession()) { if ("getClass".equals(method.getName())) {
Object mapper = sqlSession.getMapper(mapperClass); return mapperClass;
return method.invoke(mapper, args); } else if ("toString".equals(method.getName())) {
return mapperClass.getName();
} else {
try (SqlSession sqlSession = openSession()) {
Object mapper = sqlSession.getMapper(mapperClass);
return method.invoke(mapper, args);
}
} }
} }
} }

View File

@ -24,13 +24,27 @@ import java.util.function.Predicate;
/** /**
* 类实例创建者创建者 * 类实例创建者创建者
* Created by michael on 17/3/21. *
* @author michael
* @date 17/3/21
*/ */
public class ClassUtil { public class ClassUtil {
private ClassUtil() { private ClassUtil() {
} }
private static final String[] OBJECT_METHODS = new String[]{
"toString",
"getClass",
"equals",
"hashCode",
"wait",
"notify",
"notifyAll",
"clone",
"finalize"
};
//proxy frameworks //proxy frameworks
private static final List<String> PROXY_CLASS_NAMES = Arrays.asList("net.sf.cglib.proxy.Factory" private static final List<String> PROXY_CLASS_NAMES = Arrays.asList("net.sf.cglib.proxy.Factory"
// cglib // cglib
@ -299,4 +313,8 @@ public class ClassUtil {
} }
} }
public static boolean isObjectMethod(String methodName) {
return ArrayUtil.contains(OBJECT_METHODS, methodName);
}
} }