Sharding-JDBC单库分表和多库分表其他都一样,就是在application.yml配置中设置下分库就可以了,单库分表链接
application.yml:
# 框架设置
spring:
# 读取配置文件
profiles:
active: dev #开发环境
# active: pro #正式环境
# mybatis
mybatis:
mapper-locations: classpath:mapper/*Mapper.xml
configuration:
map-underscore-to-camel-case: true
sharding:
jdbc:
datasource:
names: ds0,ds1
# 数据源ds0
ds0:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://xxx.xxx.xx.xx:3306/imageserver?serverTimezone=GMT%2B8&useSSL=false
username: canal
password: canal
# 数据源ds0
ds1:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://xx.xx.xx.xx:3306/imageserver1?serverTimezone=GMT%2B8&useSSL=false
username: root
password: BaiBing123!
config:
sharding:
props:
sql.show: true
tables:
tb_boot: # tb_boot表
key-generator-column-name: id #主键
actual-data-nodes: ds${0..1}.tb_boot${0..1} #数据节点,均匀分布
database-strategy: #分库策略
inline: #行表达式
sharding-column: sex #列名称,多个列以逗号分隔
algorithm-expression: ds${sex % 2} #按模运算分配
table-strategy: #分表策略
inline: #行表达式
sharding-column: id
algorithm-expression: tb_boot${id % 2} #按模运算分配
# 日志配置
logging:
config: classpath:logging/logback.xml
level:
cn.mapper: debug
psot请求添加: