Skip to content
This repository has been archived by the owner on Jan 3, 2023. It is now read-only.

add Batch insert with increment in globalconfigdao #879

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,16 @@ public long insert(GlobalConfig globalConfig) {
public void insert(GlobalConfig[] globalConfigs) {
SimpleJdbcInsert simpleJdbcInsert = new SimpleJdbcInsert(dataSource);
simpleJdbcInsert.setTableName("global_config");
simpleJdbcInsert.usingGeneratedKeyColumns("cid");
Map<String, Object>[] maps = new Map[globalConfigs.length];
for (int i = 0; i < globalConfigs.length; i++) {
maps[i] = toMaps(globalConfigs[i]);
}
simpleJdbcInsert.executeBatch(maps);

int[] cids = simpleJdbcInsert.executeBatch(maps);
for (int i = 0; i < globalConfigs.length; i++) {
globalConfigs[i].setCid(cids[i]);
}
}

public long getCountByName(String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,17 @@ public void testInsertAndGetSingleRecord(){

@Test
public void testBatchInsert() {
GlobalConfig globalConfig = new GlobalConfig();
globalConfig.setCid(1);
globalConfig.setProperty_name("test");
globalConfig.setProperty_value("test1");


GlobalConfig[] globalConfigs = new GlobalConfig[1];
globalConfigs[0] = globalConfig;
GlobalConfig[] globalConfigs = new GlobalConfig[2];
globalConfigs[0] = new GlobalConfig(0, "test1", "test1");
globalConfigs[1] = new GlobalConfig(0, "test2", "test2");

globalConfigDao.insert(globalConfigs);

Assert.assertTrue(globalConfigDao.getById(1).equals(globalConfig));
globalConfigs[0].setCid(1);
globalConfigs[1].setCid(2);

Assert.assertTrue(globalConfigDao.getById(1).equals(globalConfigs[0]));
Assert.assertTrue(globalConfigDao.getById(2).equals(globalConfigs[1]));
}

@Test
Expand All @@ -80,4 +79,5 @@ public void testUpdate() {

Assert.assertTrue(globalConfigDao.getById(1).equals(globalConfig));
}

}