You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
2.0 KiB
Java
46 lines
2.0 KiB
Java
package com.example.config;
|
|
|
|
import org.apache.ibatis.session.SqlSessionFactory;
|
|
import org.mybatis.spring.SqlSessionFactoryBean;
|
|
import org.mybatis.spring.SqlSessionTemplate;
|
|
import org.mybatis.spring.annotation.MapperScan;
|
|
import org.springframework.beans.factory.annotation.Qualifier;
|
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
|
import org.springframework.boot.jdbc.DataSourceBuilder;
|
|
import org.springframework.context.annotation.Bean;
|
|
import org.springframework.context.annotation.Configuration;
|
|
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
|
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
|
|
|
import javax.sql.DataSource;
|
|
|
|
|
|
@Configuration
|
|
@MapperScan(basePackages = "com.example.db2.dao", sqlSessionTemplateRef = "db2SqlSessionTemplate")
|
|
public class DataSource2Config {
|
|
|
|
@Bean(name = "db2DataSource")
|
|
@ConfigurationProperties(prefix = "spring.datasource.hikari.db2")
|
|
public DataSource testDataSource() {
|
|
return DataSourceBuilder.create().build();
|
|
}
|
|
|
|
@Bean(name = "db2SqlSessionFactory")
|
|
public SqlSessionFactory testSqlSessionFactory(@Qualifier("db2DataSource") DataSource dataSource) throws Exception {
|
|
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
|
|
bean.setDataSource(dataSource);
|
|
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/db2/*.xml"));
|
|
return bean.getObject();
|
|
}
|
|
|
|
@Bean(name = "db2TransactionManager")
|
|
public DataSourceTransactionManager testTransactionManager(@Qualifier("db2DataSource") DataSource dataSource) {
|
|
return new DataSourceTransactionManager(dataSource);
|
|
}
|
|
|
|
@Bean(name = "db2SqlSessionTemplate")
|
|
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("db2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
|
|
return new SqlSessionTemplate(sqlSessionFactory);
|
|
}
|
|
}
|