Commit a4add006524109578f71bc4f85d90b5665386de0
1 parent
8ace1bac
Exists in
master
and in
2 other branches
1.批量添加的支持
Showing
2 changed files
with
26 additions
and
25 deletions
Show diff stats
build.gradle
| @@ -55,7 +55,7 @@ uploadArchives { | @@ -55,7 +55,7 @@ uploadArchives { | ||
| 55 | authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD) | 55 | authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD) |
| 56 | } | 56 | } |
| 57 | pom.project { | 57 | pom.project { |
| 58 | - version '2.1.16' | 58 | + version '2.1.22' |
| 59 | artifactId ARTIFACT_Id | 59 | artifactId ARTIFACT_Id |
| 60 | groupId GROUP_ID | 60 | groupId GROUP_ID |
| 61 | packaging TYPE | 61 | packaging TYPE |
src/main/java/com/taover/repository/CustomJdbcTemplate.java
| @@ -451,28 +451,44 @@ public class CustomJdbcTemplate<T, ID extends Serializable> { | @@ -451,28 +451,44 @@ public class CustomJdbcTemplate<T, ID extends Serializable> { | ||
| 451 | * 批量添加 | 451 | * 批量添加 |
| 452 | * @throws Exception | 452 | * @throws Exception |
| 453 | */ | 453 | */ |
| 454 | - public int[] addEntityList(List<T> entityList) throws Exception { | 454 | + public int addEntityList(List<T> entityList) throws Exception { |
| 455 | if(entityList == null || entityList.isEmpty()) { | 455 | if(entityList == null || entityList.isEmpty()) { |
| 456 | throw new Exception("entitylist is empty or null"); | 456 | throw new Exception("entitylist is empty or null"); |
| 457 | } | 457 | } |
| 458 | //构造SQL语句及Entity Field列表 | 458 | //构造SQL语句及Entity Field列表 |
| 459 | List<Field> beanFieldList = new ArrayList<Field>(this.beanToTableField.size()); | 459 | List<Field> beanFieldList = new ArrayList<Field>(this.beanToTableField.size()); |
| 460 | - String exeSql = this.constructUpdateSql(entityList.get(0), beanFieldList); | 460 | + StringBuffer exeSql = new StringBuffer(this.constructUpdateSql(entityList.get(0), beanFieldList)); |
| 461 | 461 | ||
| 462 | //构造参数信息 | 462 | //构造参数信息 |
| 463 | - List<Object[]> batchArgs = new ArrayList<Object[]>(); | ||
| 464 | - for(T item: entityList) { | ||
| 465 | - Object[] itemData = this.getArgsByFieldAndEntity(item, beanFieldList); | ||
| 466 | - batchArgs.add(itemData); | 463 | + List<Object> args = new ArrayList<Object>(); |
| 464 | + exeSql.append(" VALUES"); | ||
| 465 | + for(int itemIndex=0; itemIndex<entityList.size(); ++itemIndex) { | ||
| 466 | + T item = entityList.get(itemIndex); | ||
| 467 | + exeSql.append("("); | ||
| 468 | + for(int i=0; i<beanFieldList.size(); ++i) { | ||
| 469 | + Field itemField = beanFieldList.get(i); | ||
| 470 | + Object itemData = itemField.get(item); | ||
| 471 | + if(itemData == null) { | ||
| 472 | + args.add(this.getDefaultValueByFieldType(itemField)); | ||
| 473 | + }else { | ||
| 474 | + args.add(itemData); | ||
| 475 | + } | ||
| 476 | + exeSql.append("?,"); | ||
| 477 | + } | ||
| 478 | + exeSql.setCharAt(exeSql.length()-1, ' '); | ||
| 479 | + exeSql.append("),"); | ||
| 467 | } | 480 | } |
| 481 | + exeSql.setCharAt(exeSql.length()-1, ';'); | ||
| 468 | 482 | ||
| 469 | //执行SQL | 483 | //执行SQL |
| 470 | - return jdbcTemplateWrite.batchUpdate(exeSql, batchArgs); | 484 | + this.jdbcTemplateWrite.update(exeSql.toString(), args.toArray()); |
| 485 | + | ||
| 486 | + //获取第一个ID | ||
| 487 | + return this.jdbcTemplateWrite.queryForObject("SELECT LAST_INSERT_ID();", BigInteger.class).intValue(); | ||
| 471 | } | 488 | } |
| 472 | 489 | ||
| 473 | private String constructUpdateSql(T entity, List<Field> beanFieldList) { | 490 | private String constructUpdateSql(T entity, List<Field> beanFieldList) { |
| 474 | StringBuffer sqlInsertPart = new StringBuffer("INSERT INTO "+this.getTableSql()+"("); | 491 | StringBuffer sqlInsertPart = new StringBuffer("INSERT INTO "+this.getTableSql()+"("); |
| 475 | - StringBuffer sqlColumnPart = new StringBuffer(") VALUES ("); | ||
| 476 | Iterator<String> beanFieldIter = this.beanToTableField.keySet().iterator(); | 492 | Iterator<String> beanFieldIter = this.beanToTableField.keySet().iterator(); |
| 477 | while(beanFieldIter.hasNext()){ | 493 | while(beanFieldIter.hasNext()){ |
| 478 | String beanFieldName = beanFieldIter.next(); | 494 | String beanFieldName = beanFieldIter.next(); |
| @@ -494,23 +510,8 @@ public class CustomJdbcTemplate<T, ID extends Serializable> { | @@ -494,23 +510,8 @@ public class CustomJdbcTemplate<T, ID extends Serializable> { | ||
| 494 | 510 | ||
| 495 | beanFieldList.add(beanField); | 511 | beanFieldList.add(beanField); |
| 496 | sqlInsertPart.append("`"+tableFieldName+"`,"); | 512 | sqlInsertPart.append("`"+tableFieldName+"`,"); |
| 497 | - sqlColumnPart.append(" ?,"); | ||
| 498 | - } | ||
| 499 | - return sqlInsertPart.substring(0, sqlInsertPart.length()-1)+sqlColumnPart.substring(0, sqlColumnPart.length()-1)+")"; | ||
| 500 | - } | ||
| 501 | - | ||
| 502 | - private Object[] getArgsByFieldAndEntity(T entity, List<Field> beanFieldList) throws IllegalArgumentException, IllegalAccessException { | ||
| 503 | - Object[] data = new Object[beanFieldList.size()]; | ||
| 504 | - for(int i=0; i<data.length; ++i) { | ||
| 505 | - Field itemField = beanFieldList.get(i); | ||
| 506 | - Object itemData = itemField.get(entity); | ||
| 507 | - if(itemData == null) { | ||
| 508 | - data[i] = this.getDefaultValueByFieldType(itemField); | ||
| 509 | - }else { | ||
| 510 | - data[i] = itemData; | ||
| 511 | - } | ||
| 512 | } | 513 | } |
| 513 | - return data; | 514 | + return sqlInsertPart.substring(0, sqlInsertPart.length()-1)+")"; |
| 514 | } | 515 | } |
| 515 | 516 | ||
| 516 | private Object getDefaultValueByFieldType(Field itemField) { | 517 | private Object getDefaultValueByFieldType(Field itemField) { |