Commit 5212255525771956718eabdda63858a358caeede
1 parent
d7fc41cc
Exists in
master
add support update by id list
Showing
7 changed files
with
64 additions
and
9 deletions
Show diff stats
build.gradle
src/main/java/com/taover/repository/CustomJdbcTemplateWrapperTenant.java
| ... | ... | @@ -555,6 +555,43 @@ public class CustomJdbcTemplateWrapperTenant<T, ID extends Number> implements Cu |
| 555 | 555 | list.add(id); |
| 556 | 556 | return _jdbcTemplateWrapperTenant.update(updateSql, shardingKey, list.toArray()); |
| 557 | 557 | } |
| 558 | + | |
| 559 | + @Override | |
| 560 | + public void updateEntityByIdList(List<Object[]> changeList, List<ID> idList, Long shardingKey) { | |
| 561 | + if(null == idList){ | |
| 562 | + throw new RuntimeException("params[idList] is null"); | |
| 563 | + } | |
| 564 | + if (null == changeList || changeList.size() == 0) { | |
| 565 | + throw new RuntimeException("params[changeList] is empty"); | |
| 566 | + } | |
| 567 | + | |
| 568 | + StringBuffer sql = new StringBuffer("UPDATE "+this.getTableSql()+" SET"); | |
| 569 | + StringBuffer pql = new StringBuffer(sql.toString()); | |
| 570 | + List<Object> list = new ArrayList<Object>(); | |
| 571 | + this.appendSetSql(sql, pql, list, changeList); | |
| 572 | + String updateAndFromSql = sql.substring(0, sql.length()-1); | |
| 573 | + | |
| 574 | + int idIndex = 0; | |
| 575 | + int idListSize = idList.size(); | |
| 576 | + StringBuffer idSb = new StringBuffer(); | |
| 577 | + while(idIndex < idListSize) { | |
| 578 | + idSb.append(idList.get(idIndex)+","); | |
| 579 | + ++idIndex; | |
| 580 | + | |
| 581 | + //满100条则向mysql发送更新请求 | |
| 582 | + if(idIndex%100 == 0) { | |
| 583 | + 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()); | |
| 585 | + idSb.setLength(0); | |
| 586 | + } | |
| 587 | + } | |
| 588 | + | |
| 589 | + //是否剩余id列表 | |
| 590 | + if(idSb.length() > 0) { | |
| 591 | + String whereSql = " WHERE "+this.appendWhereSqlWithShardingCondition(this._idTableFieldName+" in ("+idSb.toString().substring(0, idSb.length()-1)+") ", shardingKey); | |
| 592 | + this._jdbcTemplateWrapperTenant.update(updateAndFromSql+whereSql, shardingKey, list.toArray()); | |
| 593 | + } | |
| 594 | + } | |
| 558 | 595 | |
| 559 | 596 | @Override |
| 560 | 597 | public int updateEntityByCondition(List<Object[]> updateObj, List<Object[]> condition, Long shardingKey){ | ... | ... |
src/main/java/com/taover/repository/CustomJdbcTemplateWrapperTenantInterface.java
| ... | ... | @@ -115,6 +115,15 @@ public interface CustomJdbcTemplateWrapperTenantInterface<T, ID extends Number> |
| 115 | 115 | * 根据ID修改指定的值 |
| 116 | 116 | */ |
| 117 | 117 | public int updateEntityById(List<Object[]> changeList, ID id, Long tenantId); |
| 118 | + | |
| 119 | + /** | |
| 120 | + * 根据ID列表修改指定数据 | |
| 121 | + * @param changeList | |
| 122 | + * @param idList | |
| 123 | + * @param tenantId | |
| 124 | + * @return | |
| 125 | + */ | |
| 126 | + public void updateEntityByIdList(List<Object[]> changeList, List<ID> idList, Long tenantId); | |
| 118 | 127 | |
| 119 | 128 | /** |
| 120 | 129 | * List<Object[]> updateObj 要修改成的值,数组长度为2,第一个值为列名,第二个值是要改成的值。 | ... | ... |
src/main/java/com/taover/repository/exception/NotFoundException.java
| 1 | 1 | package com.taover.repository.exception; |
| 2 | 2 | |
| 3 | 3 | public class NotFoundException extends Exception { |
| 4 | - private Exception originError; | |
| 5 | - | |
| 4 | + private static final long serialVersionUID = 1L; | |
| 5 | + | |
| 6 | 6 | public NotFoundException() {} |
| 7 | 7 | |
| 8 | 8 | public NotFoundException(Exception e) { |
| 9 | - this.originError = e; | |
| 9 | + super(e); | |
| 10 | 10 | } |
| 11 | 11 | } | ... | ... |
src/main/java/com/taover/repository/exception/SqlParsePagerException.java
| 1 | 1 | package com.taover.repository.exception; |
| 2 | 2 | |
| 3 | 3 | public class SqlParsePagerException extends RuntimeException{ |
| 4 | - private String msg = null; | |
| 5 | - | |
| 4 | + private static final long serialVersionUID = 1L; | |
| 5 | + | |
| 6 | 6 | public SqlParsePagerException(String msg) { |
| 7 | - this.msg = msg; | |
| 7 | + super(msg); | |
| 8 | 8 | } |
| 9 | 9 | |
| 10 | 10 | } | ... | ... |
src/test/java/com/taover/repository/test/TestMapper.java
| 1 | 1 | package com.taover.repository.test; |
| 2 | 2 | |
| 3 | +import java.util.ArrayList; | |
| 4 | +import java.util.List; | |
| 3 | 5 | import java.util.Properties; |
| 4 | 6 | |
| 5 | 7 | import javax.sql.DataSource; |
| ... | ... | @@ -25,7 +27,14 @@ public class TestMapper { |
| 25 | 27 | JdbcTemplateWrapperTenant template = new JdbcTemplateWrapperTenantImpl(new JdbcTemplate(ds)); |
| 26 | 28 | //System.out.println(template.queryForObject("select count(*) from wxorder_order", Integer.class)); |
| 27 | 29 | CommonRegionRepository repo = new CommonRegionRepository(template); |
| 28 | - System.out.println(repo.findListBySql("1=1", 1L)); | |
| 30 | + //System.out.println(repo.findListBySql("1=1", 1L)); | |
| 31 | + List<Object[]> changeList = new ArrayList<Object[]>(); | |
| 32 | + changeList.add(new Object[] {"large_area", "1"}); | |
| 33 | + List<Long> data = new ArrayList<Long>(); | |
| 34 | + data.add(12L); | |
| 35 | + data.add(14L); | |
| 36 | + data.add(33L); | |
| 37 | + repo.updateEntityByIdList(changeList, data, null); | |
| 29 | 38 | } catch (Exception e) { |
| 30 | 39 | // TODO Auto-generated catch block |
| 31 | 40 | e.printStackTrace(); | ... | ... |
src/test/java/com/taover/repository/test/repository/CommonRegionEntity.java
| ... | ... | @@ -16,7 +16,7 @@ import com.taover.repository.annotation.TableShardingType; |
| 16 | 16 | */ |
| 17 | 17 | @Entity |
| 18 | 18 | @Table(name="common_region", catalog="") |
| 19 | -@TableSharding(type=TableShardingType.MULTI_DB_MULTI_TABLE, column="id") | |
| 19 | +//@TableSharding(type=TableShardingType.MULTI_DB_MULTI_TABLE, column="id") | |
| 20 | 20 | public class CommonRegionEntity implements Serializable { |
| 21 | 21 | |
| 22 | 22 | private static final long serialVersionUID = 1L; | ... | ... |