mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
fix Db 类 批量插入数据发生属性名乱序问题,导致数据无法正常插入
增加测试方法
This commit is contained in:
parent
93d4eed6a8
commit
65079d451e
@ -30,7 +30,10 @@ import org.dromara.hutool.db.sql.QuoteWrapper;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* ANSI SQL 方言
|
||||
@ -67,7 +70,8 @@ public class AnsiSqlDialect implements Dialect {
|
||||
}
|
||||
// 批量,根据第一行数据结构生成SQL占位符
|
||||
final SqlBuilder insert = SqlBuilder.of(quoteWrapper).insert(entities[0], this.dialectName());
|
||||
final Set<String> fields = CollUtil.filter(entities[0].keySet(), StrUtil::isNotBlank);
|
||||
final List<String> fields =
|
||||
entities[0].keySet().stream().filter(StrUtil::isNotBlank).collect(Collectors.toList());
|
||||
return StatementUtil.prepareStatementForBatch(conn, insert.build(), fields, entities);
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
package org.dromara.hutool.db;
|
||||
|
||||
import org.dromara.hutool.core.util.RandomUtil;
|
||||
import org.dromara.hutool.db.handler.EntityListHandler;
|
||||
import org.dromara.hutool.db.sql.Condition;
|
||||
import org.dromara.hutool.log.LogUtil;
|
||||
@ -10,12 +11,13 @@ import org.junit.jupiter.api.Test;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Db对象单元测试
|
||||
* @author looly
|
||||
*
|
||||
* @author looly
|
||||
*/
|
||||
public class DbTest {
|
||||
|
||||
@ -134,6 +136,47 @@ public class DbTest {
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void batchInsert() throws SQLException {
|
||||
List<Entity> es = new ArrayList<>();
|
||||
String tableName = "act_ge_property";
|
||||
Entity e = buildEntity(tableName);
|
||||
es.add(e);
|
||||
Db.of().tx(db -> {
|
||||
db.insert(es);
|
||||
List<Entity> ens = Db.of().find(e);
|
||||
Assertions.assertEquals(1, ens.size());
|
||||
db.del(tableName, "NAME_", "!= null");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void batchInsertBatch() throws SQLException {
|
||||
List<Entity> es = new ArrayList<>();
|
||||
String tableName = "act_ge_property";
|
||||
Entity e = buildEntity(tableName);
|
||||
es.add(e);
|
||||
es.add(buildEntity(tableName));
|
||||
es.add(buildEntity(tableName));
|
||||
es.add(buildEntity(tableName));
|
||||
Db.of().tx(db -> {
|
||||
db.insert(es);
|
||||
List<Entity> ens = Db.of().find(e);
|
||||
Assertions.assertEquals(1, ens.size());
|
||||
db.del(tableName, "NAME_", "!= null");
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
|
||||
private Entity buildEntity(String tableName) {
|
||||
Entity entity = Entity.of("act_ge_property");
|
||||
entity.put("NAME_", RandomUtil.randomString(15));
|
||||
entity.put("VALUE_", RandomUtil.randomString(50));
|
||||
entity.put("REV_", RandomUtil.randomInt());
|
||||
return entity;
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled
|
||||
public void queryFetchTest() {
|
||||
|
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user