Commit ba309f53be158f53666ca20e2d4c9f16348f49ab
1 parent
249f5861
Exists in
master
and in
2 other branches
1.优化批量添加的接口
Showing
2 changed files
with
77 additions
and
7 deletions
Show diff stats
build.gradle
src/main/java/com/taover/repository/CustomJdbcTemplate.java
| ... | ... | @@ -3,6 +3,7 @@ package com.taover.repository; |
| 3 | 3 | import java.io.Serializable; |
| 4 | 4 | import java.lang.reflect.Field; |
| 5 | 5 | import java.lang.reflect.ParameterizedType; |
| 6 | +import java.math.BigDecimal; | |
| 6 | 7 | import java.math.BigInteger; |
| 7 | 8 | import java.util.ArrayList; |
| 8 | 9 | import java.util.HashMap; |
| ... | ... | @@ -16,7 +17,6 @@ import javax.persistence.Id; |
| 16 | 17 | import javax.persistence.Table; |
| 17 | 18 | |
| 18 | 19 | import org.springframework.jdbc.core.JdbcTemplate; |
| 19 | -import org.springframework.transaction.annotation.Transactional; | |
| 20 | 20 | |
| 21 | 21 | /** |
| 22 | 22 | * |
| ... | ... | @@ -449,15 +449,85 @@ public class CustomJdbcTemplate<T, ID extends Serializable> { |
| 449 | 449 | |
| 450 | 450 | /** |
| 451 | 451 | * 批量添加 |
| 452 | + * @throws Exception | |
| 452 | 453 | */ |
| 453 | - public List<BigInteger> addEntityList(List<T> entityList) { | |
| 454 | - List<BigInteger> result = new ArrayList<BigInteger>(); | |
| 455 | - for (T entity : entityList) { | |
| 456 | - result.add(addEntityForAutoincrementId(entity)); | |
| 454 | + public int[] addEntityList(List<T> entityList) throws Exception { | |
| 455 | + if(entityList == null || entityList.isEmpty()) { | |
| 456 | + throw new Exception("entitylist is empty or null"); | |
| 457 | 457 | } |
| 458 | - return result; | |
| 458 | + //构造SQL语句及Entity Field列表 | |
| 459 | + List<Field> beanFieldList = new ArrayList<Field>(this.beanToTableField.size()); | |
| 460 | + String exeSql = this.constructUpdateSql(entityList.get(0), beanFieldList); | |
| 461 | + System.out.println("exeSql >>>> "+exeSql); | |
| 462 | + | |
| 463 | + //构造参数信息 | |
| 464 | + List<Object[]> batchArgs = new ArrayList<Object[]>(); | |
| 465 | + for(T item: entityList) { | |
| 466 | + Object[] itemData = this.getArgsByFieldAndEntity(item, beanFieldList); | |
| 467 | + System.out.println("exeData >>>> "+itemData); | |
| 468 | + batchArgs.add(itemData); | |
| 469 | + } | |
| 470 | + | |
| 471 | + //执行SQL | |
| 472 | + return jdbcTemplateWrite.batchUpdate(exeSql, batchArgs); | |
| 459 | 473 | } |
| 460 | 474 | |
| 475 | + private String constructUpdateSql(T entity, List<Field> beanFieldList) { | |
| 476 | + StringBuffer sqlInsertPart = new StringBuffer("INSERT INTO "+this.getTableSql()+"("); | |
| 477 | + StringBuffer sqlColumnPart = new StringBuffer(") VALUES ("); | |
| 478 | + Iterator<String> beanFieldIter = this.beanToTableField.keySet().iterator(); | |
| 479 | + while(beanFieldIter.hasNext()){ | |
| 480 | + String beanFieldName = beanFieldIter.next(); | |
| 481 | + String tableFieldName = this.beanToTableField.get(beanFieldName); | |
| 482 | + Field beanField = null; | |
| 483 | + try { | |
| 484 | + beanField = this.tClassInfo.getDeclaredField(beanFieldName); | |
| 485 | + beanField.setAccessible(true); | |
| 486 | + if(beanField.get(entity) == null) { | |
| 487 | + continue; | |
| 488 | + } | |
| 489 | + } catch (Exception e) { | |
| 490 | + continue; | |
| 491 | + } | |
| 492 | + | |
| 493 | + if(tableFieldName == null || beanFieldName == null){ | |
| 494 | + continue; | |
| 495 | + } | |
| 496 | + | |
| 497 | + beanFieldList.add(beanField); | |
| 498 | + sqlInsertPart.append("`"+tableFieldName+"`,"); | |
| 499 | + sqlColumnPart.append(" ?,"); | |
| 500 | + } | |
| 501 | + return sqlInsertPart.substring(0, sqlInsertPart.length()-1)+sqlColumnPart.substring(0, sqlColumnPart.length()-1)+")"; | |
| 502 | + } | |
| 503 | + | |
| 504 | + private Object[] getArgsByFieldAndEntity(T entity, List<Field> beanFieldList) throws IllegalArgumentException, IllegalAccessException { | |
| 505 | + Object[] data = new Object[beanFieldList.size()]; | |
| 506 | + for(int i=0; i<data.length; ++i) { | |
| 507 | + Field itemField = beanFieldList.get(i); | |
| 508 | + Object itemData = itemField.get(entity); | |
| 509 | + if(itemData == null) { | |
| 510 | + data[i] = this.getDefaultValueByFieldType(itemField); | |
| 511 | + }else { | |
| 512 | + data[i] = itemData; | |
| 513 | + } | |
| 514 | + } | |
| 515 | + return data; | |
| 516 | + } | |
| 517 | + | |
| 518 | + private Object getDefaultValueByFieldType(Field itemField) { | |
| 519 | + String simpleName = itemField.getType().getSimpleName(); | |
| 520 | + if("String".equals(simpleName)) { | |
| 521 | + return ""; | |
| 522 | + }else if("Date".equals(simpleName) || "Timestamp".equals(simpleName)) { | |
| 523 | + return "2000-01-01 00:00:00"; | |
| 524 | + }else if("BigDecimal".equals(simpleName)){ | |
| 525 | + return BigDecimal.ZERO; | |
| 526 | + }else { | |
| 527 | + return 0; | |
| 528 | + } | |
| 529 | + } | |
| 530 | + | |
| 461 | 531 | /** |
| 462 | 532 | * 按ID删除 |
| 463 | 533 | */ | ... | ... |