org.zodiac.datasource.jdbc.dynamic.DataSourceClassResolver Maven / Gradle / Ivy
The newest version!
package org.zodiac.datasource.jdbc.dynamic;
import org.aopalliance.intercept.MethodInvocation;
import java.lang.reflect.Field;
import java.lang.reflect.Proxy;
/**
* 获取对mybatis(tk.mapper及mybatis-plus)的支持。
*
*/
public class DataSourceClassResolver {
private static boolean mpEnabled = false;
private static Field mapperInterfaceField;
static {
Class> proxyClass = null;
try {
proxyClass = Class.forName("com.baomidou.mybatisplus.core.override.MybatisMapperProxy");
} catch (ClassNotFoundException e1) {
try {
proxyClass = Class.forName("com.baomidou.mybatisplus.core.override.PageMapperProxy");
} catch (ClassNotFoundException e2) {
try {
proxyClass = Class.forName("org.apache.ibatis.binding.MapperProxy");
} catch (ClassNotFoundException e3) {
}
}
}
if (proxyClass != null) {
try {
mapperInterfaceField = proxyClass.getDeclaredField("mapperInterface");
mapperInterfaceField.setAccessible(true);
mpEnabled = true;
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
}
}
public Class> targetClass(MethodInvocation invocation) throws IllegalAccessException {
if (mpEnabled) {
Object target = invocation.getThis();
Class> targetClass = target.getClass();
return Proxy.isProxyClass(targetClass)
? (Class>)mapperInterfaceField.get(Proxy.getInvocationHandler(target)) : targetClass;
}
return invocation.getMethod().getDeclaringClass();
}
}