package jp.dip.arimodoki.config;
import javax.sql.DataSource;
import org.apache.tomcat.dbcp.dbcp2.BasicDataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternUtils;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import jp.dip.arimodoki.common.CConst;
/**
* Mybatis設定クラス
* Datasourceを生成して、Mybatisを初期化する処理を行います。
*/
@Configuration
@PropertySource("classpath:application.properties")
public class MybatisConfig implements CConst {
/** JDBC接続情報 */
@Value("${jdbc.driverClassName}")
private String driverClassName;
@Value("${jdbc.url}")
private String jdbcURL;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Value("${jdbc.maxidle}")
private Integer maxIdle;
@Value("${jdbc.minidle}")
private Integer minIdle;
@Value("${jdbc.initialSize}")
private Integer initialSize;
@Value("${jdbc.defaultAutoCommit}")
private Boolean defaultAutoCommit;
/** Mybatis 環境設定 */
@Value("${mybatis.config}")
private String mybatisconfig;
@Value("${mybatis.mapperlocation}")
private String mybatismapperlocation;
@Value("${mybatis.typealiases}")
private String mybatistypealiases;
/**
* 唯一のBasicDataSourceインスタンス
*/
private static BasicDataSource appDataSource = null;
/**
* Tomcat dbcp BasicDataSource を生成する
* @return 生成したDataSource を返す
*/
@Bean
public DataSource getDataSource() {
if(appDataSource == null) {
appDataSource = new BasicDataSource();
appDataSource.setDriverClassName(this.driverClassName);
appDataSource.setUrl(this.jdbcURL);
appDataSource.setUsername(this.username);
appDataSource.setPassword(this.password);
appDataSource.setMaxIdle(this.maxIdle);
appDataSource.setMinIdle(this.minIdle);
appDataSource.setInitialSize(this.initialSize);
appDataSource.setDefaultAutoCommit(this.defaultAutoCommit);
}
return appDataSource;
}
/**
* トランザクションマネージャー
* @return DataSourceTransactionManagerを返す
*/
@Bean
public DataSourceTransactionManager transactionManager() {
DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
transactionManager.setDataSource(this.getDataSource());
return transactionManager;
}
/**
* MyBatis SqlSessionTemplate 生成
* @return 生成したSqlSessionTemplate を返す
* @throws Exception
*/
@Bean
public SqlSessionTemplate sqlSessionTemplate() throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(this.getDataSource());
ResourcePatternResolver resolver =
ResourcePatternUtils.getResourcePatternResolver(new DefaultResourceLoader());
bean.setConfigLocation(resolver.getResource(this.mybatisconfig));
bean.setMapperLocations(resolver.getResources(this.mybatismapperlocation));
bean.setTypeAliasesPackage(this.mybatistypealiases);
return new SqlSessionTemplate(bean.getObject());
}
}
|