Sharding Sphere JDBC使用Mybatis的saveBatch无法返回主键的问题

问题背景

项目中使用了MybatisPlus框架,数据库是PostgreSQL,配置了主键自增,新增数据后返回主键到实体类中。

项目中因为数据量问题,需要用到分库分表,因此引入了Sharding Sphere JDBC框架。但是Sharding Sphere JDBC会读取sql语句,根据分库分分表规则,重新组合sql语句,到这一步还没有问题。使用Mybatis的批量新增之后,无法获取到数据库自增产生的ID,实体类中id字段为空。

原因分析

原因应该是Sharding Sphere JDBC重写了statment,没有将数据库返回的结果保存下来

这个应该是Sharding Sphere与ORM框架整合的bug

Github中有人提出这个问题,官方回答他们没人时间去阅读第三方ORM的框架代码,只对JDBC做兼容,意思就是说他们不管这个bug😂

https://github.com/apache/shardingsphere/issues/9592

解决方案

1.配置多数据源,分表的地方使用ShardingSphere,其他表使用正常的ORM框架

2.Github大佬提供的解决办法:

通过重写ShardingSpherePreparedStatement类中的addBatch方法,将statment缓存下来

注意这个版本必须是5.2.1版本以上的才可以使用

java 复制代码
public void addBatch() {
    try {
        QueryContext queryContext = this.createQueryContext();
        this.trafficInstanceId = this.getInstanceIdAndSet(queryContext).orElse(null);
        this.executionContext = null != this.trafficInstanceId ? this.createExecutionContext(queryContext, this.trafficInstanceId) : this.createExecutionContext(queryContext);
        this.batchPreparedStatementExecutor.addBatchForExecutionUnits(this.executionContext.getExecutionUnits());

        // 这里加一下缓存statement,cacheBatchStatements是新方法
        ExecutionGroupContext<JDBCExecutionUnit> executionGroupContext = this.createExecutionGroupContext();
        this.cacheBatchStatements(executionGroupContext.getInputGroups());
    } catch (SQLException e) {
        throw new RuntimeException(e);
    } finally {
        this.currentResultSet = null;
        this.clearParameters();
    }
}

private void cacheBatchStatements(Collection<ExecutionGroup<JDBCExecutionUnit>> executionGroups) throws SQLException {
    Iterator var2 = executionGroups.iterator();

    while(var2.hasNext()) {
        ExecutionGroup<JDBCExecutionUnit> each = (ExecutionGroup<JDBCExecutionUnit>)var2.next();
        each.getInputs().forEach((eachInput) -> {
            if (!this.statements.contains((PreparedStatement)eachInput.getStorageResource())) {
                this.statements.add((PreparedStatement)eachInput.getStorageResource());
            }
        });
    }
}
xml 复制代码
<dependency>
    <groupId>org.apache.shardingsphere</groupId>
    <artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId>
    <version>5.2.1</version>
</dependency>

github大佬地址:https://github.com/baomidou/mybatis-plus/issues/3207

相关推荐
sensenlin912 天前
Mybatis中SQL全大写或全小写影响执行性能吗
数据库·sql·mybatis
BXCQ_xuan2 天前
软件工程实践四:MyBatis-Plus 教程(连接、分页、查询)
spring boot·mysql·json·mybatis
wuyunhang1234562 天前
Redis----缓存策略和注意事项
redis·缓存·mybatis
lunz_fly19922 天前
【源码解读之 Mybatis】【基础篇】-- 第2篇:配置系统深度解析
mybatis
森林-2 天前
MyBatis 从入门到精通(第一篇)—— 框架基础与环境搭建
java·tomcat·mybatis
森林-2 天前
MyBatis 从入门到精通(第三篇)—— 动态 SQL、关联查询与查询缓存
sql·缓存·mybatis
java干货2 天前
MyBatis 的“魔法”:Mapper 接口是如何找到并执行 SQL 的?
数据库·sql·mybatis
嬉牛3 天前
项目日志输出配置总结(多数据源MyBatis+Logback)
mybatis·logback
哈喽姥爷4 天前
Spring Boot--yml配置信息书写和获取
java·数据库·spring boot·mybatis
奔跑你个Run5 天前
mybatis plus 使用wrapper输出SQL
mybatis