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

相关推荐
xuanjiong2 小时前
Javaweb项目--Mybatis,导入com.mysql.cj.jdbc.Driver时报错,Cannot resolve class ‘Driver‘
数据库·mysql·mybatis
计算机学姐19 小时前
基于SpringBoot的同城宠物照看管理系统
java·vue.js·spring boot·后端·mysql·mybatis·宠物
爱尚你199320 小时前
MyBatis 核心类详解与架构解析:从入门到源码级理解
mybatis
小杜-coding1 天前
黑马点评day01(基于Redis)
java·数据库·spring boot·redis·spring·缓存·mybatis
Code哈哈笑1 天前
【图书管理系统】环境介绍、设计数据库和表、配置文件、引入依赖
java·数据库·spring boot·后端·mybatis
漫步者TZ2 天前
【Mytais系列】介绍、核心概念
mybatis
magic 2452 天前
深入理解 MyBatis 代理机制
mybatis
昔我往昔3 天前
使用mybatis实例类和MySQL表的字段不一致怎么办
java·面试·mybatis
Code哈哈笑3 天前
【SpringBoot】Spring中事务的实现:声明式事务@Transactional、编程式事务
java·spring boot·后端·spring·mybatis
geekmice3 天前
通过SpringBoot+H2数据库+Mybatis实现DAO单元测试
数据库·spring boot·mybatis