!37 IService 添加 updateBatch 方法;ClassUtil 修复无法正确读取 JDK 动态代理超类问题

Merge pull request !37 from Saoforest/main
This commit is contained in:
Michael Yang 2023-05-17 21:27:38 +00:00 committed by Gitee
commit 663ee88313
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
2 changed files with 45 additions and 10 deletions

View File

@ -36,6 +36,8 @@ public class ClassUtil {
// javassist // javassist
, "javassist.util.proxy.ProxyObject" , "javassist.util.proxy.ProxyObject"
, "org.apache.ibatis.javassist.util.proxy.ProxyObject"); , "org.apache.ibatis.javassist.util.proxy.ProxyObject");
private static final String ENHANCER_BY = "$$EnhancerBy";
private static final String JAVASSIST_BY = "_$$_";
public static boolean isProxy(Class<?> clazz) { public static boolean isProxy(Class<?> clazz) {
for (Class<?> cls : clazz.getInterfaces()) { for (Class<?> cls : clazz.getInterfaces()) {
@ -47,12 +49,9 @@ public class ClassUtil {
return Proxy.isProxyClass(clazz); return Proxy.isProxyClass(clazz);
} }
private static final String ENHANCER_BY = "$$EnhancerBy";
private static final String JAVASSIST_BY = "_$$_";
public static <T> Class<T> getUsefulClass(Class<T> clazz) { public static <T> Class<T> getUsefulClass(Class<T> clazz) {
if (isProxy(clazz)) { if (isProxy(clazz)) {
return (Class<T>) clazz.getSuperclass(); return getJdkProxySuperClass(clazz);
} }
//ControllerTest$ServiceTest$$EnhancerByGuice$$40471411#hello -------> Guice //ControllerTest$ServiceTest$$EnhancerByGuice$$40471411#hello -------> Guice
@ -235,4 +234,9 @@ public class ClassUtil {
doGetMethods(cl.getSuperclass(), methods, predicate); doGetMethods(cl.getSuperclass(), methods, predicate);
} }
private static <T> Class<T> getJdkProxySuperClass(Class<T> clazz) {
final Class<?> proxyClass = Proxy.getProxyClass(clazz.getClassLoader(), clazz.getInterfaces());
return (Class<T>) proxyClass.getInterfaces()[0];
}
} }

View File

@ -19,6 +19,9 @@ import com.mybatisflex.core.BaseMapper;
import com.mybatisflex.core.paginate.Page; import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition; import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper; import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.row.Db;
import com.mybatisflex.core.row.RowMapper;
import com.mybatisflex.core.util.ClassUtil;
import com.mybatisflex.core.util.CollectionUtil; import com.mybatisflex.core.util.CollectionUtil;
import com.mybatisflex.core.util.SqlUtil; import com.mybatisflex.core.util.SqlUtil;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@ -37,6 +40,8 @@ import java.util.*;
@SuppressWarnings("unused") @SuppressWarnings("unused")
public interface IService<T> { public interface IService<T> {
// ===== 保存操作 =====
/** /**
* 获取对应实体类Entity的基础映射类BaseMapper * 获取对应实体类Entity的基础映射类BaseMapper
* *
@ -44,8 +49,6 @@ public interface IService<T> {
*/ */
BaseMapper<T> getMapper(); BaseMapper<T> getMapper();
// ===== 保存操作 =====
/** /**
* 保存实体类对象数据 * 保存实体类对象数据
* *
@ -80,6 +83,8 @@ public interface IService<T> {
return SqlUtil.toBool(getMapper().insertBatch(new ArrayList<>(entities))); return SqlUtil.toBool(getMapper().insertBatch(new ArrayList<>(entities)));
} }
// ===== 删除操作 =====
/** /**
* 批量保存实体类对象数据 * 批量保存实体类对象数据
* *
@ -92,8 +97,6 @@ public interface IService<T> {
return SqlUtil.toBool(getMapper().insertBatch(new ArrayList<>(entities), size)); return SqlUtil.toBool(getMapper().insertBatch(new ArrayList<>(entities), size));
} }
// ===== 删除操作 =====
/** /**
* 根据查询条件删除数据 * 根据查询条件删除数据
* *
@ -138,6 +141,8 @@ public interface IService<T> {
return SqlUtil.toBool(getMapper().deleteBatchByIds(ids)); return SqlUtil.toBool(getMapper().deleteBatchByIds(ids));
} }
// ===== 更新操作 =====
/** /**
* 根据 {@link Map} 构建查询条件删除数据 * 根据 {@link Map} 构建查询条件删除数据
* *
@ -148,8 +153,6 @@ public interface IService<T> {
return SqlUtil.toBool(getMapper().deleteByMap(query)); return SqlUtil.toBool(getMapper().deleteByMap(query));
} }
// ===== 更新操作 =====
/** /**
* 根据查询条件更新数据 * 根据查询条件更新数据
* *
@ -172,6 +175,34 @@ public interface IService<T> {
return SqlUtil.toBool(getMapper().updateByCondition(entity, condition)); return SqlUtil.toBool(getMapper().updateByCondition(entity, condition));
} }
/**
* 根据 id 批量更新数据
*
* @param entities 实体类对象集合
* @return boolean {@code true} 更新成功{@code false} 更新失败
*/
default boolean updateBatch(Collection<T> entities) {
return updateBatch(entities, RowMapper.DEFAULT_BATCH_SIZE);
}
/**
* 根据 id 批量更新数据
*
* @param entities 实体类对象集合
* @param batchSize 每批次更新数量
* @return boolean {@code true} 更新成功{@code false} 更新失败
*/
@SuppressWarnings("unchecked")
default boolean updateBatch(Collection<T> entities, int batchSize) {
return Db.tx(() -> {
final List<T> entityList = CollectionUtil.toList(entities);
// BaseMapper 是经过 Mybatis 动态代理处理过的对象需要获取原始 BaseMapper 类型
final Class<BaseMapper<T>> usefulClass = (Class<BaseMapper<T>>) ClassUtil.getUsefulClass(getMapper().getClass());
return SqlUtil.toBool(Arrays.stream(Db.executeBatch(entityList.size(), batchSize, usefulClass, (mapper, index) -> mapper.update(entityList.get(index)))).sum());
});
}
/** /**
* 根据数据主键更新数据 * 根据数据主键更新数据
* *