数据源配置
package com.base.configs;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
@Configuration
public class DataSourceConfig {
@Bean(name = "lawyerDataSourceProperties")
@Qualifier("lawyerDataSourceProperties")
@ConfigurationProperties("spring.datasource")
public DataSourceProperties lawyerDataSourceProperties() {
return new DataSourceProperties();
}
@Bean(name = "lawyerDataSource")
@Qualifier("lawyerDataSource")
@ConfigurationProperties(prefix="spring.datasource")
public DataSource lawyerDataSource() {
return lawyerDataSourceProperties().initializeDataSourceBuilder().build();
}
@Bean(name = "b2bDataSourceProperties")
@Qualifier("b2bDataSourceProperties")
@ConfigurationProperties("spring.primary")
@Primary
public DataSourceProperties b2bDataSourceProperties() {
return new DataSourceProperties();
}
@Bean(name = "b2bDataSource")
@Qualifier("b2bDataSource")
@ConfigurationProperties(prefix="spring.primary")
@Primary
public DataSource b2bDataSource() {
return b2bDataSourceProperties().initializeDataSourceBuilder().build();
}
}
数据源注入
- 第一个数据源
package com.base.configs;
import javax.sql.DataSource;
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.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
@Configuration
@MapperScan(basePackages = {B2bDataSourceConfig.MAPPER_PACKAGE}, sqlSessionFactoryRef = "b2bSqlSessionFactory")
public class B2bDataSourceConfig {
public static final String MAPPER_PACKAGE = "com.base.b2b.dao";
public static final String MAPPER_XML_PACKAGE = "classpath:mybatis/mapper/b2b/**/*.xml";
public static final String MYBATIS_BEAN_PACKAGE = "com.base.entities.b2b";
public static final String CONFIG_LOCATION = "classpath:mybatis/mybatis.cfg.xml";
@Autowired
@Qualifier("b2bDataSource")
private DataSource b2bDataSource;
@Bean(name = "b2bSqlSessionFactory")
@Primary
public SqlSessionFactory sqlSessionFactory() throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(b2bDataSource);
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
//设置扫描 mybatis-config.xml
sqlSessionFactoryBean.setConfigLocation(resolver.getResource(CONFIG_LOCATION));
//设置扫描mapper.xml
Resource[] resources = resolver.getResources(MAPPER_XML_PACKAGE);
sqlSessionFactoryBean.setMapperLocations(resources);
//设置扫描实体类
sqlSessionFactoryBean.setTypeAliasesPackage(MYBATIS_BEAN_PACKAGE);
return sqlSessionFactoryBean.getObject();
}
@Bean(name = "b2bSqlSessionTemplate")
@Primary
public SqlSessionTemplate sqlSessionTemplate() throws Exception {
return new SqlSessionTemplate(sqlSessionFactory());
}
@Bean(name = "b2bTransactionManager")
@Primary
public DataSourceTransactionManager transactionManager() {
return new DataSourceTransactionManager(b2bDataSource);
}
}
- 第二个数据源
package com.base.configs;
import javax.sql.DataSource;
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.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.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
@Configuration
@MapperScan(basePackages = {LawyerDataSourceConfig.LAWYER_MAPPER_PACKAGE}, sqlSessionFactoryRef = "lawyerSqlSessionFactory")
public class LawyerDataSourceConfig {
public static final String LAWYER_MAPPER_PACKAGE = "com.base.lawyer.dao";
public static final String LAWYER_MAPPER_XML_PACKAGE = "classpath:mybatis/mapper/lawyer/**/*.xml";
public static final String LAWYER_MYBATIS_BEAN_PACKAGE = "com.base.entities.lawyer";
public static final String LAWYER_CONFIG_LOCATION = "classpath:mybatis/mybatis.cfg.xml";
@Autowired
@Qualifier("lawyerDataSource")
private DataSource lawyerDataSource;
@Bean(name = "lawyerSqlSessionFactory")
public SqlSessionFactory lawyerSqlSessionFactory() throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(lawyerDataSource);
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
//设置扫描 mybatis-config.xml
sqlSessionFactoryBean.setConfigLocation(resolver.getResource(LAWYER_CONFIG_LOCATION));
//设置扫描mapper.xml
Resource[] resources = resolver.getResources(LAWYER_MAPPER_XML_PACKAGE);
sqlSessionFactoryBean.setMapperLocations(resources);
//设置扫描实体类
sqlSessionFactoryBean.setTypeAliasesPackage(LAWYER_MYBATIS_BEAN_PACKAGE);
return sqlSessionFactoryBean.getObject();
}
@Bean(name = "lawyerSqlSessionTemplate")
public SqlSessionTemplate lawyerSqlSessionTemplate() throws Exception {
return new SqlSessionTemplate(lawyerSqlSessionFactory());
}
@Bean(name = "lawyerTransactionManager")
public DataSourceTransactionManager lawyerTransactionManager() {
return new DataSourceTransactionManager(lawyerDataSource);
}
}
注意:本文归作者所有,未经作者允许,不得转载