Commit 9fee20377e300f5a29515cf409a60d32487afae0
1 parent
362d2827
Exists in
master
and in
2 other branches
support sharding database
Showing
15 changed files
with
233 additions
and
187 deletions
Show diff stats
build.gradle
| @@ -57,7 +57,7 @@ uploadArchives { | @@ -57,7 +57,7 @@ uploadArchives { | ||
| 57 | authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD) | 57 | authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD) |
| 58 | } | 58 | } |
| 59 | pom.project { | 59 | pom.project { |
| 60 | - version '2.1.46' | 60 | + version '2.1.48' |
| 61 | artifactId ARTIFACT_Id | 61 | artifactId ARTIFACT_Id |
| 62 | groupId GROUP_ID | 62 | groupId GROUP_ID |
| 63 | packaging TYPE | 63 | packaging TYPE |
src/main/java/com/taover/repository/CustomJdbcTemplate.java
| @@ -18,6 +18,7 @@ import javax.persistence.Table; | @@ -18,6 +18,7 @@ import javax.persistence.Table; | ||
| 18 | 18 | ||
| 19 | import org.springframework.jdbc.core.JdbcTemplate; | 19 | import org.springframework.jdbc.core.JdbcTemplate; |
| 20 | 20 | ||
| 21 | +import com.taover.repository.mapper.CustomJdbcTemplateRowMapper; | ||
| 21 | import com.taover.repository.util.UtilsSql; | 22 | import com.taover.repository.util.UtilsSql; |
| 22 | 23 | ||
| 23 | /** | 24 | /** |
src/main/java/com/taover/repository/CustomJdbcTemplateRowMapper.java
| @@ -1,119 +0,0 @@ | @@ -1,119 +0,0 @@ | ||
| 1 | -package com.taover.repository; | ||
| 2 | - | ||
| 3 | -import java.lang.reflect.Field; | ||
| 4 | -import java.lang.reflect.Method; | ||
| 5 | -import java.sql.ResultSet; | ||
| 6 | -import java.sql.SQLException; | ||
| 7 | -import java.util.HashMap; | ||
| 8 | -import java.util.Map; | ||
| 9 | - | ||
| 10 | -import org.apache.commons.logging.Log; | ||
| 11 | -import org.apache.commons.logging.LogFactory; | ||
| 12 | -import org.springframework.jdbc.core.RowMapper; | ||
| 13 | -import org.springframework.jdbc.support.rowset.ResultSetWrappingSqlRowSetMetaData; | ||
| 14 | - | ||
| 15 | -public class CustomJdbcTemplateRowMapper <E> implements RowMapper<E>{ | ||
| 16 | - Log log = LogFactory.getLog(this.getClass()); | ||
| 17 | - | ||
| 18 | - private Map<String, Field> beanFielNameToField = new HashMap<String, Field>(); | ||
| 19 | - private Method beforeMethod = null; | ||
| 20 | - private Method afterMethod = null; | ||
| 21 | - | ||
| 22 | - private Class<E> classInfo; | ||
| 23 | - private Map<String, String> tableToBeanMap; | ||
| 24 | - | ||
| 25 | - public CustomJdbcTemplateRowMapper(Class<E> classInfo, Map<String, String> tableToBeanMap) { | ||
| 26 | - this.classInfo = classInfo; | ||
| 27 | - this.tableToBeanMap = tableToBeanMap; | ||
| 28 | - | ||
| 29 | - Field[] fields = this.classInfo.getDeclaredFields(); | ||
| 30 | - for(Field item: fields) { | ||
| 31 | - try { | ||
| 32 | - item.setAccessible(true); | ||
| 33 | - this.beanFielNameToField.put(item.getName(), item); | ||
| 34 | - }catch (Exception e) { | ||
| 35 | - log.error("set field accessible:"+e.getMessage()); | ||
| 36 | - } | ||
| 37 | - } | ||
| 38 | - | ||
| 39 | - boolean hasImplementPointCut = false; | ||
| 40 | - Class[] interfaceArr = this.classInfo.getInterfaces(); | ||
| 41 | - for(int i=0; i<interfaceArr.length; ++i){ | ||
| 42 | - if(interfaceArr[i].getName().equals("com.taover.repository.EntityPointCut")){ | ||
| 43 | - hasImplementPointCut = true; | ||
| 44 | - break; | ||
| 45 | - } | ||
| 46 | - } | ||
| 47 | - | ||
| 48 | - if(hasImplementPointCut){ | ||
| 49 | - try{ | ||
| 50 | - beforeMethod = this.classInfo.getDeclaredMethod("before"); | ||
| 51 | - beforeMethod.setAccessible(true); | ||
| 52 | - }catch(Exception e){ | ||
| 53 | - log.error("set before method exception:"+e.getMessage()); | ||
| 54 | - } | ||
| 55 | - | ||
| 56 | - try{ | ||
| 57 | - afterMethod = this.classInfo.getDeclaredMethod("after"); | ||
| 58 | - afterMethod.setAccessible(true); | ||
| 59 | - }catch(Exception e){ | ||
| 60 | - log.error("set after method exception:"+e.getMessage()); | ||
| 61 | - } | ||
| 62 | - } | ||
| 63 | - } | ||
| 64 | - | ||
| 65 | - @Override | ||
| 66 | - public E mapRow(ResultSet rs, int index) throws SQLException { | ||
| 67 | - E targetObj; | ||
| 68 | - try { | ||
| 69 | - targetObj = this.classInfo.newInstance(); | ||
| 70 | - } catch (Exception e) { | ||
| 71 | - throw new RuntimeException(e); | ||
| 72 | - } | ||
| 73 | - | ||
| 74 | - if(this.beforeMethod != null){ | ||
| 75 | - try{ | ||
| 76 | - beforeMethod.invoke(targetObj); | ||
| 77 | - }catch(Exception e){ | ||
| 78 | - log.error("invoke before exception:"+e.getMessage()); | ||
| 79 | - } | ||
| 80 | - } | ||
| 81 | - | ||
| 82 | - ResultSetWrappingSqlRowSetMetaData wapping = new ResultSetWrappingSqlRowSetMetaData(rs.getMetaData()); | ||
| 83 | - int columnCount = wapping.getColumnCount(); | ||
| 84 | - for (int i = 1; i<=columnCount; i++) { | ||
| 85 | - String tableFieldName = wapping.getColumnLabel(i); | ||
| 86 | - String beanFieldName = this.tableToBeanMap.get(tableFieldName); | ||
| 87 | - Object value = rs.getObject(i); | ||
| 88 | - Field beanField = this.beanFielNameToField.get(beanFieldName); | ||
| 89 | - if(null == value || beanFieldName == null && beanField == null){ | ||
| 90 | - continue; | ||
| 91 | - } | ||
| 92 | - try { | ||
| 93 | - beanField.set(targetObj, value); | ||
| 94 | - } catch (IllegalArgumentException e) { | ||
| 95 | - if("Integer".equals(value.getClass().getSimpleName()) && "Long".equals(beanField.getType().getSimpleName())) { | ||
| 96 | - try { | ||
| 97 | - beanField.set(targetObj, Long.valueOf(value.toString())); | ||
| 98 | - } catch (IllegalArgumentException | IllegalAccessException e1) { | ||
| 99 | - log.error("map set object field error -> Integer convert Long:"+e.getMessage()); | ||
| 100 | - } | ||
| 101 | - }else { | ||
| 102 | - log.error("map set object field error:"+e.getMessage()); | ||
| 103 | - } | ||
| 104 | - } catch (IllegalAccessException e) { | ||
| 105 | - log.error("map set object field error:"+e.getMessage()); | ||
| 106 | - } | ||
| 107 | - } | ||
| 108 | - | ||
| 109 | - if(this.afterMethod != null){ | ||
| 110 | - try { | ||
| 111 | - afterMethod.invoke(targetObj); | ||
| 112 | - }catch (Exception e) { | ||
| 113 | - log.error("invoke after exception:"+e.getMessage()); | ||
| 114 | - } | ||
| 115 | - } | ||
| 116 | - | ||
| 117 | - return targetObj; | ||
| 118 | - } | ||
| 119 | -} |
src/main/java/com/taover/repository/CustomJdbcTemplateWrapperTenant.java
| @@ -22,6 +22,7 @@ import com.taover.repository.exception.NoContainTenantException; | @@ -22,6 +22,7 @@ import com.taover.repository.exception.NoContainTenantException; | ||
| 22 | import com.taover.repository.exception.NotFoundException; | 22 | import com.taover.repository.exception.NotFoundException; |
| 23 | import com.taover.repository.exception.ObjectReflectException; | 23 | import com.taover.repository.exception.ObjectReflectException; |
| 24 | import com.taover.repository.jdbctemplate.JdbcTemplateWrapperTenant; | 24 | import com.taover.repository.jdbctemplate.JdbcTemplateWrapperTenant; |
| 25 | +import com.taover.repository.mapper.CustomJdbcTemplateRowMapper; | ||
| 25 | import com.taover.repository.util.UtilsSql; | 26 | import com.taover.repository.util.UtilsSql; |
| 26 | 27 | ||
| 27 | /** | 28 | /** |
src/main/java/com/taover/repository/EntityPointCut.java
src/main/java/com/taover/repository/advice/EntityPointCut.java
0 → 100644
src/main/java/com/taover/repository/jdbctemplate/JdbcTemplateWrapperTenantImpl.java
| @@ -3,7 +3,6 @@ package com.taover.repository.jdbctemplate; | @@ -3,7 +3,6 @@ package com.taover.repository.jdbctemplate; | ||
| 3 | import java.util.Collection; | 3 | import java.util.Collection; |
| 4 | import java.util.List; | 4 | import java.util.List; |
| 5 | import java.util.Map; | 5 | import java.util.Map; |
| 6 | -import java.util.Map.Entry; | ||
| 7 | 6 | ||
| 8 | import javax.annotation.Resource; | 7 | import javax.annotation.Resource; |
| 9 | 8 | ||
| @@ -26,6 +25,7 @@ import org.springframework.jdbc.core.StatementCallback; | @@ -26,6 +25,7 @@ import org.springframework.jdbc.core.StatementCallback; | ||
| 26 | import org.springframework.jdbc.support.KeyHolder; | 25 | import org.springframework.jdbc.support.KeyHolder; |
| 27 | import org.springframework.jdbc.support.rowset.SqlRowSet; | 26 | import org.springframework.jdbc.support.rowset.SqlRowSet; |
| 28 | 27 | ||
| 28 | +import com.taover.repository.shardingsphere.ShardingInfoEntity; | ||
| 29 | import com.taover.repository.shardingsphere.ShardingSphereService; | 29 | import com.taover.repository.shardingsphere.ShardingSphereService; |
| 30 | 30 | ||
| 31 | public class JdbcTemplateWrapperTenantImpl implements JdbcTemplateWrapperTenant { | 31 | public class JdbcTemplateWrapperTenantImpl implements JdbcTemplateWrapperTenant { |
| @@ -35,13 +35,15 @@ public class JdbcTemplateWrapperTenantImpl implements JdbcTemplateWrapperTenant | @@ -35,13 +35,15 @@ public class JdbcTemplateWrapperTenantImpl implements JdbcTemplateWrapperTenant | ||
| 35 | private ShardingSphereService shardingSphereService; | 35 | private ShardingSphereService shardingSphereService; |
| 36 | 36 | ||
| 37 | private void loadShardingInfo(Long tenantId) { | 37 | private void loadShardingInfo(Long tenantId) { |
| 38 | - Map<String, String> shardingInfo = this.shardingSphereService.getShardingInfoMapByTenantId(tenantId); | 38 | + List<ShardingInfoEntity> shardingInfo = this.shardingSphereService.getShardingInfoMapByTenantId(tenantId); |
| 39 | if(shardingInfo == null || shardingInfo.isEmpty()) { | 39 | if(shardingInfo == null || shardingInfo.isEmpty()) { |
| 40 | return; | 40 | return; |
| 41 | } | 41 | } |
| 42 | HintManager.clear(); | 42 | HintManager.clear(); |
| 43 | - for(Entry<String, String> item: shardingInfo.entrySet()) { | ||
| 44 | - HintManager.getInstance().addTableShardingValue(item.getKey(), item.getValue()); | 43 | + HintManager instance = HintManager.getInstance(); |
| 44 | + for(ShardingInfoEntity item: shardingInfo) { | ||
| 45 | + instance.addTableShardingValue(item.getTableName(), item.getTableSuffix()); | ||
| 46 | + instance.addDatabaseShardingValue(item.getTableName(), item.getDsName()); | ||
| 45 | } | 47 | } |
| 46 | } | 48 | } |
| 47 | 49 |
src/main/java/com/taover/repository/mapper/CustomJdbcTemplateRowMapper.java
0 → 100644
| @@ -0,0 +1,119 @@ | @@ -0,0 +1,119 @@ | ||
| 1 | +package com.taover.repository.mapper; | ||
| 2 | + | ||
| 3 | +import java.lang.reflect.Field; | ||
| 4 | +import java.lang.reflect.Method; | ||
| 5 | +import java.sql.ResultSet; | ||
| 6 | +import java.sql.SQLException; | ||
| 7 | +import java.util.HashMap; | ||
| 8 | +import java.util.Map; | ||
| 9 | + | ||
| 10 | +import org.apache.commons.logging.Log; | ||
| 11 | +import org.apache.commons.logging.LogFactory; | ||
| 12 | +import org.springframework.jdbc.core.RowMapper; | ||
| 13 | +import org.springframework.jdbc.support.rowset.ResultSetWrappingSqlRowSetMetaData; | ||
| 14 | + | ||
| 15 | +public class CustomJdbcTemplateRowMapper <E> implements RowMapper<E>{ | ||
| 16 | + Log log = LogFactory.getLog(this.getClass()); | ||
| 17 | + | ||
| 18 | + private Map<String, Field> beanFielNameToField = new HashMap<String, Field>(); | ||
| 19 | + private Method beforeMethod = null; | ||
| 20 | + private Method afterMethod = null; | ||
| 21 | + | ||
| 22 | + private Class<E> classInfo; | ||
| 23 | + private Map<String, String> tableToBeanMap; | ||
| 24 | + | ||
| 25 | + public CustomJdbcTemplateRowMapper(Class<E> classInfo, Map<String, String> tableToBeanMap) { | ||
| 26 | + this.classInfo = classInfo; | ||
| 27 | + this.tableToBeanMap = tableToBeanMap; | ||
| 28 | + | ||
| 29 | + Field[] fields = this.classInfo.getDeclaredFields(); | ||
| 30 | + for(Field item: fields) { | ||
| 31 | + try { | ||
| 32 | + item.setAccessible(true); | ||
| 33 | + this.beanFielNameToField.put(item.getName(), item); | ||
| 34 | + }catch (Exception e) { | ||
| 35 | + log.error("set field accessible:"+e.getMessage()); | ||
| 36 | + } | ||
| 37 | + } | ||
| 38 | + | ||
| 39 | + boolean hasImplementPointCut = false; | ||
| 40 | + Class[] interfaceArr = this.classInfo.getInterfaces(); | ||
| 41 | + for(int i=0; i<interfaceArr.length; ++i){ | ||
| 42 | + if(interfaceArr[i].getName().equals("com.taover.repository.advice.EntityPointCut")){ | ||
| 43 | + hasImplementPointCut = true; | ||
| 44 | + break; | ||
| 45 | + } | ||
| 46 | + } | ||
| 47 | + | ||
| 48 | + if(hasImplementPointCut){ | ||
| 49 | + try{ | ||
| 50 | + beforeMethod = this.classInfo.getDeclaredMethod("before"); | ||
| 51 | + beforeMethod.setAccessible(true); | ||
| 52 | + }catch(Exception e){ | ||
| 53 | + log.error("set before method exception:"+e.getMessage()); | ||
| 54 | + } | ||
| 55 | + | ||
| 56 | + try{ | ||
| 57 | + afterMethod = this.classInfo.getDeclaredMethod("after"); | ||
| 58 | + afterMethod.setAccessible(true); | ||
| 59 | + }catch(Exception e){ | ||
| 60 | + log.error("set after method exception:"+e.getMessage()); | ||
| 61 | + } | ||
| 62 | + } | ||
| 63 | + } | ||
| 64 | + | ||
| 65 | + @Override | ||
| 66 | + public E mapRow(ResultSet rs, int index) throws SQLException { | ||
| 67 | + E targetObj; | ||
| 68 | + try { | ||
| 69 | + targetObj = this.classInfo.newInstance(); | ||
| 70 | + } catch (Exception e) { | ||
| 71 | + throw new RuntimeException(e); | ||
| 72 | + } | ||
| 73 | + | ||
| 74 | + if(this.beforeMethod != null){ | ||
| 75 | + try{ | ||
| 76 | + beforeMethod.invoke(targetObj); | ||
| 77 | + }catch(Exception e){ | ||
| 78 | + log.error("invoke before exception:"+e.getMessage()); | ||
| 79 | + } | ||
| 80 | + } | ||
| 81 | + | ||
| 82 | + ResultSetWrappingSqlRowSetMetaData wapping = new ResultSetWrappingSqlRowSetMetaData(rs.getMetaData()); | ||
| 83 | + int columnCount = wapping.getColumnCount(); | ||
| 84 | + for (int i = 1; i<=columnCount; i++) { | ||
| 85 | + String tableFieldName = wapping.getColumnLabel(i); | ||
| 86 | + String beanFieldName = this.tableToBeanMap.get(tableFieldName); | ||
| 87 | + Object value = rs.getObject(i); | ||
| 88 | + Field beanField = this.beanFielNameToField.get(beanFieldName); | ||
| 89 | + if(null == value || beanFieldName == null && beanField == null){ | ||
| 90 | + continue; | ||
| 91 | + } | ||
| 92 | + try { | ||
| 93 | + beanField.set(targetObj, value); | ||
| 94 | + } catch (IllegalArgumentException e) { | ||
| 95 | + if("Integer".equals(value.getClass().getSimpleName()) && "Long".equals(beanField.getType().getSimpleName())) { | ||
| 96 | + try { | ||
| 97 | + beanField.set(targetObj, Long.valueOf(value.toString())); | ||
| 98 | + } catch (IllegalArgumentException | IllegalAccessException e1) { | ||
| 99 | + log.error("map set object field error -> Integer convert Long:"+e.getMessage()); | ||
| 100 | + } | ||
| 101 | + }else { | ||
| 102 | + log.error("map set object field error:"+e.getMessage()); | ||
| 103 | + } | ||
| 104 | + } catch (IllegalAccessException e) { | ||
| 105 | + log.error("map set object field error:"+e.getMessage()); | ||
| 106 | + } | ||
| 107 | + } | ||
| 108 | + | ||
| 109 | + if(this.afterMethod != null){ | ||
| 110 | + try { | ||
| 111 | + afterMethod.invoke(targetObj); | ||
| 112 | + }catch (Exception e) { | ||
| 113 | + log.error("invoke after exception:"+e.getMessage()); | ||
| 114 | + } | ||
| 115 | + } | ||
| 116 | + | ||
| 117 | + return targetObj; | ||
| 118 | + } | ||
| 119 | +} |
src/main/java/com/taover/repository/shardingsphere/ShardingAlgorithmHint.java
| @@ -1,33 +0,0 @@ | @@ -1,33 +0,0 @@ | ||
| 1 | -package com.taover.repository.shardingsphere; | ||
| 2 | - | ||
| 3 | -import java.util.Collection; | ||
| 4 | -import java.util.HashSet; | ||
| 5 | -import java.util.Set; | ||
| 6 | - | ||
| 7 | -import org.apache.commons.logging.Log; | ||
| 8 | -import org.apache.commons.logging.LogFactory; | ||
| 9 | -import org.apache.shardingsphere.api.sharding.hint.HintShardingAlgorithm; | ||
| 10 | -import org.apache.shardingsphere.api.sharding.hint.HintShardingValue; | ||
| 11 | - | ||
| 12 | -public class ShardingAlgorithmHint implements HintShardingAlgorithm<String> { | ||
| 13 | - Log log = LogFactory.getLog(ShardingAlgorithmHint.class); | ||
| 14 | - | ||
| 15 | - public ShardingAlgorithmHint() { | ||
| 16 | - System.out.println("ShardingAlgorithmHint()"); | ||
| 17 | - } | ||
| 18 | - | ||
| 19 | - @Override | ||
| 20 | - public Collection<String> doSharding(Collection<String> availableTargetNames, HintShardingValue<String> shardingValue) { | ||
| 21 | - Collection<String> values = shardingValue.getValues(); | ||
| 22 | - if(values == null || values.isEmpty()) { | ||
| 23 | - String message = shardingValue.getLogicTableName()+"已启用分库分表,SQL语句请指定tenant_id 或 使用封装好的DAO层方法"; | ||
| 24 | - log.error(message); | ||
| 25 | - throw new RuntimeException(message); | ||
| 26 | - } | ||
| 27 | - Set<String> result = new HashSet<String>(availableTargetNames); | ||
| 28 | - for(String item: values) { | ||
| 29 | - result.add(shardingValue.getLogicTableName()+item); | ||
| 30 | - } | ||
| 31 | - return result; | ||
| 32 | - } | ||
| 33 | -} |
src/main/java/com/taover/repository/shardingsphere/ShardingDatabaseAlgorithmHint.java
0 → 100644
| @@ -0,0 +1,31 @@ | @@ -0,0 +1,31 @@ | ||
| 1 | +package com.taover.repository.shardingsphere; | ||
| 2 | + | ||
| 3 | +import java.util.Collection; | ||
| 4 | +import java.util.HashSet; | ||
| 5 | +import java.util.Set; | ||
| 6 | + | ||
| 7 | +import org.apache.commons.logging.Log; | ||
| 8 | +import org.apache.commons.logging.LogFactory; | ||
| 9 | +import org.apache.shardingsphere.api.sharding.hint.HintShardingAlgorithm; | ||
| 10 | +import org.apache.shardingsphere.api.sharding.hint.HintShardingValue; | ||
| 11 | + | ||
| 12 | +public class ShardingDatabaseAlgorithmHint implements HintShardingAlgorithm<String> { | ||
| 13 | + Log log = LogFactory.getLog(ShardingDatabaseAlgorithmHint.class); | ||
| 14 | + | ||
| 15 | + public ShardingDatabaseAlgorithmHint() { } | ||
| 16 | + | ||
| 17 | + @Override | ||
| 18 | + public Collection<String> doSharding(Collection<String> availableTargetNames, HintShardingValue<String> shardingValue) { | ||
| 19 | + Collection<String> values = shardingValue.getValues(); | ||
| 20 | + if(values == null || values.isEmpty()) { | ||
| 21 | + String message = shardingValue.getLogicTableName()+"已启用分库分表,SQL语句请指定tenant_id 或 使用封装好的DAO层方法"; | ||
| 22 | + log.error(message); | ||
| 23 | + throw new RuntimeException(message); | ||
| 24 | + } | ||
| 25 | + Set<String> result = new HashSet<String>(1); | ||
| 26 | + for(String item: shardingValue.getValues()) { | ||
| 27 | + result.add(item); | ||
| 28 | + } | ||
| 29 | + return result; | ||
| 30 | + } | ||
| 31 | +} |
src/main/java/com/taover/repository/shardingsphere/ShardingInfoEntity.java
| @@ -61,16 +61,16 @@ public class ShardingInfoEntity implements Serializable { | @@ -61,16 +61,16 @@ public class ShardingInfoEntity implements Serializable { | ||
| 61 | } | 61 | } |
| 62 | 62 | ||
| 63 | /** | 63 | /** |
| 64 | - * 后缀 | 64 | + * 表后缀 |
| 65 | */ | 65 | */ |
| 66 | - @Column(name="sharding_suffix") | ||
| 67 | - private java.lang.String shardingSuffix; | 66 | + @Column(name="table_suffix") |
| 67 | + private java.lang.String tableSuffix; | ||
| 68 | 68 | ||
| 69 | - public java.lang.String getShardingSuffix(){ | ||
| 70 | - return shardingSuffix; | 69 | + public java.lang.String getTableSuffix(){ |
| 70 | + return tableSuffix; | ||
| 71 | } | 71 | } |
| 72 | - public void setShardingSuffix(java.lang.String shardingSuffix){ | ||
| 73 | - this.shardingSuffix = shardingSuffix; | 72 | + public void setTableSuffix(java.lang.String tableSuffix){ |
| 73 | + this.tableSuffix = tableSuffix; | ||
| 74 | } | 74 | } |
| 75 | 75 | ||
| 76 | /** | 76 | /** |
| @@ -98,9 +98,22 @@ public class ShardingInfoEntity implements Serializable { | @@ -98,9 +98,22 @@ public class ShardingInfoEntity implements Serializable { | ||
| 98 | public void setUpdateTime(java.sql.Timestamp updateTime){ | 98 | public void setUpdateTime(java.sql.Timestamp updateTime){ |
| 99 | this.updateTime = updateTime; | 99 | this.updateTime = updateTime; |
| 100 | } | 100 | } |
| 101 | + | ||
| 102 | + /** | ||
| 103 | + * 数据库 后缀 | ||
| 104 | + */ | ||
| 105 | + @Column(name="ds_name") | ||
| 106 | + private java.lang.String dsName; | ||
| 107 | + | ||
| 108 | + public java.lang.String getDsName(){ | ||
| 109 | + return dsName; | ||
| 110 | + } | ||
| 111 | + public void setDsName(java.lang.String dsName){ | ||
| 112 | + this.dsName = dsName; | ||
| 113 | + } | ||
| 101 | 114 | ||
| 102 | @Override | 115 | @Override |
| 103 | public String toString() { | 116 | public String toString() { |
| 104 | - return "ShardingInfoEntity: [id="+id+",tableName="+tableName+",tenantId="+tenantId+",shardingSuffix="+shardingSuffix+",createTime="+createTime+",updateTime="+updateTime+"]"; | 117 | + return "ShardingInfoEntity: [id="+id+",tableName="+tableName+",tenantId="+tenantId+",tableSuffix="+tableSuffix+",createTime="+createTime+",updateTime="+updateTime+",dsName="+dsName+"]"; |
| 105 | } | 118 | } |
| 106 | } | 119 | } |
src/main/java/com/taover/repository/shardingsphere/ShardingSphereService.java
| 1 | package com.taover.repository.shardingsphere; | 1 | package com.taover.repository.shardingsphere; |
| 2 | 2 | ||
| 3 | +import java.util.ArrayList; | ||
| 3 | import java.util.HashMap; | 4 | import java.util.HashMap; |
| 4 | import java.util.List; | 5 | import java.util.List; |
| 5 | import java.util.Map; | 6 | import java.util.Map; |
| @@ -12,7 +13,7 @@ import com.taover.repository.autoconfigure.ShardingSphereKeyGeneratorConfigurati | @@ -12,7 +13,7 @@ import com.taover.repository.autoconfigure.ShardingSphereKeyGeneratorConfigurati | ||
| 12 | 13 | ||
| 13 | @Service | 14 | @Service |
| 14 | public class ShardingSphereService { | 15 | public class ShardingSphereService { |
| 15 | - private Map<Long, Map<String, String>> CACHED_TABLE_SUFFIX_BY_TENANT = null; | 16 | + private Map<Long, List<ShardingInfoEntity>> CACHED_TABLE_SUFFIX_BY_TENANT = null; |
| 16 | private Map<String, ShardingKeyGeneratorExt> GENERATOR_HOLDER = new HashMap<String, ShardingKeyGeneratorExt>(); | 17 | private Map<String, ShardingKeyGeneratorExt> GENERATOR_HOLDER = new HashMap<String, ShardingKeyGeneratorExt>(); |
| 17 | 18 | ||
| 18 | @Resource | 19 | @Resource |
| @@ -30,7 +31,7 @@ public class ShardingSphereService { | @@ -30,7 +31,7 @@ public class ShardingSphereService { | ||
| 30 | return GENERATOR_HOLDER.get(tableName).generateKeyList(number); | 31 | return GENERATOR_HOLDER.get(tableName).generateKeyList(number); |
| 31 | } | 32 | } |
| 32 | 33 | ||
| 33 | - public Map<String, String> getShardingInfoMapByTenantId(Long tenantId) { | 34 | + public List<ShardingInfoEntity> getShardingInfoMapByTenantId(Long tenantId) { |
| 34 | if(CACHED_TABLE_SUFFIX_BY_TENANT == null) { | 35 | if(CACHED_TABLE_SUFFIX_BY_TENANT == null) { |
| 35 | loadCacheTableShardingInfoByTenantId(); | 36 | loadCacheTableShardingInfoByTenantId(); |
| 36 | } | 37 | } |
| @@ -42,10 +43,10 @@ public class ShardingSphereService { | @@ -42,10 +43,10 @@ public class ShardingSphereService { | ||
| 42 | return; | 43 | return; |
| 43 | } | 44 | } |
| 44 | List<ShardingInfoEntity> dataList = this.shardingInfoRepository.findListBySql("1=1"); | 45 | List<ShardingInfoEntity> dataList = this.shardingInfoRepository.findListBySql("1=1"); |
| 45 | - Map<Long, Map<String, String>> tempData = new HashMap<Long, Map<String, String>>(); | 46 | + Map<Long, List<ShardingInfoEntity>> tempData = new HashMap<Long, List<ShardingInfoEntity>>(); |
| 46 | for(ShardingInfoEntity item: dataList) { | 47 | for(ShardingInfoEntity item: dataList) { |
| 47 | - Map<String, String> tempItem = tempData.getOrDefault(item.getTenantId(), new HashMap<String, String>()); | ||
| 48 | - tempItem.put(item.getTableName(), item.getShardingSuffix()); | 48 | + List<ShardingInfoEntity> tempItem = tempData.getOrDefault(item.getTenantId(), new ArrayList<ShardingInfoEntity>()); |
| 49 | + tempItem.add(item); | ||
| 49 | tempData.put(item.getTenantId(), tempItem); | 50 | tempData.put(item.getTenantId(), tempItem); |
| 50 | } | 51 | } |
| 51 | CACHED_TABLE_SUFFIX_BY_TENANT = tempData; | 52 | CACHED_TABLE_SUFFIX_BY_TENANT = tempData; |
src/main/java/com/taover/repository/shardingsphere/ShardingTableAlgorithmHint.java
0 → 100644
| @@ -0,0 +1,31 @@ | @@ -0,0 +1,31 @@ | ||
| 1 | +package com.taover.repository.shardingsphere; | ||
| 2 | + | ||
| 3 | +import java.util.Collection; | ||
| 4 | +import java.util.HashSet; | ||
| 5 | +import java.util.Set; | ||
| 6 | + | ||
| 7 | +import org.apache.commons.logging.Log; | ||
| 8 | +import org.apache.commons.logging.LogFactory; | ||
| 9 | +import org.apache.shardingsphere.api.sharding.hint.HintShardingAlgorithm; | ||
| 10 | +import org.apache.shardingsphere.api.sharding.hint.HintShardingValue; | ||
| 11 | + | ||
| 12 | +public class ShardingTableAlgorithmHint implements HintShardingAlgorithm<String> { | ||
| 13 | + Log log = LogFactory.getLog(ShardingTableAlgorithmHint.class); | ||
| 14 | + | ||
| 15 | + public ShardingTableAlgorithmHint() { } | ||
| 16 | + | ||
| 17 | + @Override | ||
| 18 | + public Collection<String> doSharding(Collection<String> availableTargetNames, HintShardingValue<String> shardingValue) { | ||
| 19 | + Collection<String> values = shardingValue.getValues(); | ||
| 20 | + if(values == null || values.isEmpty()) { | ||
| 21 | + String message = shardingValue.getLogicTableName()+"已启用分库分表,SQL语句请指定tenant_id 或 使用封装好的DAO层方法"; | ||
| 22 | + log.error(message); | ||
| 23 | + throw new RuntimeException(message); | ||
| 24 | + } | ||
| 25 | + Set<String> result = new HashSet<String>(availableTargetNames); | ||
| 26 | + for(String item: values) { | ||
| 27 | + result.add(shardingValue.getLogicTableName()+item); | ||
| 28 | + } | ||
| 29 | + return result; | ||
| 30 | + } | ||
| 31 | +} |
src/test/java/com/taover/repository/test/TestAutoconfigure.java
| @@ -13,6 +13,7 @@ public class TestAutoconfigure { | @@ -13,6 +13,7 @@ public class TestAutoconfigure { | ||
| 13 | public static void main(String args[]) { | 13 | public static void main(String args[]) { |
| 14 | ConfigurableApplicationContext context = SpringApplication.run(TestAutoconfigure.class, args); | 14 | ConfigurableApplicationContext context = SpringApplication.run(TestAutoconfigure.class, args); |
| 15 | JdbcTemplateWrapperTenant jdbcTemplate = context.getBean(JdbcTemplateWrapperTenant.class); | 15 | JdbcTemplateWrapperTenant jdbcTemplate = context.getBean(JdbcTemplateWrapperTenant.class); |
| 16 | + System.out.println(jdbcTemplate.queryForObject("select id from wxorder_order limit 1", Long.class, 1L)); | ||
| 16 | System.out.println(jdbcTemplate.queryForList("select * from wxorder_order limit 1", 1L)); | 17 | System.out.println(jdbcTemplate.queryForList("select * from wxorder_order limit 1", 1L)); |
| 17 | System.out.println(jdbcTemplate.queryForList("select * from wxorder_order limit 1", 2L)); | 18 | System.out.println(jdbcTemplate.queryForList("select * from wxorder_order limit 1", 2L)); |
| 18 | System.out.println(jdbcTemplate.queryForList("select * from wxorder_order_express limit 1", 1L)); | 19 | System.out.println(jdbcTemplate.queryForList("select * from wxorder_order_express limit 1", 1L)); |
src/test/resources/application.properties
| @@ -16,9 +16,9 @@ spring.mvc.dispatch-options-request=true | @@ -16,9 +16,9 @@ spring.mvc.dispatch-options-request=true | ||
| 16 | spring.shardingsphere.datasource.names=ds0 | 16 | spring.shardingsphere.datasource.names=ds0 |
| 17 | spring.shardingsphere.datasource.ds0.type=com.alibaba.druid.pool.DruidDataSource | 17 | spring.shardingsphere.datasource.ds0.type=com.alibaba.druid.pool.DruidDataSource |
| 18 | spring.shardingsphere.datasource.ds0.driver-class-name=com.mysql.jdbc.Driver | 18 | spring.shardingsphere.datasource.ds0.driver-class-name=com.mysql.jdbc.Driver |
| 19 | -#spring.shardingsphere.datasource.ds0.url=jdbc:mysql://121.42.142.102:3306/bzyun_wxorder?characterEncoding=UTF-8&zeroDateTimeBehavior=CONVERT_TO_NULL | ||
| 20 | -#spring.shardingsphere.datasource.ds0.username=dev | ||
| 21 | -#spring.shardingsphere.datasource.ds0.password=taover02 | 19 | +#spring.shardingsphere.datasource.ds_0.url=jdbc:mysql://121.42.142.102:3306/bzyun_wxorder?characterEncoding=UTF-8&zeroDateTimeBehavior=CONVERT_TO_NULL |
| 20 | +#spring.shardingsphere.datasource.ds_0.username=dev | ||
| 21 | +#spring.shardingsphere.datasource.ds_0.password=taover02 | ||
| 22 | spring.shardingsphere.datasource.ds0.url=jdbc:mysql://rdsifmezqifmezqo.mysql.rds.aliyuncs.com:3306/bzyun_wxorder?characterEncoding=UTF-8 | 22 | spring.shardingsphere.datasource.ds0.url=jdbc:mysql://rdsifmezqifmezqo.mysql.rds.aliyuncs.com:3306/bzyun_wxorder?characterEncoding=UTF-8 |
| 23 | spring.shardingsphere.datasource.ds0.username=tylife | 23 | spring.shardingsphere.datasource.ds0.username=tylife |
| 24 | spring.shardingsphere.datasource.ds0.password=lexi365 | 24 | spring.shardingsphere.datasource.ds0.password=lexi365 |
| @@ -28,16 +28,14 @@ spring.shardingsphere.datasource.ds0.min-idle=5 | @@ -28,16 +28,14 @@ spring.shardingsphere.datasource.ds0.min-idle=5 | ||
| 28 | spring.shardingsphere.datasource.ds0.max-wait=60000 | 28 | spring.shardingsphere.datasource.ds0.max-wait=60000 |
| 29 | 29 | ||
| 30 | spring.shardingsphere.sharding.tables.wxorder_order.actual-data-nodes=ds0.wxorder_order | 30 | spring.shardingsphere.sharding.tables.wxorder_order.actual-data-nodes=ds0.wxorder_order |
| 31 | -spring.shardingsphere.sharding.tables.wxorder_order.database-strategy.inline.sharding-column=id | ||
| 32 | -spring.shardingsphere.sharding.tables.wxorder_order.database-strategy.inline.algorithm-expression=ds0 | ||
| 33 | -spring.shardingsphere.sharding.tables.wxorder_order.table-strategy.hint.algorithm-class-name=com.taover.repository.shardingsphere.ShardingAlgorithmHint | 31 | +spring.shardingsphere.sharding.tables.wxorder_order.database-strategy.hint.algorithm-class-name=com.taover.repository.shardingsphere.ShardingDatabaseAlgorithmHint |
| 32 | +spring.shardingsphere.sharding.tables.wxorder_order.table-strategy.hint.algorithm-class-name=com.taover.repository.shardingsphere.ShardingTableAlgorithmHint | ||
| 34 | spring.shardingsphere.sharding.tables.wxorder_order.key-generator.column=id | 33 | spring.shardingsphere.sharding.tables.wxorder_order.key-generator.column=id |
| 35 | spring.shardingsphere.sharding.tables.wxorder_order.key-generator.type=SNOWFLAKE-SELF | 34 | spring.shardingsphere.sharding.tables.wxorder_order.key-generator.type=SNOWFLAKE-SELF |
| 36 | 35 | ||
| 37 | spring.shardingsphere.sharding.tables.wxorder_order_goods.actual-data-nodes=ds0.wxorder_order_goods | 36 | spring.shardingsphere.sharding.tables.wxorder_order_goods.actual-data-nodes=ds0.wxorder_order_goods |
| 38 | -spring.shardingsphere.sharding.tables.wxorder_order_goods.database-strategy.inline.sharding-column=id | ||
| 39 | -spring.shardingsphere.sharding.tables.wxorder_order_goods.database-strategy.inline.algorithm-expression=ds0 | ||
| 40 | -spring.shardingsphere.sharding.tables.wxorder_order.table-strategy.hint.algorithm-class-name=com.taover.repository.shardingsphere.ShardingAlgorithmHint | 37 | +spring.shardingsphere.sharding.tables.wxorder_order_goods.database-strategy.hint.algorithm-class-name=com.taover.repository.shardingsphere.ShardingDatabaseAlgorithmHint |
| 38 | +spring.shardingsphere.sharding.tables.wxorder_order_goods.table-strategy.hint.algorithm-class-name=com.taover.repository.shardingsphere.ShardingTableAlgorithmHint | ||
| 41 | spring.shardingsphere.sharding.tables.wxorder_order_goods.key-generator.column=id | 39 | spring.shardingsphere.sharding.tables.wxorder_order_goods.key-generator.column=id |
| 42 | spring.shardingsphere.sharding.tables.wxorder_order_goods.key-generator.type=SNOWFLAKE-SELF | 40 | spring.shardingsphere.sharding.tables.wxorder_order_goods.key-generator.type=SNOWFLAKE-SELF |
| 43 | 41 |