sharding jdbc(sphere) 3.1.0 spring boot配置

  sharding jdbc 2.x系列详解参见https://www.cnblogs.com/zhjh256/p/9221634.html

  最近将sharding jdbc的配置从xml切换到了spring boot。首先引入依赖:

            <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版本占位符是${},如下:

    <sharding:inline-strategy id="orderTableStrategy" sharding-column="order_id" algorithm-expression="t_order_${order_id % 2}" />

  3.x版本则是$->{},原因是跟spring占位符冲突,如下:

sharding.jdbc.config.sharding.tables.ibf_acco_confirm.actual-data-nodes=ds$->{0..1}_$->{0..1}.ibf_acco_confirm

  规则:

复制代码
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/

  4.0系列:https://shardingsphere.apache.org/document/current/cn/manual/sharding-jdbc/configuration/config-spring-boot/

posted @   zhjh256  阅读(1763)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
历史上的今天:
2016-12-12 INFO: task java:27465 blocked for more than 120 seconds不一定是cache太大的问题
点击右上角即可分享
微信分享提示