有没有办法为MyBatis配置进行可定制的超时?MyBatis上的可定制超时
我在Spring框架中使用MyBatis,但是我无法将'defaultStatementTimeout'属性作为Spring的PropertyPlaceHolder进行定制。
有没有办法为MyBatis配置进行可定制的超时?MyBatis上的可定制超时
我在Spring框架中使用MyBatis,但是我无法将'defaultStatementTimeout'属性作为Spring的PropertyPlaceHolder进行定制。
有一种方法,但只能通过MyBatis配置文件。您可以在Spring配置文件中添加的MyBatis配置文件的位置(也就是在MyBatis的页面example)为负载settings要:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="location" value="classpath:mybatis-config.xml" />
</bean>
MyBatis的配置文件可以是:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="defaultStatementTimeout" value="10"/> <!-- seconds -->
</settings>
</configuration>
我已经解决了我的问题。我不得不重写SqlSessionFactoryBean
与CustomSqlSessionFactoryBean.
<bean id="sqlSessionFactory" class="com.myapp.CustomSqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:sql-config.xml" />
<property name="timeout" value="${custom.timeout}" />
</bean>
我的MyBatis配置文件看起来像
<configuration>
<settings>
<setting name="defaultStatementTimeout" value="${custom.timeout}"/>
</settings>
</configuration>
我的SqlSessionFactoryBean
自定义实现覆盖buildSqlSessionFactory()
方法,即设置超时(替换$ {custom.timeout }通过MyBatis配置文件中的超时值)。
@Override
protected SqlSessionFactory buildSqlSessionFactory() throws IOException {
String strConfig = IOUtils.toString(CustomSqlSessionFactoryBean.class.getResourceAsStream(MYBATIS_CONFIG_PATH));
strConfig = StringUtils.replace(strConfig, TO_REPLACE, String.valueOf(timeout));
InputStream inputStream = IOUtils.toInputStream(strConfig);
setConfigLocation(new CustomClasspathResource(inputStream, strConfig));
return super.buildSqlSessionFactory();
}
这个类让我将修改的输入流设置为setConfigLocation
方法。
class CustomClasspathResource extends ClassPathResource {
private InputStream inputStream;
@Override
public InputStream getInputStream() throws IOException {
return inputStream;
}
private CustomClasspathResource(InputStream inputStream, String path) {
super(path);
this.inputStream = inputStream;
}
因此,超时可用${custom.timeout}
变量进行自定义。
对于Spring Batch的它是在映射语句中使用属性“超时”使用属性“超时”的豆
<bean id="FlxxtUpdaterTasklet" class="org.springframework.batch.core.step.tasklet.SystemCommandTasklet">
<property name="command" value="xxx" />
<property name="timeout" value="3600000" />
<property name="interruptOnCancel" value="true" />
<property name="terminationCheckInterval" value="5000" />
</bean>
了MyBatis的覆盖声明超时上SystemCommandTasklet可能一套定制超时:
<mapper namespace="com.xxxx.blsf.batch.mapperbls.Bls2CsvMapper">
<select id="bls2csv" parameterType="com.xxxx.blsf.batch.mapperparams.SpParamInteger"
statementType="CALLABLE" timeout="6000" >
{ CALL PKG_BLACKLIST_PLUS_RC.BLS_2_CSV(#{rcInt, mode=OUT, jdbcType=INTEGER, javaType=java.lang.Integer})}
</select>
</mapper>
嗨,谢谢你的回答。但是,这不是可定制的。 – rvillablanca