mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-08 01:28:24 +08:00
add Db.executeBatch method
This commit is contained in:
parent
d7c5ebd16d
commit
f2f6846ed4
@ -220,18 +220,18 @@ public class RowSqlProvider {
|
||||
*
|
||||
* @param params
|
||||
* @return sql
|
||||
* @see RowMapper#updateBatchEntity(Object entities)
|
||||
* @see RowMapper#updateEntity(Object entities)
|
||||
*/
|
||||
public static String updateBatchEntity(Map params) {
|
||||
public static String updateEntity(Map params) {
|
||||
Object entity = ProviderUtil.getEntity(params);
|
||||
if (entity == null) {
|
||||
throw FlexExceptions.wrap("entity can not be null");
|
||||
}
|
||||
|
||||
// 该 Mapper 是通用 Mapper 无法通过ProviderContext获取,直接使用TableInfoFactory
|
||||
final TableInfo tableInfo = TableInfoFactory.ofEntityClass(entity.getClass());
|
||||
// 该 Mapper 是通用 Mapper 无法通过 ProviderContext 获取,直接使用 TableInfoFactory
|
||||
TableInfo tableInfo = TableInfoFactory.ofEntityClass(entity.getClass());
|
||||
|
||||
//执行 onUpdate 监听器
|
||||
// 执行 onUpdate 监听器
|
||||
tableInfo.invokeOnUpdateListener(entity);
|
||||
|
||||
Object[] updateValues = tableInfo.buildUpdateSqlArgs(entity, false, false);
|
||||
|
||||
@ -24,13 +24,16 @@ import com.mybatisflex.core.query.QueryTable;
|
||||
import com.mybatisflex.core.query.QueryWrapper;
|
||||
import com.mybatisflex.core.transaction.Propagation;
|
||||
import com.mybatisflex.core.transaction.TransactionalManager;
|
||||
import com.mybatisflex.core.util.CollectionUtil;
|
||||
import org.apache.ibatis.session.SqlSessionFactory;
|
||||
import org.apache.ibatis.util.MapUtil;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
@ -86,7 +89,7 @@ public class Db {
|
||||
* @param tableName 表名
|
||||
* @param rows 数据
|
||||
*/
|
||||
public static int[] insertBatch(String tableName, List<Row> rows) {
|
||||
public static int[] insertBatch(String tableName, Collection<Row> rows) {
|
||||
return insertBatch(tableName, rows, rows.size());
|
||||
}
|
||||
|
||||
@ -97,12 +100,16 @@ public class Db {
|
||||
* @param rows 数据
|
||||
* @param batchSize 每次提交的数据量
|
||||
*/
|
||||
public static int[] insertBatch(String tableName, List<Row> rows, int batchSize) {
|
||||
return invoker().insertBatch(tableName, rows, batchSize);
|
||||
public static int[] insertBatch(String tableName, Collection<Row> rows, int batchSize) {
|
||||
List<Row> list = CollectionUtil.toList(rows);
|
||||
return executeBatch(rows.size(), batchSize, RowMapper.class, (mapper, index) -> {
|
||||
Row row = list.get(index);
|
||||
mapper.insert(tableName, row);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量插入数据,根据第一条内容来构建插入的字段,效率比 {@link #insertBatch(String, List, int)} 高
|
||||
* 批量插入数据,根据第一条内容来构建插入的字段,效率比 {@link #insertBatch(String, Collection, int)} 高
|
||||
*
|
||||
* @param tableName 表名
|
||||
* @param rows 数据
|
||||
@ -197,13 +204,14 @@ public class Db {
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param sql
|
||||
* @param batchArgsSetter
|
||||
* @return
|
||||
*/
|
||||
public static int[] updateBatch(String sql, BatchArgsSetter batchArgsSetter){
|
||||
return invoker().updateBatch(sql, batchArgsSetter);
|
||||
public static int[] updateBatch(String sql, BatchArgsSetter batchArgsSetter) {
|
||||
int batchSize = batchArgsSetter.getBatchSize();
|
||||
return executeBatch(batchSize, batchSize, RowMapper.class
|
||||
, (mapper, index) -> mapper.updateBySql(sql, batchArgsSetter.getSqlArgs(index)));
|
||||
}
|
||||
|
||||
|
||||
@ -266,25 +274,43 @@ public class Db {
|
||||
/**
|
||||
* 根据主键来批量更新数据
|
||||
*
|
||||
* @param entities 实体
|
||||
* @param size 大小
|
||||
* @param entities 实体
|
||||
* @param batchSize 批次大小
|
||||
* @return int
|
||||
*/
|
||||
public static <T> int updateBatchEntity(Collection<T> entities, int size) {
|
||||
return invoker().updateBatchEntity(entities, size);
|
||||
public static <T> int updateBatchEntity(Collection<T> entities, int batchSize) {
|
||||
List<T> list = CollectionUtil.toList(entities);
|
||||
return Arrays.stream(executeBatch(list.size(), batchSize, RowMapper.class, (mapper, index) -> {
|
||||
T entity = list.get(index);
|
||||
mapper.updateEntity(entity);
|
||||
})).sum();
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据主键来批量更新数据
|
||||
*
|
||||
* @param entities 实体
|
||||
* @return int
|
||||
* @return int 影响行数
|
||||
*/
|
||||
public static <T> int updateBatchEntity(Collection<T> entities) {
|
||||
return invoker().updateBatchEntity(entities, RowMapper.DEFAULT_BATCH_SIZE);
|
||||
return updateBatchEntity(entities, RowMapper.DEFAULT_BATCH_SIZE);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 批量执行工具方法
|
||||
*
|
||||
* @param totalSize 执行总量
|
||||
* @param batchSize 每一批次的数据量
|
||||
* @param mapperClass 通过那个 Mapper 来执行
|
||||
* @param consumer 执行内容
|
||||
* @param <M> Mapper
|
||||
* @return 执行影响的行数
|
||||
*/
|
||||
public static <M> int[] executeBatch(int totalSize, int batchSize, Class<M> mapperClass, BiConsumer<M, Integer> consumer) {
|
||||
return invoker().executeBatch(totalSize, batchSize, mapperClass, consumer);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 sql 来查询 1 条数据
|
||||
*
|
||||
@ -466,7 +492,8 @@ public class Db {
|
||||
|
||||
/**
|
||||
* 根据 queryWrapper 查询内容,数据返回的应该只有 1 行 1 列
|
||||
* @param tableName 表名
|
||||
*
|
||||
* @param tableName 表名
|
||||
* @param queryWrapper query 封装
|
||||
* @return 数据内容
|
||||
*/
|
||||
@ -477,6 +504,7 @@ public class Db {
|
||||
|
||||
/**
|
||||
* 根据 queryWrapper 查询内容,数据返回的应该只有 1 行 1 列
|
||||
*
|
||||
* @param queryWrapper query 封装
|
||||
* @return 数据内容
|
||||
*/
|
||||
@ -498,7 +526,8 @@ public class Db {
|
||||
|
||||
/**
|
||||
* 根据 queryWrapper 查询内容,数据返回的应该只有 1 行 1 列
|
||||
* @param tableName 表名
|
||||
*
|
||||
* @param tableName 表名
|
||||
* @param queryWrapper query 封装
|
||||
* @return 数据内容
|
||||
*/
|
||||
@ -509,6 +538,7 @@ public class Db {
|
||||
|
||||
/**
|
||||
* 根据 queryWrapper 查询内容,数据返回的应该只有 1 行 1 列
|
||||
*
|
||||
* @param queryWrapper query 封装
|
||||
* @return 数据内容
|
||||
*/
|
||||
|
||||
@ -38,7 +38,7 @@ import static com.mybatisflex.core.query.QueryMethods.count;
|
||||
|
||||
public interface RowMapper {
|
||||
|
||||
Integer DEFAULT_BATCH_SIZE = 1000;
|
||||
int DEFAULT_BATCH_SIZE = 1000;
|
||||
|
||||
//////insert //////
|
||||
|
||||
@ -194,8 +194,14 @@ public interface RowMapper {
|
||||
int updateBatchById(@Param(FlexConsts.TABLE_NAME) String tableName, @Param(FlexConsts.ROWS) List<Row> rows);
|
||||
|
||||
|
||||
@UpdateProvider(value = RowSqlProvider.class, method = "updateBatchEntity")
|
||||
int updateBatchEntity(@Param(FlexConsts.ENTITY) Object entities);
|
||||
/**
|
||||
* 更新 entity,主要用于进行批量更新的场景
|
||||
* @param entity 实体类
|
||||
* @see RowSqlProvider#updateEntity(Map)
|
||||
* @see RowMapperInvoker#updateBatchEntity(Collection, int)
|
||||
*/
|
||||
@UpdateProvider(value = RowSqlProvider.class, method = "updateEntity")
|
||||
int updateEntity(@Param(FlexConsts.ENTITY) Object entity);
|
||||
|
||||
///////select /////
|
||||
|
||||
|
||||
@ -22,8 +22,6 @@ import org.apache.ibatis.session.ExecutorType;
|
||||
import org.apache.ibatis.session.SqlSession;
|
||||
import org.apache.ibatis.session.SqlSessionFactory;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.function.BiConsumer;
|
||||
@ -53,13 +51,6 @@ public class RowMapperInvoker {
|
||||
return execute(mapper -> mapper.insertBySql(sql, args));
|
||||
}
|
||||
|
||||
public int[] insertBatch(String tableName, List<Row> rows, int batchSize) {
|
||||
return executeBatch(rows.size(), batchSize, (mapper, index) -> {
|
||||
Row row = rows.get(index);
|
||||
mapper.insert(tableName, row);
|
||||
});
|
||||
}
|
||||
|
||||
public int insertBatchWithFirstRowColumns(String tableName, List<Row> rows) {
|
||||
return execute(mapper -> mapper.insertBatchWithFirstRowColumns(tableName, rows));
|
||||
}
|
||||
@ -89,18 +80,11 @@ public class RowMapperInvoker {
|
||||
return execute(mapper -> mapper.updateBySql(sql, args));
|
||||
}
|
||||
|
||||
public int[] updateBatch(String sql, BatchArgsSetter batchArgsSetter) {
|
||||
int batchSize = batchArgsSetter.getBatchSize();
|
||||
return executeBatch(batchSize, batchSize,
|
||||
(mapper, index) -> mapper.updateBySql(sql, batchArgsSetter.getSqlArgs(index))
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
public int[] executeBatch(int totalSize, int batchSize, BiConsumer<RowMapper, Integer> consumer) {
|
||||
public <M> int[] executeBatch(int totalSize, int batchSize, Class<M> mapperClass, BiConsumer<M, Integer> consumer) {
|
||||
int[] results = new int[totalSize];
|
||||
try (SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, true)) {
|
||||
RowMapper mapper = sqlSession.getMapper(RowMapper.class);
|
||||
M mapper = sqlSession.getMapper(mapperClass);
|
||||
int counter = 0;
|
||||
int resultsPos = 0;
|
||||
for (int i = 0; i < totalSize; i++) {
|
||||
@ -143,14 +127,6 @@ public class RowMapperInvoker {
|
||||
return execute(mapper -> mapper.updateBatchById(tableName, rows));
|
||||
}
|
||||
|
||||
public <T> int updateBatchEntity(Collection<T> entities, int size) {
|
||||
final ArrayList<T> ts = new ArrayList<>(entities);
|
||||
return Arrays.stream(executeBatch(ts.size(), size, (mapper, index) -> {
|
||||
T entity = ts.get(index);
|
||||
mapper.updateBatchEntity(entity);
|
||||
})).sum();
|
||||
}
|
||||
|
||||
public Row selectOneBySql(String sql, Object... args) {
|
||||
return execute(mapper -> mapper.selectOneBySql(sql, args));
|
||||
}
|
||||
|
||||
@ -83,4 +83,12 @@ public class CollectionUtil {
|
||||
return new ArrayList<>(Arrays.asList(elements));
|
||||
}
|
||||
|
||||
public static <T> List<T> toList(Collection<T> collection) {
|
||||
if (collection instanceof List) {
|
||||
return (List<T>) collection;
|
||||
} else {
|
||||
return new ArrayList<>(collection);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user