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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章