package com.dfj.config;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import javax.sql.DataSource;
@Configuration
public class DataSourceConfig {
@Bean
@ConfigurationProperties(prefix = "spring.mg-rw-datasource")
public DataSource mgDataSource() {
return DruidDataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties(prefix = "spring.ex-ro-datasource")
public DataSource exDataSource() {
return DruidDataSourceBuilder.create().build();
}
}
mg
package com.dfj.config;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
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 org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import tk.mybatis.spring.annotation.MapperScan;
import javax.sql.DataSource;
@Configuration
@EnableTransactionManagement
@MapperScan(basePackages = "com.itranswarp.crypto.manage.mapper.mg",sqlSessionTemplateRef = "mgSqlSessionTemplate")
public class MgDataSourceConfig {
@Autowired
@Qualifier("mgDataSource")
private DataSource mgDS;
@Bean
public SqlSessionFactory mgSqlSessionFactory() throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
tk.mybatis.mapper.session.Configuration configuration = new tk.mybatis.mapper.session.Configuration();
configuration.setMapUnderscoreToCamelCase(true);
bean.setConfiguration(configuration);
bean.setDataSource(mgDS);
bean.setTypeAliasesPackage("com.itranswarp.crypto.manage.web.mvc.bean.model.mg");
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:*.xml"));
return bean.getObject();
}
@Bean
public PlatformTransactionManager mgTransactionManager() {
return new DataSourceTransactionManager(mgDS);
}
@Bean
public SqlSessionTemplate mgSqlSessionTemplate(@Qualifier("mgSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
uiデータソースとトランザクションのバインディングを設定します.
package com.dfj.config;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
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 org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import tk.mybatis.spring.annotation.MapperScan;
import javax.sql.DataSource;
@Configuration
@EnableTransactionManagement
@MapperScan(basePackages = "com.itranswarp.crypto.manage.mapper.ui",sqlSessionTemplateRef = "uiSqlSessionTemplate")
public class UiDataSourceConfig {
@Autowired
@Qualifier("uiDataSource")
private DataSource uiDS;
@Bean
public SqlSessionFactory uiSqlSessionFactory() throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(uiDS);
bean.setTypeAliasesPackage("com.itranswarp.crypto.manage.web.mvc.bean.model.ui");
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:*.xml"));
return bean.getObject();
}
@Bean
public PlatformTransactionManager uiTransactionManager() {
return new DataSourceTransactionManager(uiDS);
}
@Bean
public SqlSessionTemplate uiSqlSessionTemplate(@Qualifier("uiSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}