参考:
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/103992376
Sharding-JDBC:单库分表的实现,读写分离
https://www.cnblogs.com/yinjihuan/p/11262559.html
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张方式:
- sharding-jdbc-spring-boot-starter(上面的就是,决定mybatis扫描包就要采用yml配置,而不用DataSource里配置)
这种方式可以不用自己写DataSourceConfig文件 - sharding-jdbc-core
这种方式的例子,基本都是要自己写DataSourceConfig文件的
参考:https://www.jianshu.com/p/3b2ab87b0de7
https://blog.csdn.net/a992795427/article/details/85102918
https://www.cnblogs.com/mr-yang-localhost/p/8280500.html
单独的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
public class ShardingAlgorithmInVoiceDetail implements PreciseShardingAlgorithm, RangeShardingAlgorithm
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 /> }
@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