有没有办法获得当前的执行者类型?

Is there a way to get the current executor type?

我正在尝试使用 for each 方法进行批量插入,但出现异常,告诉我无法更改 运行 执行程序类型。 这很奇怪,因为我有一个如此定义的 SQL 会话:

<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
    <constructor-arg index="0" ref="sqlSessionFactory" />
    <constructor-arg index="1" value="BATCH" />
</bean>

无论哪种方式,我都想检查当前执行程序类型是什么,以确保它是 BATCH 类型。怎么做?我在 SQLSession...

中看不到任何方法

在你的例子中 sqlSession 是类型 SqlSessionTemplate 并且它有 getExecutorType() 方法所以你只需要使用正确的类型并有几个选项来做到这一点

  1. SqlSessionType 而不是普通的 SqlSession 注入到您需要访问执行程序类型的服务中
  2. SqlSession转换为SqlSessionTemplate然后使用getExecutorType
  3. 注入 SqlSessionType 的 属性 将 SPEL 用于服务,如下所示:

@Value("#{sqlSession.executorType}") private ExecutorType executorType;