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
src/main/java/com/taover/repository/CustomJdbcTemplateWrapperTenant.java
| ... | ... | @@ -6,6 +6,7 @@ import java.sql.Connection; | 
| 6 | 6 | import java.sql.PreparedStatement; | 
| 7 | 7 | import java.sql.SQLException; | 
| 8 | 8 | import java.util.ArrayList; | 
| 9 | +import java.util.Collection; | |
| 9 | 10 | import java.util.Collections; | 
| 10 | 11 | import java.util.HashMap; | 
| 11 | 12 | import java.util.Iterator; | 
| ... | ... | @@ -47,6 +48,8 @@ public class CustomJdbcTemplateWrapperTenant<T, ID extends Number> implements Cu | 
| 47 | 48 | @Resource | 
| 48 | 49 | private JdbcTemplateWrapperTenant _jdbcTemplateWrapperTenant; | 
| 49 | 50 | |
| 51 | + private static int DEFAULT_BATCH_SIZE_WHEN_UPDATE_BY_IDLIST = 100; | |
| 52 | + | |
| 50 | 53 | private Map<String, String> _beanToTableField; | 
| 51 | 54 | private Map<String, String> _tableToBeanField; | 
| 52 | 55 | private String _tableFieldNameListGapWithComma; | 
| ... | ... | @@ -557,13 +560,16 @@ public class CustomJdbcTemplateWrapperTenant<T, ID extends Number> implements Cu | 
| 557 | 560 | } | 
| 558 | 561 | |
| 559 | 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 | 564 | if(null == idList){ | 
| 562 | 565 | throw new RuntimeException("params[idList] is null"); | 
| 563 | 566 | } | 
| 564 | 567 | if (null == changeList || changeList.size() == 0) { | 
| 565 | 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 | 574 | StringBuffer sql = new StringBuffer("UPDATE "+this.getTableSql()+" SET"); | 
| 569 | 575 | StringBuffer pql = new StringBuffer(sql.toString()); | 
| ... | ... | @@ -571,15 +577,15 @@ public class CustomJdbcTemplateWrapperTenant<T, ID extends Number> implements Cu | 
| 571 | 577 | this.appendSetSql(sql, pql, list, changeList); | 
| 572 | 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 | 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 | 587 | //满100条则向mysql发送更新请求 | 
| 582 | - if(idIndex%100 == 0) { | |
| 588 | + if(idCount%batchSize == 0) { | |
| 583 | 589 | String whereSql = " WHERE "+this.appendWhereSqlWithShardingCondition(this._idTableFieldName+" in ("+idSb.toString().substring(0, idSb.length()-1)+") ", shardingKey); | 
| 584 | 590 | this._jdbcTemplateWrapperTenant.update(updateAndFromSql+whereSql, shardingKey, list.toArray()); | 
| 585 | 591 | idSb.setLength(0); | 
| ... | ... | @@ -592,6 +598,11 @@ public class CustomJdbcTemplateWrapperTenant<T, ID extends Number> implements Cu | 
| 592 | 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 | 607 | @Override | 
| 597 | 608 | public int updateEntityByCondition(List<Object[]> updateObj, List<Object[]> condition, Long shardingKey){ | ... | ... | 
src/main/java/com/taover/repository/CustomJdbcTemplateWrapperTenantInterface.java
| 1 | 1 | package com.taover.repository; | 
| 2 | 2 | |
| 3 | +import java.util.Collection; | |
| 3 | 4 | import java.util.List; | 
| 4 | 5 | import java.util.Map; | 
| 5 | 6 | |
| ... | ... | @@ -123,7 +124,16 @@ public interface CustomJdbcTemplateWrapperTenantInterface<T, ID extends Number> | 
| 123 | 124 | * @param tenantId | 
| 124 | 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 | 139 | * List<Object[]> updateObj 要修改成的值,数组长度为2,第一个值为列名,第二个值是要改成的值。 | ... | ... |