ShardingJDBC简单使用

Sharding-JDBC是当当网开源的适用于微服务的分布式数据访问基础类库,完整的实现了分库分表读写分离分布式主键功能,并初步实现了柔性事务。Sharding-JDBC定位为轻量级Java框架,在Java的JDBC层提供的额外服务。其适用于任何基于JDBC的ORM框架,如:JPA, Hibernate, Mybatis, Spring JDBC Template 或直接使用 JDBC。支持任何第三方的数据库连接池,如:DBCP, C3P0, BoneCP, Druid, HikariCP等。支持任意实现JDBC规范的数据库,目前支持MySQL,Oracle,SQLServer,PostgreSQL以及任何遵循SQL92标准的数据库。
Sharding-JDBC可支持等号、between、in等多维度分片,也可支持多分片键。SQL解析功能完善,支持聚合、分组、排序、limit、or等查询,并支持Binding Table以及笛卡尔积表查询。
案例:

  shardingsphere:
    datasource:
      ##定义主从数据源
      names: master,slave
      master:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3306/microcloud?useSSL=false&useUnicode=true&characterEncoding=utf8
        username: xxxx
        password: xxxx
      slave:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3307/microcloud?useSSL=false&useUnicode=true&characterEncoding=utf8
        username: xxxx
        password: xxxx
        maxPoolSize: 100
        minPoolSize: 5
    sharding:
      tables:
        t_order:
          actualDataNodes: ds$->{1}.t_order_$->{1..2}
          tableStrategy:
            inline:
              shardingColumn: id
              algorithmExpression: t_order_$->{id%2+1}
          keyGenerator:
            type: SNOWFLAKE
            column: id
      master-slave-rules:
        ds1:
          masterDataSourceName: master
          slaveDataSourceNames: slave
          ## loadBalanceAlgorithmType: ROUND_ROBIN
  • 18
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kk变色龙13

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值