mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-08 01:28:24 +08:00
feat: ✨ IService 添加 updateBatch 方法;ClassUtil 修复无法正确读取 JDK 动态代理超类问题
This commit is contained in:
parent
11fe36ad98
commit
8d7f7d21cb
@ -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];
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据数据主键更新数据。
|
* 根据数据主键更新数据。
|
||||||
*
|
*
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user