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

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

  1. <dependency>
  2. <groupId>io.shardingsphere</groupId>
  3. <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
  4. <version>3.1.0</version>
  5. </dependency>

  然后按照https://shardingsphere.apache.org/document/legacy/3.x/document/en/manual/sharding-jdbc/configuration/config-spring-boot/配置即可,基本上和xml配置是一样的。

  需要注意的是,2.x版本占位符是${},如下:

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

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

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

  规则:

  1. sharding.jdbc.config.sharding.tables.ibf_acco_confirm.actual-data-nodes=ds$->{0..1}.ibf_acco_confirm
    # 分库策略,定义了目标库列表,没有定义分库策略的,默认为default-database-strategy,实际上分库策略应该就一种,所以应不定义才对
  1. 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}
    # 分表策略
  2. sharding.jdbc.config.sharding.tables.t_order_item.table-strategy.inline.sharding-column=order_id
  3. sharding.jdbc.config.sharding.tables.t_order_item.table-strategy.inline.algorithm-expression=t_order_item$->{order_id % 2}
    # 自定生成id值的列,一般用ID中心,不应使用具体框架的
  4. sharding.jdbc.config.sharding.tables.t_order_item.key-generator-column-name=order_item_id
    # 主子表关系定义
  5. #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
  1. 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/

sharding jdbc(sphere) 3.1.0 spring boot配置的更多相关文章

  1. Spring Boot -- 配置切换指南

    一般在一个项目中,总是会有好多个环境.比如: 开发环境 -> 测试环境 -> 预发布环境 -> 生产环境 每个环境上的配置文件总是不一样的,甚至开发环境中每个开发者的环境可能也会有一 ...

  2. Spring Boot 配置优先级顺序

    一般在一个项目中,总是会有好多个环境.比如: 开发环境 -> 测试环境 -> 预发布环境 -> 生产环境 每个环境上的配置文件总是不一样的,甚至开发环境中每个开发者的环境可能也会有一 ...

  3. spring boot配置mybatis和事务管理

    spring boot配置mybatis和事务管理 一.spring boot与mybatis的配置 1.首先,spring boot 配置mybatis需要的全部依赖如下: <!-- Spri ...

  4. Spring Boot配置多数据源并实现Druid自动切换

    原文:https://blog.csdn.net/acquaintanceship/article/details/75350653 Spring Boot配置多数据源配置yml文件主数据源配置从数据 ...

  5. spring boot配置druid连接池连接mysql

    Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...

  6. Redis篇之操作、lettuce客户端、Spring集成以及Spring Boot配置

    Redis篇之操作.lettuce客户端.Spring集成以及Spring Boot配置 目录 一.Redis简介 1.1 数据结构的操作 1.2 重要概念分析 二.Redis客户端 2.1 简介 2 ...

  7. Spring boot配置多个Redis数据源操作实例

    原文:https://www.jianshu.com/p/c79b65b253fa Spring boot配置多个Redis数据源操作实例 在SpringBoot是项目中整合了两个Redis的操作实例 ...

  8. [转] Spring Boot配置多个DataSource

    [From]  https://www.liaoxuefeng.com/article/001484212576147b1f07dc0ab9147a1a97662a0bd270c20000 Sprin ...

  9. Spring boot 配置异步处理执行器

    示例如下: 1. 新建Maven 项目 async-executor 2.pom.xml <project xmlns="http://maven.apache.org/POM/4.0 ...

随机推荐

  1. getsockopt套接口选项

    1. getsockopt int getsockopt(int sockfd, int level, int optname, void *optval, socklen_t *optlen); i ...

  2. Prometheus学习

    简介 Prometheus 最初是 SoundCloud 构建的开源系统监控和报警工具,是一个独立的开源项目,于2016年加入了 CNCF 基金会,作为继 Kubernetes 之后的第二个托管项目. ...

  3. C#8.0—非空引用类型

    非空引用类型--C#8.0 原文地址:https://devblogs.microsoft.com/dotnet/try-out-nullable-reference-types/?utm_sourc ...

  4. js 提取特定的时间区间段

    项目中遇到问题,需要根据用户的选择提取出一个时间的区间段,然后对后台进行请求. 基本思路,先根据new Date()对象求出start_time和end_time时间戳,然后把时间戳转化成new Da ...

  5. Microsoft SQL Server 2012安装说明

    Microsoft SQL Server 2012安装说明 环境:Windows8, Windows7, WinVista, Win2003, WinXP Microsoft SQL Server 2 ...

  6. java.io.IOException: Premature EOF

    http访问第三方系统的接口时,小概率抛出下面的异常: java.io.IOException: Premature EOF at sun.net.www.http.ChunkedInputStrea ...

  7. 带你快速上手前端三剑客之css

    CSS介绍 ​ CSS(Cascading Style Sheet , 层叠样式表)定义如何显示HTML元素.当浏览器读到一个样式表,它就会按照这个样式表来对文档进行格式化(渲染) 组成 ​ 每个CS ...

  8. php调停者模式(mediator pattern)

    差不多了,睡一觉,下次再弄. <?php /* The more classes we have in our software, the more complex their communic ...

  9. 图论 - PAT乙级 1029 旧键盘 (C++ python3)

    PAT甲级 1029 旧键盘 (C++ python3) 旧键盘上坏了几个键,于是在敲一段文字的时候,对应的字符就不会出现.现在给出应该输入的一段文字.以及实际被输入的文字,请你列出肯定坏掉的那些键. ...

  10. Gym - 247731E :room(最小费用流裸题)

    题意:有N个宿舍(N<200),给出第一年每个宿舍有哪4个同学.现在给出N个4元组y[][4],表示这4个人想住一起,问最少多少人需要换宿舍. 思路:费用流,每个4元组y[]到每个宿舍连边,流量 ...