Insert statement does not support sharding table routing to multiple data nodes.【已解决sharding-jdbc错误】

2022-07-11 16:09:15 浏览数 (1)

错误原因:

导致这个错误的原因: 网上有的说实体类上添加@TableId的主键标识,但是不能解决小编的问题!

还有一种思路,就是自己的分片策略有问题,不是写错了就是sharding-jdbc的版本不同,yml的写法也是不同的。所以只能一点点的去看自己的配置,一定是==配置问题==,分片算法配置问题!!

小编产生的原因是4版本到5版本的改写,差距还是挺大的,==一定要看好格式,别多缩进和少缩进==

代码语言:javascript复制
### Error updating database.  Cause: java.lang.IllegalStateException: Insert statement does not support sharding table routing to multiple data nodes.
### The error may exist in com/example/demo/mapper/UserMapper.java (best guess)
### The error may involve com.example.demo.mapper.UserMapper.insert-Inline
### The error occurred while setting parameters
### SQL: INSERT INTO user  ( name, gender, data )  VALUES  ( ?, ?, ? )
### Cause: java.lang.IllegalStateException: Insert statement does not support sharding table routing to multiple data nodes.] with root cause

java.lang.IllegalStateException: Insert statement does not support sharding table routing to multiple data nodes.
	at com.google.common.base.Preconditions.checkState(Preconditions.java:508) ~[guava-30.0-jre.jar:na]
	at org.apache.shardingsphere.sharding.route.engine.validator.dml.impl.ShardingInsertStatementValidator.postValidate(ShardingInsertStatementValidator.java:101) ~[shardingsphere-sharding-core-5.1.1.jar:5.1.1]
	at org.apache.shardingsphere.sharding.route.engine.ShardingSQLRouter.lambda$createRouteContext$1(ShardingSQLRouter.java:57) ~[shardingsphere-sharding-core-5.1.1.jar:5.1.1]
	at java.util.Optional.ifPresent(Optional.java:159) ~[na:1.8.0_121]
	at org.apache.shardingsphere.sharding.route.engine.ShardingSQLRouter.createRouteContext(ShardingSQLRouter.java:57) ~[shardingsphere-sharding-core-5.1.1.jar:5.1.1]
	at 

下面给出5.1.1版本的简单分片yml,小编测试通过的哈!!

代码语言:javascript复制
spring:
  shardingsphere:
    # 是否开启
    datasource:
      # 数据源(逻辑名字)
      names: m1
      # 配置数据源
      m1:
        type: com.zaxxer.hikari.HikariDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://localhost:3306/test?useSSL=false&autoReconnect=true&characterEncoding=UTF-8&serverTimezone=UTC
        username: root
        password: root
    # 分片的配置
    rules:
      sharding:
        # 表的分片策略
        tables:
          # 逻辑表的名称
          user:
            # 数据节点配置,采用Groovy表达式
            actual-data-nodes: m1.user_$->{0..1}
            # 配置策略
            table-strategy:
              # 用于单分片键的标准分片场景
              standard:
                sharding-column: cid
                # 分片算法名字
                sharding-algorithm-name: user_inline
            key-generate-strategy: # 主键生成策略
              column: cid  # 主键列
              key-generator-name: snowflake  # 策略算法名称(推荐使用雪花算法)
        key-generators:
          snowflake:
            type: SNOWFLAKE
        sharding-algorithms:
          user_inline:
            type: inline
            props:
              algorithm-expression: user_$->{cid % 2}
    props:
      # 日志显示具体的SQL
      sql-show: true

Q.E.D.

0 人点赞