分表的键为id, 且用数据库自增主键时, last insert id未正确回写到对象.

方便更快捷的说明问题,可以按需填写(可删除)

使用环境:

ShardingSphere 5.3.1, mybatis-plus

场景:

想所有的insert 都插入到最新的表里, 然后有一些归档表(手动将老数据归档到归档表). 这种场景下其实我是可以用自增主键的. 但在mybatis执行完成, keyGenerator获取key的时候,会报

ResultSet should call next or has no more data. 的错误…

场景、问题:

我的配置:

mode:
  type: Standalone
  repository:
    type: JDBC

dataSources:
  ds_0:
    dataSourceClassName: com.zaxxer.hikari.HikariDataSource
#    jdbc-url: jdbc:mysql://127.0.0.1:3306/test2?useUnicode=true&characterEncoding=utf-8
    url: jdbc:mysql://127.0.0.1:3306/test2?useUnicode=true&characterEncoding=utf-8
    password: root
    username:
    driver-class-name: com.mysql.jdbc.Driver

rules:
#- !TRANSACTION
#  defaultType: LOCAL
- !SHARDING
  defaultKeyGenerateStrategy:
    column: id
    keyGeneratorName: autoinc
  defaultDatabaseStrategy:
    standard:
      shardingColumn: id
      shardingAlgorithmName: db_master
  tables:
    user:
      actualDataNodes: ds_0.user
      tableStrategy:
        standard:
          shardingColumn: id
          shardingAlgorithmName: user_archive
      keyGenerateStrategy:
        column: id
        keyGeneratorName: autoinc

  keyGenerators:
    uuid:
      type: UUID
    snowflake:
      type: SNOWFLAKE
    autoinc:
      type: AUTOINC
    random:
      type: RANDOM
  shardingAlgorithms:
    db_master:
      type: INLINE
      props:
        algorithm-expression: ds_0
    user_archive:
      type: CLASS_BASED
      props:
        strategy: STANDARD
        algorithmClassName: com.ff.code.sharding.UserShardingAlgorithm

props:
  sql-show: true

public class UserShardingAlgorithm implements StandardShardingAlgorithm<Long> {
    @Override
    public String getType() {
        return "USER_SHARD";
    }

    @Override
    public String doSharding(Collection<String> availables, PreciseShardingValue<Long> shardingValue) {
        String column = shardingValue.getColumnName();
        Long value = shardingValue.getValue();
        return "user";
    }

    @Override
    public Collection<String> doSharding(Collection<String> collection, RangeShardingValue<Long> rangeShardingValue) {
        return null;
    }

    @Override
    public Properties getProps() {
        return null;
    }

    @Override
    public void init(Properties properties) {

    }
}

keyGenerator

public class AutoIncrementKeyGeneratorAlgorithm implements KeyGenerateAlgorithm {
    @Override
    public Comparable<?> generateKey() {
        return null;
    }

    @Override
    public Properties getProps() {
        return null;
    }

    @Override
    public String getType() {
        return "AUTOINC";
    }

    @Override
    public void init(Properties properties) {

    }
}
@Data
public class User {
    @TableId(type = IdType.AUTO)
    private Long id;
    private String uu;
}
CREATE TABLE `user` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `uu` longtext NOT NULL
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

用snowflake生成id可以正常执行, 但依赖数据库自增主键就报错了.

Thanks

解决了吗,我遇到的是在一个事务里面循环插入,返回的id是第一次插入的id

京ICP备2021015875号