1.增加pom坐标
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>4.0.0-RC1</version>
</dependency>
2.增加配置文件
#数据源
spring.shardingsphere.datasource.names=master0
#配置数据源
spring.shardingsphere.datasource.master0.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.master0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.master0.url=jdbc:mysql://192.168.101.24:3306/anyouliang_exchange
spring.shardingsphere.datasource.master0.username=root
spring.shardingsphere.datasource.master0.password=123456
#spring.shardingsphere.datasource.master0slave0.type=com.alibaba.druid.pool.DruidDataSource
#spring.shardingsphere.datasource.master0slave0.driver-class-name=com.mysql.jdbc.Driver
#spring.shardingsphere.datasource.master0slave0.url=jdbc:mysql://192.168.101.47:3306/anyouliang_exchange
#spring.shardingsphere.datasource.master0slave0.username=root
#spring.shardingsphere.datasource.master0slave0.password=123456
#配置指定表的数据节点
spring.shardingsphere.sharding.tables.vouchers_of_sale.actual-data-nodes=master0.vouchers_of_sale_$->{1..2}
#主键生成策略
spring.shardingsphere.sharding.tables.vouchers_of_sale.key-generator.column=sale_id
spring.shardingsphere.sharding.tables.vouchers_of_sale.key-generator.type=SNOWFLAKE
#指定vouchers_of_sale表的分片策略,包括分片建表和分片算法
spring.shardingsphere.sharding.tables.vouchers_of_sale.table-strategy.inline.sharding-column=sale_id
spring.shardingsphere.sharding.tables.vouchers_of_sale.table-strategy.inline.algorithm-expression=vouchers_of_sale_$->{sale_id % 2+1}
spring.shardingsphere.props.sql.show = true
3.项目中如果有多个数据源,删除其他数据源。orm可以用jpa mybatis等。