shardingsphere 5.x beta + mybatis plus 配置

spring boot + shardingsphere 5.0.0 beta + mybatis plus 配置

版本

spring boot 2.5.2
shardingsphere 5.0.0-beta
mybatis plus 3.4.3.1
druid 1.2.6

表结构

create table t_business_1
(
    id          bigint auto_increment comment '主键' primary key,
    type        tinyint default 1 null,
    state       tinyint default 1 null comment '状态,1有效,0无效'
)
    comment '业务表';
    
create table t_business_2
(
    id          bigint auto_increment comment '主键' primary key,
    type        tinyint default 1 null,
    state       tinyint default 1 null comment '状态,1有效,0无效'
)
    comment '业务表';

创建项目

idea新建spring boot项目
idea新建spring boot项目

Java代码

MybatisX一键生成

依赖

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.2.6</version>
</dependency>

<dependency>
    <groupId>org.apache.shardingsphere</groupId>
    <artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId>
    <version>5.0.0-beta</version>
</dependency>

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.3.1</version>
</dependency>

配置文件

# 开发环境配置
server:
  # 服务器的HTTP端口,默认为80
  port: 80

# Spring配置
spring:
  shardingsphere:
    # 数据源配置
    datasource:
      # 数据源名称,多数据源以逗号分隔
      names: db0 #你想要的数据库名字
      db0:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://127.0.0.1:3306/junisyoan?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai
        username: junisyoan
        password: 123123
    # 规则配置
    rules:
      sharding:
        # 分片算法配置
        sharding-algorithms:
          table-inline:
            # 分片算法类型 https://shardingsphere.apache.org/document/current/cn/user-manual/shardingsphere-jdbc/configuration/built-in-algorithm/sharding/
            type: INLINE
            props:
              # 分片算法的行表达式
              algorithm-expression: t_business_$->{id % 2 + 1}
        # 分布式序列算法配置
        key-generators:
          snowflake:
            # 分布式序列算法(雪花算法:SNOWFLAKE; UUID:UUID)
            type: SNOWFLAKE
            # 分布式序列算法属性配置
            props:
              # 工作机器唯一标识
              worker-id: 123
        tables:
          # 逻辑表名称
          t_business:
            # 行表达式标识符可以使用 ${...} 或 $->{...},但前者与 Spring 本身的属性文件占位符冲突,因此在 Spring 环境中使用行表达式标识符建议使用 $->{...}
            actual-data-nodes: db0.t_business_$->{1..2}
            # 分表策略
            table-strategy:
              standard:
                # 分片列名称
                sharding-column: id
                # 分片算法名称
                sharding-algorithm-name: table-inline
            # 表主键算法
            key-generate-strategy:
              column: id
              key-generator-name: snowflake
    # 属性配置
    props:
      # 展示修改以后的sql语句
      sql-show: true

输出日志

2021-07-01 00:57:17.534  INFO 16404 --- [nio-8000-exec-4] ShardingSphere-SQL: Logic SQL: INSERT INTO t_business  ( type, state )  VALUES  ( ?, ? )
2021-07-01 00:57:17.534  INFO 16404 --- [nio-8000-exec-4] ShardingSphere-SQL: SQLStatement: MySQLInsertStatement(setAssignment=Optional.empty, onDuplicateKeyColumns=Optional.empty)
2021-07-01 00:57:17.534  INFO 16404 --- [nio-8000-exec-4] ShardingSphere-SQL: Actual SQL: db0 ::: INSERT INTO t_business_1  ( type, state , id)  VALUES  (?, ?, ?) ::: [1, 1, 617160953175715840]

2021-07-01 00:57:18.534  INFO 16404 --- [nio-8000-exec-5] ShardingSphere-SQL: Logic SQL: INSERT INTO t_business  ( type, state )  VALUES  ( ?,? )
2021-07-01 00:57:18.534  INFO 16404 --- [nio-8000-exec-5] ShardingSphere-SQL: SQLStatement: MySQLInsertStatement(setAssignment=Optional.empty, onDuplicateKeyColumns=Optional.empty)
2021-07-01 00:57:18.534  INFO 16404 --- [nio-8000-exec-5] ShardingSphere-SQL: Actual SQL: db0 ::: INSERT INTO t_business_2  ( type, state , id)  VALUES  (?, ?, ?) ::: [1, 1, 617160957370019841]

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值