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

Commit

Permalink
Add Batch insert with increment in globalconfigdao (#879)
Browse files Browse the repository at this point in the history
  • Loading branch information
duzhen1996 authored and qiyuangong committed Aug 4, 2017
1 parent c09e7ab commit 30cd4fc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
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));
}

}

0 comments on commit 30cd4fc

Please sign in to comment.