sharding jdbc 2.x系列详解参见https://www.cnblogs.com/zhjh256/p/9221634.html。html
最近将sharding jdbc的配置从xml切换到了spring boot。首先引入依赖:node
<dependency> <groupId>io.shardingsphere</groupId> <artifactId>sharding-jdbc-spring-boot-starter</artifactId> <version>3.1.0</version> </dependency>
而后按照https://shardingsphere.apache.org/document/legacy/3.x/document/en/manual/sharding-jdbc/configuration/config-spring-boot/配置便可,基本上和xml配置是同样的。算法
须要注意的是,2.x版本占位符是${},以下:spring
<sharding:inline-strategy id="orderTableStrategy" sharding-column="order_id" algorithm-expression="t_order_${order_id % 2}" />
3.x版本则是$->{},缘由是跟spring占位符冲突,以下:express
sharding.jdbc.config.sharding.tables.ibf_acco_confirm.actual-data-nodes=ds$->{0..1}_$->{0..1}.ibf_acco_confirm
规则:apache
sharding.jdbc.config.sharding.tables.ibf_acco_confirm.actual-data-nodes=ds$->{0..1}.ibf_acco_confirm
# 分库策略,定义了目标库列表,没有定义分库策略的,默认为default-database-strategy,实际上分库策略应该就一种,因此应不定义才对
sharding.jdbc.config.sharding.tables.ibf_acco_confirm.database-strategy.hint.algorithm-class-name=com.hundsun.ta.tabase.sharding.DatabaseShardingAlgorithmByHint
# 表的目标库列表
sharding.jdbc.config.sharding.tables.t_order_item.actual-data-nodes=ds$->{0..1}.t_order_item$->{0..1}
# 分表策略 sharding.jdbc.config.sharding.tables.t_order_item.table-strategy.inline.sharding-column=order_id sharding.jdbc.config.sharding.tables.t_order_item.table-strategy.inline.algorithm-expression=t_order_item$->{order_id % 2}
# 自定生成id值的列,通常用ID中心,不该使用具体框架的 sharding.jdbc.config.sharding.tables.t_order_item.key-generator-column-name=order_item_id
# 主子表关系定义 #sharding.jdbc.config.sharding.binding-tables=t_order,t_order_item
# 广播表,既不在sharding.jdbc.config.sharding.tables定义,又没有定义在broadcast-tables定义的就是全局表
#sharding.jdbc.config.sharding.broadcast-tables=t_config
# 默认的分库策略,通常不该使用行内算法(groovy的),建议使用algorithm-class-name
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}
参考:框架
3.1.0系列:https://shardingsphere.apache.org/document/legacy/3.x/document/en/manual/sharding-jdbc/configuration/config-spring-boot/spring-boot
4.0系列:https://shardingsphere.apache.org/document/current/cn/manual/sharding-jdbc/configuration/config-spring-boot/spa