Commit 9a55aef101dc0ce371b89a3c5a6c25105be0337d
1 parent
0f129a61
Exists in
master
optimized update by id list
Showing
3 changed files
with
30 additions
and
9 deletions
 
Show diff stats
build.gradle
| @@ -56,7 +56,7 @@ uploadArchives { | @@ -56,7 +56,7 @@ uploadArchives { | ||
| 56 | authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD) | 56 | authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD) | 
| 57 | } | 57 | } | 
| 58 | pom.project { | 58 | pom.project { | 
| 59 | - version '2.3.4' | 59 | + version '2.3.6' | 
| 60 | artifactId ARTIFACT_Id | 60 | artifactId ARTIFACT_Id | 
| 61 | groupId GROUP_ID | 61 | groupId GROUP_ID | 
| 62 | packaging TYPE | 62 | packaging TYPE | 
src/main/java/com/taover/repository/CustomJdbcTemplateWrapperTenant.java
| @@ -6,6 +6,7 @@ import java.sql.Connection; | @@ -6,6 +6,7 @@ import java.sql.Connection; | ||
| 6 | import java.sql.PreparedStatement; | 6 | import java.sql.PreparedStatement; | 
| 7 | import java.sql.SQLException; | 7 | import java.sql.SQLException; | 
| 8 | import java.util.ArrayList; | 8 | import java.util.ArrayList; | 
| 9 | +import java.util.Collection; | ||
| 9 | import java.util.Collections; | 10 | import java.util.Collections; | 
| 10 | import java.util.HashMap; | 11 | import java.util.HashMap; | 
| 11 | import java.util.Iterator; | 12 | import java.util.Iterator; | 
| @@ -47,6 +48,8 @@ public class CustomJdbcTemplateWrapperTenant<T, ID extends Number> implements Cu | @@ -47,6 +48,8 @@ public class CustomJdbcTemplateWrapperTenant<T, ID extends Number> implements Cu | ||
| 47 | @Resource | 48 | @Resource | 
| 48 | private JdbcTemplateWrapperTenant _jdbcTemplateWrapperTenant; | 49 | private JdbcTemplateWrapperTenant _jdbcTemplateWrapperTenant; | 
| 49 | 50 | ||
| 51 | + private static int DEFAULT_BATCH_SIZE_WHEN_UPDATE_BY_IDLIST = 100; | ||
| 52 | + | ||
| 50 | private Map<String, String> _beanToTableField; | 53 | private Map<String, String> _beanToTableField; | 
| 51 | private Map<String, String> _tableToBeanField; | 54 | private Map<String, String> _tableToBeanField; | 
| 52 | private String _tableFieldNameListGapWithComma; | 55 | private String _tableFieldNameListGapWithComma; | 
| @@ -557,13 +560,16 @@ public class CustomJdbcTemplateWrapperTenant<T, ID extends Number> implements Cu | @@ -557,13 +560,16 @@ public class CustomJdbcTemplateWrapperTenant<T, ID extends Number> implements Cu | ||
| 557 | } | 560 | } | 
| 558 | 561 | ||
| 559 | @Override | 562 | @Override | 
| 560 | - public void updateEntityByIdList(List<Object[]> changeList, List<ID> idList, Long shardingKey) { | 563 | + public void updateEntityByIdList(List<Object[]> changeList, Collection<ID> idList, Integer batchSize, Long shardingKey) { | 
| 561 | if(null == idList){ | 564 | if(null == idList){ | 
| 562 | throw new RuntimeException("params[idList] is null"); | 565 | throw new RuntimeException("params[idList] is null"); | 
| 563 | } | 566 | } | 
| 564 | if (null == changeList || changeList.size() == 0) { | 567 | if (null == changeList || changeList.size() == 0) { | 
| 565 | throw new RuntimeException("params[changeList] is empty"); | 568 | throw new RuntimeException("params[changeList] is empty"); | 
| 566 | } | 569 | } | 
| 570 | + if(batchSize == null) { | ||
| 571 | + batchSize = DEFAULT_BATCH_SIZE_WHEN_UPDATE_BY_IDLIST; | ||
| 572 | + } | ||
| 567 | 573 | ||
| 568 | StringBuffer sql = new StringBuffer("UPDATE "+this.getTableSql()+" SET"); | 574 | StringBuffer sql = new StringBuffer("UPDATE "+this.getTableSql()+" SET"); | 
| 569 | StringBuffer pql = new StringBuffer(sql.toString()); | 575 | StringBuffer pql = new StringBuffer(sql.toString()); | 
| @@ -571,15 +577,15 @@ public class CustomJdbcTemplateWrapperTenant<T, ID extends Number> implements Cu | @@ -571,15 +577,15 @@ public class CustomJdbcTemplateWrapperTenant<T, ID extends Number> implements Cu | ||
| 571 | this.appendSetSql(sql, pql, list, changeList); | 577 | this.appendSetSql(sql, pql, list, changeList); | 
| 572 | String updateAndFromSql = sql.substring(0, sql.length()-1); | 578 | String updateAndFromSql = sql.substring(0, sql.length()-1); | 
| 573 | 579 | ||
| 574 | - int idIndex = 0; | ||
| 575 | - int idListSize = idList.size(); | 580 | + Iterator<ID> idIter = idList.iterator(); | 
| 581 | + int idCount = 0; | ||
| 576 | StringBuffer idSb = new StringBuffer(); | 582 | StringBuffer idSb = new StringBuffer(); | 
| 577 | - while(idIndex < idListSize) { | ||
| 578 | - idSb.append(idList.get(idIndex)+","); | ||
| 579 | - ++idIndex; | 583 | + while(idIter.hasNext()) { | 
| 584 | + idSb.append(idIter.next()+","); | ||
| 585 | + ++idCount; | ||
| 580 | 586 | ||
| 581 | //满100条则向mysql发送更新请求 | 587 | //满100条则向mysql发送更新请求 | 
| 582 | - if(idIndex%100 == 0) { | 588 | + if(idCount%batchSize == 0) { | 
| 583 | String whereSql = " WHERE "+this.appendWhereSqlWithShardingCondition(this._idTableFieldName+" in ("+idSb.toString().substring(0, idSb.length()-1)+") ", shardingKey); | 589 | String whereSql = " WHERE "+this.appendWhereSqlWithShardingCondition(this._idTableFieldName+" in ("+idSb.toString().substring(0, idSb.length()-1)+") ", shardingKey); | 
| 584 | this._jdbcTemplateWrapperTenant.update(updateAndFromSql+whereSql, shardingKey, list.toArray()); | 590 | this._jdbcTemplateWrapperTenant.update(updateAndFromSql+whereSql, shardingKey, list.toArray()); | 
| 585 | idSb.setLength(0); | 591 | idSb.setLength(0); | 
| @@ -592,6 +598,11 @@ public class CustomJdbcTemplateWrapperTenant<T, ID extends Number> implements Cu | @@ -592,6 +598,11 @@ public class CustomJdbcTemplateWrapperTenant<T, ID extends Number> implements Cu | ||
| 592 | this._jdbcTemplateWrapperTenant.update(updateAndFromSql+whereSql, shardingKey, list.toArray()); | 598 | this._jdbcTemplateWrapperTenant.update(updateAndFromSql+whereSql, shardingKey, list.toArray()); | 
| 593 | } | 599 | } | 
| 594 | } | 600 | } | 
| 601 | + | ||
| 602 | + @Override | ||
| 603 | + public void updateEntityByIdList(List<Object[]> changeList, Collection<ID> idList, Long shardingKey) { | ||
| 604 | + this.updateEntityByIdList(changeList, idList, DEFAULT_BATCH_SIZE_WHEN_UPDATE_BY_IDLIST, shardingKey); | ||
| 605 | + } | ||
| 595 | 606 | ||
| 596 | @Override | 607 | @Override | 
| 597 | public int updateEntityByCondition(List<Object[]> updateObj, List<Object[]> condition, Long shardingKey){ | 608 | public int updateEntityByCondition(List<Object[]> updateObj, List<Object[]> condition, Long shardingKey){ | 
src/main/java/com/taover/repository/CustomJdbcTemplateWrapperTenantInterface.java
| 1 | package com.taover.repository; | 1 | package com.taover.repository; | 
| 2 | 2 | ||
| 3 | +import java.util.Collection; | ||
| 3 | import java.util.List; | 4 | import java.util.List; | 
| 4 | import java.util.Map; | 5 | import java.util.Map; | 
| 5 | 6 | ||
| @@ -123,7 +124,16 @@ public interface CustomJdbcTemplateWrapperTenantInterface<T, ID extends Number> | @@ -123,7 +124,16 @@ public interface CustomJdbcTemplateWrapperTenantInterface<T, ID extends Number> | ||
| 123 | * @param tenantId | 124 | * @param tenantId | 
| 124 | * @return | 125 | * @return | 
| 125 | */ | 126 | */ | 
| 126 | - public void updateEntityByIdList(List<Object[]> changeList, List<ID> idList, Long tenantId); | 127 | + public void updateEntityByIdList(List<Object[]> changeList, Collection<ID> idList, Long tenantId); | 
| 128 | + | ||
| 129 | + /** | ||
| 130 | + * 根据ID列表修改指定数据 | ||
| 131 | + * @param changeList | ||
| 132 | + * @param idList | ||
| 133 | + * @param batchSize | ||
| 134 | + * @param tenantId | ||
| 135 | + */ | ||
| 136 | + public void updateEntityByIdList(List<Object[]> changeList, Collection<ID> idList, Integer batchSize, Long tenantId); | ||
| 127 | 137 | ||
| 128 | /** | 138 | /** | 
| 129 | * List<Object[]> updateObj 要修改成的值,数组长度为2,第一个值为列名,第二个值是要改成的值。 | 139 | * List<Object[]> updateObj 要修改成的值,数组长度为2,第一个值为列名,第二个值是要改成的值。 |