简单明了springboot集成shardingsphere分库分表

本文详细介绍了如何在SpringBoot项目中集成ShardingSphere进行分库分表操作,包括添加依赖、配置数据源、设定分库策略和分表策略,以及注意事项,强调SQL中的表名是逻辑表名而非实际表名。
摘要由CSDN通过智能技术生成

本文只讲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 <
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值