前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Insert statement does not support sharding table routing to multiple data nodes.【已解决sharding-jdbc错误】

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

作者头像
掉发的小王
发布2022-07-11 16:09:15
1.7K0
发布2022-07-11 16:09:15
举报
文章被收录于专栏:小王知识分享

错误原因:

导致这个错误的原因: 网上有的说实体类上添加@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.

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-06-16,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 错误原因:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档