Spring Boot 整合 SardingSphere (多表(分))

废话不多说,差别在配置文件上

文章源码:https://gitee.com/yihong-sword/learn_shardingsphere

Tips:

  1. 只是将原本s_user的分表配置给copy将表名替换
  2. 其他源码,就不一一copy到这篇文章,参考:https://www.cnblogs.com/yi1036943655/p/15700170.html

application.properties

# 应用名称
spring.application.name=learn_shardingsphere
# 应用服务 WEB 访问端口
server.port=8085

# 数据源 sharding01 sharding02
spring.shardingsphere.datasource.names=sharding0,sharding1

# 第一个数据库
spring.shardingsphere.datasource.sharding0.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.sharding0.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.sharding0.jdbc-url=jdbc:mysql://localhost:3306/sharding0?serverTimezone=GMT%2B8&useSSL=false
spring.shardingsphere.datasource.sharding0.username=root
spring.shardingsphere.datasource.sharding0.password=root

# 第二个数据库
spring.shardingsphere.datasource.sharding1.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.sharding1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.sharding1.jdbc-url=jdbc:mysql://localhost:3306/sharding1?serverTimezone=GMT%2B8&useSSL=false
spring.shardingsphere.datasource.sharding1.username=root
spring.shardingsphere.datasource.sharding1.password=root

spring.shardingsphere.

# 水平拆分的数据库(表) 配置分库 + 分表策略 行表达式分片策略
# 分库策略
spring.shardingsphere.sharding.default-database-strategy.inline.sharding-column=id
spring.shardingsphere.sharding.default-database-strategy.inline.algorithm-expression=sharding$->{id % 2}

# 分表策略 其中 s_user 为逻辑表 分表主要取决于s_user.id的行
spring.shardingsphere.sharding.tables.s_user.actual-data-nodes=sharding$->{0..1}.s_user$->{0..1}
spring.shardingsphere.sharding.tables.s_user.table-strategy.inline.sharding-column=age
spring.shardingsphere.sharding.tables.s_user.table-strategy.inline.algorithm-expression=s_user$->{Integer.valueOf(age) % 2}

# 分表策略 其中 s_student 为逻辑表 分表主要取决于s_student.id的行
spring.shardingsphere.sharding.tables.s_student.actual-data-nodes=sharding$->{0..1}.s_student$->{0..1}
spring.shardingsphere.sharding.tables.s_student.table-strategy.inline.sharding-column=age
spring.shardingsphere.sharding.tables.s_student.table-strategy.inline.algorithm-expression=s_student$->{Integer.valueOf(age) % 2}

# 主键生成策略
# spring.shardingsphere.sharding.tables.s_user.key-generator.column=id
# spring.shardingsphere.sharding.tables.s_user.key-generator.type=SNOWFLAKE

# 打印执行的数据库以及语句
spring.shardingsphere.props.sql.show=true
spring.main.allow-bean-definition-overriding=true
logging.level.com.sharding.demo.mapper=DEBUG

# mybatis
mybatis.mapper-locations=classpath:/mappers/*.xml
posted @ 2021-12-26 20:27  一只奋斗的猪  阅读(315)  评论(0编辑  收藏  举报