本文只讲shardingsphere的分库分表
一、在springboot工程的pom中加入依赖,版本根据自身情况决定
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-core-api</artifactId>
<version>4.0.0-RC1</version>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-core</artifactId>
<version>4.0.0-RC1</version>
</dependency>
二、配置文件中加入数据源的配置,要分几个库就配几个
# 有几个库就写几个,用逗号分开
spring.shardingsphere.datasource.names=cestc_mworkflow_new_0,cestc_mworkflow_new_1
# 每个数据源的配置,本文为了方便起见就配了一个。
spring.shardingsphere.datasource.cestc_mworkflow_new.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.cestc_mworkflow_new.url=jdbc:mysql://192.168.200.211:3306/cestc_mworkflow_new_0
spring.shardingsphere.datasource.cestc_mworkflow_new.username=root
spring.shardingsphere.datasource.cestc_mworkflow_new.password=123456
三、分库策略配置
# 逻辑表wf_core_run_execution对应的真实的表(节点)列表,也就是库名.表名,可以分开写,也可以使用如下行表达式
spring.shardingsphere.sharding.tables.wf_core_run_execution.actual-data-nodes=cestc_mworkflow_new_$->{
0..1}.wf_core_run_execution_$->{
0..1}
# 逻辑表wf_core_run_execution要根据哪个列进行分库
spring.shardingsphere.sharding.tables.wf_core_run_execution.database-strategy.inline.sharding-column=tenant_id_
# 具体的分库策略类(自己定义)
spring.shardingsphere.sharding.tables.wf_core_run_execution.database-strategy.complex.algorithm-class-name=com.cestc.basecore.config.DatabaseShardingAlgorithm
# 是否打印sql语句
spring.shardingsphere.props.sql.show=true
具体怎么分,根据自身业务来决定,不同的分法实现的接口也不同
@Service
public <