2017-10-09 7 views
0

私は同じプロジェクトでカスタムconfigを使ってspringとspringを使います。 Jpaクエリは常に高速ですが、ibatisクエリは時には遅いです。何が間違っているかもしれない。マイbatisの設定:spring jpaと一緒に使用しているibatisのアップデートが遅い

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> 
+0

dataSource()メソッドとdataSourceTransactionManager()メソッドに@Beanアノテーションを付けるべきではありませんか?これがなければ、mySqlSessionFactory()を呼び出すたびに新しい接続が作成されます。 –

+0

@MichaelPeacockあなたは答えとしてこのコメントをコピーできますか?私はあなたが正しいと感じ、私はそれをチェックしています。どうも! – ipoteka

答えて

1

私は(あなたのDataSourceを考える)とdataSourceTransactionManager()メソッドは、同様に@Beanで注釈を付けるべきです。これがなければ、mySqlSessionFactory()を呼び出すたびに新しい接続が作成されます。

関連する問題