增强BaseMapper代理类获取.

This commit is contained in:
nieqiurong 2024-04-20 22:16:56 +08:00
parent f2fd9b892b
commit 8953ad2712
5 changed files with 52 additions and 12 deletions

View File

@ -43,7 +43,6 @@ import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import java.io.Serializable;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
@ -142,7 +141,7 @@ public interface BaseMapper<T> extends Mapper<T> {
return this.deleteById(instance);
}
}
MybatisMapperProxy<?> mybatisMapperProxy = (MybatisMapperProxy<?>) Proxy.getInvocationHandler(this);
MybatisMapperProxy<?> mybatisMapperProxy = MybatisUtils.getMybatisMapperProxy(this);
SqlSession sqlSession = mybatisMapperProxy.getSqlSession();
return sqlSession.delete(mybatisMapperProxy.getMapperInterface().getName() + Constants.DOT + SqlMethod.DELETE_BY_ID.getMethod(), obj);
}
@ -189,7 +188,7 @@ public interface BaseMapper<T> extends Mapper<T> {
* @since 3.5.7
*/
default int deleteBatchIds(@Param(Constants.COLL) Collection<?> collections, boolean useFill) {
MybatisMapperProxy<?> mybatisMapperProxy = (MybatisMapperProxy<?>) Proxy.getInvocationHandler(this);
MybatisMapperProxy<?> mybatisMapperProxy = MybatisUtils.getMybatisMapperProxy(this);
Class<?> entityClass = GenericTypeUtils.resolveTypeArguments(getClass(), BaseMapper.class)[0];
SqlSession sqlSession = mybatisMapperProxy.getSqlSession();
Class<?> mapperInterface = mybatisMapperProxy.getMapperInterface();
@ -462,7 +461,7 @@ public interface BaseMapper<T> extends Mapper<T> {
* @since 3.5.7
*/
default List<BatchResult> saveBatch(Collection<T> entityList) {
MybatisMapperProxy<?> mybatisMapperProxy = (MybatisMapperProxy<?>) Proxy.getInvocationHandler(this);
MybatisMapperProxy<?> mybatisMapperProxy = MybatisUtils.getMybatisMapperProxy(this);
MybatisBatch.Method<T> method = new MybatisBatch.Method<>(mybatisMapperProxy.getMapperInterface());
SqlSessionFactory sqlSessionFactory = MybatisUtils.getSqlSessionFactory(mybatisMapperProxy);
return MybatisBatchUtils.execute(sqlSessionFactory, entityList, method.insert());
@ -475,7 +474,7 @@ public interface BaseMapper<T> extends Mapper<T> {
* @since 3.5.7
*/
default List<BatchResult> updateBatchById(Collection<T> entityList) {
MybatisMapperProxy<?> mybatisMapperProxy = (MybatisMapperProxy<?>) Proxy.getInvocationHandler(this);
MybatisMapperProxy<?> mybatisMapperProxy = MybatisUtils.getMybatisMapperProxy(this);
MybatisBatch.Method<T> method = new MybatisBatch.Method<>(mybatisMapperProxy.getMapperInterface());
SqlSessionFactory sqlSessionFactory = MybatisUtils.getSqlSessionFactory(mybatisMapperProxy);
return MybatisBatchUtils.execute(sqlSessionFactory, entityList, method.updateById());
@ -488,7 +487,7 @@ public interface BaseMapper<T> extends Mapper<T> {
* @since 3.5.7
*/
default List<BatchResult> saveOrUpdateBatch(Collection<T> entityList) {
MybatisMapperProxy<?> mybatisMapperProxy = (MybatisMapperProxy<?>) Proxy.getInvocationHandler(this);
MybatisMapperProxy<?> mybatisMapperProxy = MybatisUtils.getMybatisMapperProxy(this);
Class<?> entityClass = GenericTypeUtils.resolveTypeArguments(getClass(), BaseMapper.class)[0];
TableInfo tableInfo = TableInfoHelper.getTableInfo(entityClass);
String keyProperty = tableInfo.getKeyProperty();
@ -506,7 +505,7 @@ public interface BaseMapper<T> extends Mapper<T> {
* @since 3.5.7
*/
default List<BatchResult> saveOrUpdateBatch(Collection<T> entityList, BiPredicate<BatchSqlSession, T> insertPredicate) {
MybatisMapperProxy<?> mybatisMapperProxy = (MybatisMapperProxy<?>) Proxy.getInvocationHandler(this);
MybatisMapperProxy<?> mybatisMapperProxy = MybatisUtils.getMybatisMapperProxy(this);
MybatisBatch.Method<T> method = new MybatisBatch.Method<>(mybatisMapperProxy.getMapperInterface());
SqlSessionFactory sqlSessionFactory = MybatisUtils.getSqlSessionFactory(mybatisMapperProxy);
return MybatisBatchUtils.saveOrUpdate(sqlSessionFactory, entityList, method.insert(), insertPredicate, method.updateById());

View File

@ -13,10 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.baomidou.mybatisplus.extension.toolkit;
package com.baomidou.mybatisplus.core.toolkit;
import com.baomidou.mybatisplus.core.toolkit.ClassUtils;
import com.baomidou.mybatisplus.core.toolkit.ExceptionUtils;
import org.apache.ibatis.logging.Log;
import org.apache.ibatis.logging.LogFactory;

View File

@ -1,5 +1,6 @@
package com.baomidou.mybatisplus.core.toolkit;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.handlers.IJsonTypeHandler;
import com.baomidou.mybatisplus.core.override.MybatisMapperProxy;
import lombok.experimental.UtilityClass;
@ -9,8 +10,10 @@ import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.defaults.DefaultSqlSession;
import org.apache.ibatis.type.TypeException;
import org.apache.ibatis.type.TypeHandler;
import org.springframework.aop.framework.AopProxyUtils;
import java.lang.reflect.Field;
import java.lang.reflect.Proxy;
/**
* @author nieqiurong
@ -53,6 +56,13 @@ public class MybatisUtils {
return result;
}
/**
* 获取SqlSessionFactory
*
* @param mybatisMapperProxy {@link MybatisMapperProxy}
* @return SqlSessionFactory
* @since 3.5.7
*/
public static SqlSessionFactory getSqlSessionFactory(MybatisMapperProxy<?> mybatisMapperProxy) {
SqlSession sqlSession = mybatisMapperProxy.getSqlSession();
if (sqlSession instanceof DefaultSqlSession) {
@ -69,4 +79,33 @@ public class MybatisUtils {
}
}
/**
* 获取代理实现
*
* @param mapper mapper类
* @return 代理实现
* @since 3.5.7
*/
public static MybatisMapperProxy<?> getMybatisMapperProxy(Object mapper) {
if (mapper instanceof MybatisMapperProxy) {
// fast return
return (MybatisMapperProxy<?>) mapper;
}
Object result = mapper;
if (AopUtils.isLoadSpringAop()) {
while (org.springframework.aop.support.AopUtils.isAopProxy(result)) {
result = AopProxyUtils.getSingletonTarget(result);
}
}
if (result != null) {
while (Proxy.isProxyClass(result.getClass())) {
result = Proxy.getInvocationHandler(mapper);
}
}
if (result instanceof MybatisMapperProxy) {
return (MybatisMapperProxy<?>) result;
}
throw new MybatisPlusException("Unable to get MybatisMapperProxy : " + mapper);
}
}

View File

@ -28,4 +28,5 @@ dependencies {
testImplementation "io.github.classgraph:classgraph:4.8.165"
testImplementation "${lib.h2}"
testImplementation "${lib.mysql}"
testImplementation "${lib.'logback-classic'}"
}

View File

@ -86,13 +86,16 @@ public abstract class ServiceImpl<M extends BaseMapper<T>, T> implements IServic
if (this.sqlSessionFactory == null) {
Object target = this.baseMapper;
// 这个检查目前看着来说基本上可以不用判断Aop是不是存在了.
if (com.baomidou.mybatisplus.extension.toolkit.AopUtils.isLoadSpringAop()) {
if (com.baomidou.mybatisplus.core.toolkit.AopUtils.isLoadSpringAop()) {
while (AopUtils.isAopProxy(target)) {
target = AopProxyUtils.getSingletonTarget(target);
}
}
if (target != null && Proxy.isProxyClass(target.getClass())) {
target = Proxy.getInvocationHandler(target);
}
if (target instanceof MybatisMapperProxy) {
MybatisMapperProxy mybatisMapperProxy = (MybatisMapperProxy) Proxy.getInvocationHandler(target);
MybatisMapperProxy mybatisMapperProxy = (MybatisMapperProxy) target;
SqlSessionTemplate sqlSessionTemplate = (SqlSessionTemplate) mybatisMapperProxy.getSqlSession();
this.sqlSessionFactory = sqlSessionTemplate.getSqlSessionFactory();
} else {