properties 配置文件片段:
# 数据源
sharding.jdbc.datasource.names=test,test_0
# 数据库配置
sharding.jdbc.datasource.test.type=com.alibaba.druid.pool.DruidDataSource
sharding.jdbc.datasource.test.driver-class-name=com.mysql.jdbc.Driver
sharding.jdbc.datasource.test.url=jdbc:mysql://127.0.0.1:3306/demo
sharding.jdbc.datasource.test.username=root
sharding.jdbc.datasource.test.password=123
sharding.jdbc.datasource.test_0.type=com.alibaba.druid.pool.DruidDataSource
sharding.jdbc.datasource.test_0.driver-class-name=com.mysql.jdbc.Driver
sharding.jdbc.datasource.test_0.url=jdbc:mysql://127.0.0.1:3306/demo
sharding.jdbc.datasource.test_0.username=root
sharding.jdbc.datasource.test_0.password=123
# 默认数据源
sharding.jdbc.config.sharding.default-data-source-name=test
# 分表策略 t_order t_log
sharding.jdbc.config.sharding.tables.t_order.actual-data-nodes=test_$->{0..1}.t_order_$->{0..1}
sharding.jdbc.config.sharding.tables.t_order.table-strategy.inline.sharding-column=order_id
sharding.jdbc.config.sharding.tables.t_order.table-strategy.inline.algorithm-expression=t_order_$->{order_id % 2}
sharding.jdbc.config.sharding.tables.t_order.key-generator-column-name=order_id
sharding.jdbc.config.sharding.tables.t_log.actual-data-nodes=test_$->{0..1}.t_log_$->{0..1}
sharding.jdbc.config.sharding.tables.t_log.table-strategy.inline.sharding-column=id
sharding.jdbc.config.sharding.tables.t_log.table-strategy.inline.algorithm-expression=t_log_$->{id % 2}
sharding.jdbc.config.sharding.tables.t_log.key-generator-column-name=id
# 分库策略 - 注:分库ID不能与分表ID一样
sharding.jdbc.config.sharding.default-database-strategy.inline.sharding-column=user_id
sharding.jdbc.config.sharding.default-database-strategy.inline.algorithm-expression=ds_$->{user_id % 2}
yml 配置文件片段:
yml 有几个需要注意的地方。
driver-class-name 需要改成 driverClassName
url 根据版本不同有一定变化
sharding:
jdbc:
datasource:
names: test,test_0
test:
type: com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.jdbc.Driver
jdbcUrl: jdbc:mysql://localhost:3306/test?tinyInt1isBit=false&useUnicode=true&characterEncoding=utf-8&autoReconnect=true&failOverReadOnly=false&zeroDateTimeBehavior=convertToNull&useSSL=false
username: root
password: 123
test_0:
type: com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.jdbc.Driver
jdbcUrl: jdbc:mysql://localhost:3306/test?tinyInt1isBit=false&useUnicode=true&characterEncoding=utf-8&autoReconnect=true&failOverReadOnly=false&zeroDateTimeBehavior=convertToNull&useSSL=false
username: root
password: 123
config:
sharding:
default-data-source-name: test
tables:
order:
actual-data-nodes: test_$->{0..1}.order_$->{0..1}
table-strategy:
inline:
sharding-column: order_id
algorithm-expression: order_$->{id % 2}
key-generator-column-name: order_id
default-database-strategy:
inline:
sharding-column: user_id
algorithm-expression: order_$->{user_id % 2}
网友评论