0
我在默認配置中使用spring jpa,並在同一個項目中使用帶有自定義配置的spring-batis。 Jpa查詢總是很快,但是ibatis查詢有時很慢。什麼可能是錯的。我batis配置:ibatis與spring一起使用的緩慢更新jpa
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.transaction.managed.ManagedTransactionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.sql.DataSource;
import java.beans.PropertyVetoException;
@Configuration
@EnableTransactionManagement
@MapperScan(basePackages = {""}, sqlSessionFactoryRef = "mySqlSessionFactory")
public class DaoConfig {
@Value("${spring.datasource.url}")
private String databaseUrl;
@Value("${spring.datasource.username}")
private String username;
@Value("${spring.datasource.password}")
private String password;
@Value("${spring.datasource.driver-class-name}")
private String driverClassName;
@Value("${mybatis.mapperLocations}")
private Resource[] mapperLocation;
@Value("${mybatis.typeAliasesPackage}")
private String typeAliasesPackage;
private DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource(databaseUrl, username, password);
dataSource.setDriverClassName(driverClassName);
return dataSource;
}
private ManagedTransactionFactory dataSourceTransactionManager() throws PropertyVetoException {
return new ManagedTransactionFactory();
}
@Bean(name = "mySqlSessionFactory")
public SqlSessionFactory mySqlSessionFactory() throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setTransactionFactory(dataSourceTransactionManager());
sessionFactory.setMapperLocations(mapperLocation);
sessionFactory.setTypeAliasesPackage(typeAliasesPackage);
return sessionFactory.getObject();
}
}
和依賴:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
您的dataSource()和dataSourceTransactionManager()方法不應該使用@Bean註釋嗎?沒有這個,每次你調用mySqlSessionFactory()時你都會建立一個新的連接。 –
@MichaelPeacock你可以複製這個評論作爲答案。我覺得你是對的,我正在檢查它。謝謝! – ipoteka