参考:
springboot+sharding-jdbc+mybatis全注解实现增量数据库分片实现
https://segmentfault.com/a/1190000014037539%3Futm_source%3Dtag-newest

SpringBoot+Mybatis+Sharding-JDBC
https://www.jianshu.com/p/415fa2a49b07

sharding-jdbc实现按年分库按月分表
https://www.jianshu.com/p/943325c577a4

sharding-jdbc 按月份分表
https://www.jianshu.com/p/014d78a792fc?from=singlemessage

sharding sphere 4.0.0-RC1版本 按年分表实战
https://blog.csdn.net/tzxylao/article/details/103878016?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522163151698716780264092097%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=163151698716780264092097&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_v2~rank_v29-1-103878016.pc_v2_rank_blog_default&utm_term=sharding+sphere+4.0.0-RC1%E7%89%88%E6%9C%AC&spm=1018.2226.3001.4450

sharding sphere 4.0.0-RC1版本 按年分表(后续优化)
https://blog.csdn.net/tzxylao/article/details/104035722?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522163151698716780264092097%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=163151698716780264092097&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_v2~rank_v29-2-104035722.pc_v2_rank_blog_default&utm_term=sharding+sphere+4.0.0-RC1%E7%89%88%E6%9C%AC&spm=1018.2226.3001.4450

sharding sphere 4.0.0-RC1版本 按年分表(自动建表)
https://blog.csdn.net/tzxylao/article/details/103992376

Sharding-JDBC:单库分表的实现,读写分离
https://www.cnblogs.com/yinjihuan/p/11262559.html

sharding demo 读写分离 U (分库分表 & 不分库只分表)
http://cache.baiducontent.com/c?m=kyWCpqM41VLmxFdgJCuORrotAo5mcxh_iOR8jot41t4BmFmcE5JV9RVcmCH6SyBW2CWbeibCB7EjeY78pAca6aImiOpglyN26qOAwFLc_uA74Crnm4mqmswEKUi5Zg7F6Tu3oIjTnKk0UKC3AH6GuquJFseRQHnFhp3u9z88iWImYwT92iwCKTdRJvAm48&p=882a944293d217f21ebcc7710f0d&newp=882a94428b972ded43adc7710f4c92695d0fc20e3adcd101298ffe0cc4241a1a1a3aecbe25201707d3c6796707a84e57eef730783d0034f1f689df08d2ecce7e72dc4b766b5c&s=cfcd208495d565ef&user=baidu&fm=sc&query=sharding+%B2%BB%B7%D6%BF%E2+%D6%BB%B7%D6%B1%ED&qid=8b571dc5000492f7&p1=2

springboot整合sharding-jdbc实现按月份分表mysql
https://www.cnblogs.com/ouyanxia/p/11918627.html
使用 Sharding-Jdbc 实现分库分表、读写分离(未完待续)
https://segmentfault.com/a/1190000012115838

分片策略
https://www.cnblogs.com/mr-yang-localhost/p/8313360.html
事务
https://www.cnblogs.com/dalianpai/p/14001823.html

<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>4.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-namespace</artifactId>
<version>4.1.1</version>
</dependency>

pom文件在引用Sharding-jdbc时有2张方式:

单独的DataSourceConfig
@MapperScan(basePackages = MyDruidDataSourceDynamicConfig.PACKAGE, sqlSessionFactoryRef = “sessionFactory”)
public class MyDruidDataSourceDynamicConfig {
public static final String PACKAGE = “com.toyor.huoyuncloud.provider.mapper.*”;

yml
mybatis:
#程序里写了,这里不起作用
mapper-locations: classpath:com/toyor/huoyuncloud/provider/mapper/**/.xml
type-aliases-package: com.toyor.huoyuncloud.model
configuration:
log-impl:
org.apache.ibatis.logging.stdout.StdOutImpl #org.apache.ibatis.logging.log4j2.Log4j2Impl
#logging.level.com.toyor.huoyuncloud.provider.mapper: debug #也可以输出日志

pom.xml

org.apache.shardingsphere
sharding-jdbc-spring-boot-starter
4.1.1


org.apache.shardingsphere
sharding-jdbc-spring-namespace
4.1.1


io.shardingsphere
sharding-transaction-spring-boot-starter
3.1.0

public class ShardingAlgorithmInVoiceDetail implements PreciseShardingAlgorithm, RangeShardingAlgorithm {

  1. private List<String> listTablenames;<br /> private final String tablepre = "invoicedetail";<br /> private Boolean isFirstLoad = false;<br /> private BaseInfoService baseInfoService;<br /> @Override<br /> public String doSharding(Collection availableTargetNames, PreciseShardingValue shardingValue) {<br /> if (!isFirstLoad) {<br /> baseInfoService = SpringContextUtil.getBean("baseInfoServiceImpl");<br /> listTablenames = baseInfoService.getListTableName(tablepre);<br /> isFirstLoad = true;<br /> }<br /> String target = shardingValue.getValue().toString();<br /> return shardingValue.getLogicTableName() + target.substring(0,4);<br /> }
  2. @Override<br /> public Collection<String> doSharding(Collection<String> availableTargetNames, RangeShardingValue<Integer> shardingValue) {<br /> if (!isFirstLoad) {<br /> baseInfoService = SpringContextUtil.getBean("baseInfoServiceImpl");<br /> listTablenames = baseInfoService.getListTableName(tablepre);<br /> isFirstLoad = true;<br /> }<br /> Collection<String> availables = new ArrayList<>();<br /> Range valueRange = shardingValue.getValueRange();<br /> for (String target : listTablenames) {<br /> Integer shardValue = Integer.parseInt(StringUtils.right(target,4));<br /> if (valueRange.hasLowerBound()) {<br /> String lowerStr = valueRange.lowerEndpoint().toString();<br /> Integer start = Integer.parseInt(lowerStr.substring(0, 4));<br /> if (shardValue < start) {<br /> continue;<br /> }<br /> }<br /> if (valueRange.hasUpperBound()) {<br /> String upperStr = valueRange.upperEndpoint().toString();<br /> Integer end = Integer.parseInt(upperStr.substring(0, 4));<br /> if (shardValue > end ) {<br /> continue;<br /> }<br /> }<br /> availables.add(target);<br /> }<br /> return availables;<br /> }<br />}

application-sharding.yml
spring:
shardingsphere:
# 打印sql解析过程
props:
sql.show: true
datasource:
names: ds0
ds0:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver #com.mysql.jdbc.Driver
url: jdbc:mysql://192.168.16.102:3307/huoyuncloud?serverTimezone=GMT&useUnicode=true&characterEncoding=utf-8&useSSL=true&allowMultiQueries=true
username: root
password: Toyor@163.com

ds1:
# type: com.alibaba.druid.pool.DruidDataSource
# driver-class-name: com.mysql.cj.jdbc.Driver #com.mysql.jdbc.Driver
# url: jdbc:mysql://192.168.16.102:3307/mytest1?serverTimezone=GMT&useUnicode=true&characterEncoding=utf-8&useSSL=true&allowMultiQueries=true
# username: root
# password: Toyor@163.com
sharding:
# 默认数据源,可以将不分库分表的数据表放在这里(此处的表需与已经分库分表的表完全没有关联,不会产生联表查询操作,因为跨库连表查询是没办法实现的)
# 3.1.0版本中dql查询存在bug,不使用默认库.会在下个版本中修复
#default-data-source-name: ds0
tables:
#运单
joblist:
#actual-data-nodes: ds0.aaasharding202$->{1..6}
#分表策略ShardingAlgorithmJob 里从数据库获取真实的表,actual-data-nodes不用设置也可以
actual-data-nodes: ds0.joblist$->{2021..2099}
table-strategy:
standard:
sharding-column: jobuid
precise-algorithm-class-name: com.toyor.huoyuncloud.provider.util.ShardingAlgorithmJob
range-algorithm-class-name: com.toyor.huoyuncloud.provider.util.ShardingAlgorithmJob
#发票主表
invoicemaster:
#actual-data-nodes: ds0.aaasharding202$->{1..6}
#分表策略ShardingAlgorithmJob 里从数据库获取真实的表,actual-data-nodes不用设置也可以
actual-data-nodes: ds0.invoicemaster$->{2021..2099}
table-strategy:
standard:
sharding-column: invoiceuid
precise-algorithm-class-name: com.toyor.huoyuncloud.provider.util.ShardingAlgorithmInVoiceMaster
range-algorithm-class-name: com.toyor.huoyuncloud.provider.util.ShardingAlgorithmInVoiceMaster
#发票主表
invoicedetail:
#actual-data-nodes: ds0.aaasharding202$->{1..6}
#分表策略ShardingAlgorithmJob 里从数据库获取真实的表,actual-data-nodes不用设置也可以
actual-data-nodes: ds0.invoicemaster$->{2021..2099}
table-strategy:
standard:
sharding-column: invoiceuid
precise-algorithm-class-name: com.toyor.huoyuncloud.provider.util.ShardingAlgorithmInVoiceDetail
range-algorithm-class-name: com.toyor.huoyuncloud.provider.util.ShardingAlgorithmInVoiceDetail