mxd 3 年之前
父節點
當前提交
bfbb489f08

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

@@ -432,8 +432,8 @@ public class SQLModule extends HashMap<String, SQLModule> implements MagicModule
 	/**
 	 * 插入并返回主键
 	 */
-	@Comment("批量执行insert操作,返回插入数量")
-	public int batchInsert(String sql, List<Object[]> args) {
+	@Comment("批量执行操作,返回受影响的行数")
+	public int batchUpdate(String sql, List<Object[]> args) {
 		assertDatasourceNotNull();
 		int[] values =  dataSourceNode.getJdbcTemplate().batchUpdate(sql, args);
 		if (this.cacheName != null) {
@@ -445,8 +445,8 @@ public class SQLModule extends HashMap<String, SQLModule> implements MagicModule
 	/**
 	 * 插入并返回主键
 	 */
-	@Comment("批量执行insert操作,返回插入数量")
-	public int batchInsert(String sql, int batchSize, List<Object[]> args) {
+	@Comment("批量执行操作,返回受影响的行数")
+	public int batchUpdate(String sql, int batchSize, List<Object[]> args) {
 		assertDatasourceNotNull();
 		int[][] values = dataSourceNode.getJdbcTemplate().batchUpdate(sql, args, batchSize, (ps, arguments) -> {
 			int colIndex = 1;
@@ -472,8 +472,8 @@ public class SQLModule extends HashMap<String, SQLModule> implements MagicModule
 	/**
 	 * 插入并返回主键
 	 */
-	@Comment("批量执行insert操作,返回插入数量")
-	public int batchInsert(@Comment(name = "sqls", value = "`SQL`语句") List<String> sqls) {
+	@Comment("批量执行操作,返回受影响的行数")
+	public int batchUpdate(@Comment(name = "sqls", value = "`SQL`语句") List<String> sqls) {
 		assertDatasourceNotNull();
 		int[] values = dataSourceNode.getJdbcTemplate().batchUpdate(sqls.toArray(new String[0]));
 		if (this.cacheName != null) {

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

@@ -344,7 +344,7 @@ public class NamedTable extends Attributes<Object> {
 		builder.append(") values (");
 		builder.append(StringUtils.join(Collections.nCopies(keys.size(), "?"), ","));
 		builder.append(")");
-		return this.sqlModule.batchInsert(builder.toString(), batchSize, collection.stream()
+		return this.sqlModule.batchUpdate(builder.toString(), batchSize, collection.stream()
 				.map(it -> keys.stream().map(it::get).toArray())
 				.collect(Collectors.toList()));
 	}
@@ -354,6 +354,40 @@ public class NamedTable extends Attributes<Object> {
 		return batchInsert(collection, 100);
 	}
 
+	@Comment("批量修改")
+	public int batchUpdate(@Comment(name = "collection", value = "各项列和值") Collection<Map<String, Object>> collection, @Comment("batchSize") int batchSize) {
+		if (StringUtils.isBlank(this.primary)) {
+			throw new MagicAPIException("请设置主键");
+		}
+		List<String> keys = collection.stream().peek(it -> {
+			if(StringUtils.isBlank(Objects.toString(it.get(this.primary), ""))){
+				throw new MagicAPIException("主键值不能为空");
+			}
+		}).flatMap(it -> it.keySet().stream()).filter(it -> !it.equals(this.primary)).distinct().collect(Collectors.toList());
+		if (keys.isEmpty()) {
+			throw new MagicAPIException("要修改的列不能为空");
+		}
+		StringBuilder builder = new StringBuilder();
+		builder.append("update ");
+		builder.append(tableName);
+		builder.append(" set ");
+		for (int i = 0, size = keys.size(); i < size; i++) {
+			builder.append(keys.get(i)).append(" = ?");
+			if (i + 1 < size) {
+				builder.append(",");
+			}
+		}
+		builder.append(" where ").append(this.primary).append(" = ?");
+		return this.sqlModule.batchUpdate(builder.toString(), batchSize, collection.stream()
+				.map(it -> keys.stream().map(it::get).toArray())
+				.collect(Collectors.toList()));
+	}
+
+	@Comment("批量修改")
+	public int batchUpdate(@Comment(name = "collection", value = "各项列和值") Collection<Map<String, Object>> collection) {
+		return batchUpdate(collection, 100);
+	}
+
 
 	@Comment("执行`select`查询")
 	public List<Map<String, Object>> select(RuntimeContext runtimeContext) {