这个问题是关于在spring web应用程序中使用MyBatis。我需要在应用程序中执行批处理sql语句(例如,bulk insert )。从文档中我读到了使用SqlSessionTemplate的批处理特性的MyBatisBatchItemWriter类。激活此功能会影响依赖它的其他映射器吗?定义另一个专门用于批处理语句的SqlSessionTemplate是否明智?就像下面提供的代码一样?
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
">
<tx:annotation-driven transaction-manager="txManager" />
<!--
a PlatformTransactionManager is still required -->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- (this dependency is defined somewhere else) -->
<property name="dataSource" ref="datasourceref" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="${mpi.datasource.type}" />
<property name="configLocation" value="/WEB-INF/x1v1-mybatisconfig.xml" />
</bean>
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg ref="sqlSessionFactory" />
</bean>
<bean id="batchSqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
<constructor-arg index="1" value="BATCH" />
</bean>
</beans>发布于 2016-12-05 23:13:07
使用批处理执行器类型可能与某些服务相关,而与其他服务无关。这似乎是一个好主意,因为它让事情变得更清楚:当你知道为什么时使用batch,否则就不使用它们。因为批处理效果可能令人费解:如果您调试代码,批处理语句在到达代码行时并不实际执行,它们以某种方式被调度为在下一次刷新时实际执行,或者显式触发,或者由中间select语句隐式触发(默认行为)。请记住,行为将持续会话时间,这是一个截然不同的(独立的)会话。
https://stackoverflow.com/questions/37722083
复制相似问题