|
@@ -33,6 +33,8 @@ import org.apache.ibatis.session.SqlSession;
|
|
|
import org.apache.ibatis.session.SqlSessionFactory;
|
|
|
import org.mybatis.spring.SqlSessionTemplate;
|
|
|
import org.mybatis.spring.SqlSessionUtils;
|
|
|
+import org.springframework.aop.framework.AopProxyUtils;
|
|
|
+import org.springframework.aop.support.AopUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.core.convert.ConversionService;
|
|
|
import org.springframework.core.convert.support.DefaultConversionService;
|
|
@@ -57,6 +59,17 @@ import java.util.function.Function;
|
|
|
@SuppressWarnings("unchecked")
|
|
|
public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
|
|
|
|
|
|
+ private static boolean loadAop = false;
|
|
|
+
|
|
|
+ static {
|
|
|
+ try {
|
|
|
+ ClassUtils.toClassConfident("org.springframework.aop.framework.AopProxyUtils");
|
|
|
+ loadAop = true;
|
|
|
+ } catch (Exception exception) {
|
|
|
+ // ignore
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
private final ConversionService conversionService = DefaultConversionService.getSharedInstance();
|
|
|
|
|
|
protected final Log log = LogFactory.getLog(getClass());
|
|
@@ -82,14 +95,22 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
|
|
|
|
|
|
private volatile SqlSessionFactory sqlSessionFactory;
|
|
|
|
|
|
- @SuppressWarnings("rawtypes")
|
|
|
+ @SuppressWarnings({"rawtypes", "deprecation"})
|
|
|
protected SqlSessionFactory getSqlSessionFactory() {
|
|
|
if (this.sqlSessionFactory == null) {
|
|
|
synchronized (this) {
|
|
|
if (this.sqlSessionFactory == null) {
|
|
|
- MybatisMapperProxy mybatisMapperProxy = (MybatisMapperProxy) Proxy.getInvocationHandler(this.baseMapper);
|
|
|
- SqlSessionTemplate sqlSessionTemplate = (SqlSessionTemplate) mybatisMapperProxy.getSqlSession();
|
|
|
- this.sqlSessionFactory = sqlSessionTemplate.getSqlSessionFactory();
|
|
|
+ Object target = this.baseMapper;
|
|
|
+ if (loadAop && AopUtils.isAopProxy(this.baseMapper)) {
|
|
|
+ target = AopProxyUtils.getSingletonTarget(this.baseMapper);
|
|
|
+ }
|
|
|
+ if (target != null) {
|
|
|
+ MybatisMapperProxy mybatisMapperProxy = (MybatisMapperProxy) Proxy.getInvocationHandler(target);
|
|
|
+ SqlSessionTemplate sqlSessionTemplate = (SqlSessionTemplate) mybatisMapperProxy.getSqlSession();
|
|
|
+ this.sqlSessionFactory = sqlSessionTemplate.getSqlSessionFactory();
|
|
|
+ } else {
|
|
|
+ this.sqlSessionFactory = GlobalConfigUtils.currentSessionFactory(this.entityClass);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|