Browse Source

新增批量插入方法

mxd 3 years ago
parent
commit
1cff7ad2af

+ 42 - 10
magic-api/src/main/java/org/ssssssss/magicapi/modules/db/SQLModule.java

@@ -1,9 +1,7 @@
 package org.ssssssss.magicapi.modules.db;
 
 import org.apache.commons.lang3.StringUtils;
-import org.springframework.jdbc.core.ArgumentPreparedStatementSetter;
-import org.springframework.jdbc.core.RowMapper;
-import org.springframework.jdbc.core.SqlParameterValue;
+import org.springframework.jdbc.core.*;
 import org.springframework.jdbc.support.GeneratedKeyHolder;
 import org.ssssssss.magicapi.modules.db.dialect.DialectAdapter;
 import org.ssssssss.magicapi.datasource.model.MagicDynamicDataSource;
@@ -434,20 +432,54 @@ public class SQLModule extends HashMap<String, SQLModule> implements MagicModule
 	/**
 	 * 插入并返回主键
 	 */
-	@Comment("批量执行insert操作,返回插入主键数组")
-	public int[] batchInsert(@Comment(name = "sql", value = "`SQL`语句") String sql,
-							 @Comment(name = "list", value = "参数") List<Object[]> list) {
+	@Comment("批量执行insert操作,返回插入数量")
+	public int batchInsert(String sql, List<Object[]> args) {
 		assertDatasourceNotNull();
-		return dataSourceNode.getJdbcTemplate().batchUpdate(sql, list);
+		int[] values =  dataSourceNode.getJdbcTemplate().batchUpdate(sql, args);
+		if (this.cacheName != null) {
+			this.sqlCache.delete(this.cacheName);
+		}
+		return Arrays.stream(values).sum();
 	}
 
 	/**
 	 * 插入并返回主键
 	 */
-	@Comment("批量执行insert操作,返回插入主键数组")
-	public int[] batchInsert(@Comment(name = "sqls", value = "`SQL`语句") String[] sqls) {
+	@Comment("批量执行insert操作,返回插入数量")
+	public int batchInsert(String sql, int batchSize, List<Object[]> args) {
 		assertDatasourceNotNull();
-		return dataSourceNode.getJdbcTemplate().batchUpdate(sqls);
+		int[][] values = dataSourceNode.getJdbcTemplate().batchUpdate(sql, args, batchSize, (ps, arguments) -> {
+			int colIndex = 1;
+			for (Object value : arguments) {
+				if (value instanceof SqlParameterValue) {
+					SqlParameterValue paramValue = (SqlParameterValue) value;
+					StatementCreatorUtils.setParameterValue(ps, colIndex++, paramValue, paramValue.getValue());
+				} else {
+					StatementCreatorUtils.setParameterValue(ps, colIndex++, StatementCreatorUtils.javaTypeToSqlParameterType(value == null ? null : value.getClass()), value);
+				}
+			}
+		});
+		if (this.cacheName != null) {
+			this.sqlCache.delete(this.cacheName);
+		}
+		int count = 0;
+		for (int[] value : values) {
+			count += Arrays.stream(value).sum();
+		}
+		return count;
+	}
+
+	/**
+	 * 插入并返回主键
+	 */
+	@Comment("批量执行insert操作,返回插入数量")
+	public int batchInsert(@Comment(name = "sqls", value = "`SQL`语句") List<String> sqls) {
+		assertDatasourceNotNull();
+		int[] values = dataSourceNode.getJdbcTemplate().batchUpdate(sqls.toArray(new String[0]));
+		if (this.cacheName != null) {
+			this.sqlCache.delete(this.cacheName);
+		}
+		return Arrays.stream(values).sum();
 	}
 
 	@UnableCall

+ 24 - 0
magic-api/src/main/java/org/ssssssss/magicapi/modules/db/table/NamedTable.java

@@ -330,6 +330,30 @@ public class NamedTable extends Attributes<Object> {
 		return this.save(runtimeContext, data, false);
 	}
 
+	@Comment("批量插入")
+	public int batchInsert(@Comment(name = "collection", value = "各项列和值") Collection<Map<String, Object>> collection, @Comment("batchSize") int batchSize) {
+		Set<String> keys = collection.stream().flatMap(it -> it.keySet().stream()).collect(Collectors.toSet());
+		if (keys.isEmpty()) {
+			throw new MagicAPIException("要插入的列不能为空");
+		}
+		StringBuilder builder = new StringBuilder();
+		builder.append("insert into ");
+		builder.append(tableName);
+		builder.append("(");
+		builder.append(StringUtils.join(keys.stream().map(rowMapColumnMapper).collect(Collectors.toList()), ","));
+		builder.append(") values (");
+		builder.append(StringUtils.join(Collections.nCopies(keys.size(), "?"), ","));
+		builder.append(")");
+		return this.sqlModule.batchInsert(builder.toString(), batchSize, collection.stream()
+				.map(it -> keys.stream().map(it::get).toArray())
+				.collect(Collectors.toList()));
+	}
+
+	@Comment("批量插入")
+	public int batchInsert(@Comment(name = "collection", value = "各项列和值") Collection<Map<String, Object>> collection) {
+		return batchInsert(collection, 100);
+	}
+
 
 	@Comment("执行`select`查询")
 	public List<Map<String, Object>> select(RuntimeContext runtimeContext) {