有时候需要在不同数据源中获取数据,所以需要进行多数据源的配置,话不多说,直接上代码:
项目结构:
这里使用的是Oracle数据库,用的是IDEA,Application.properties配置文件数据库信息:
首先关闭Springboot的默认数据源读取
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
然后对数据源进行注入,使Spring能够读取数据源信息
package cn.cdhy.pdf2image.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.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
/**
* @Description: 数据源一配置
*/
@Configuration
@MapperScan(basePackages = "cn.cdhy.pdf2image.mapper.cst", sqlSessionTemplateRef = "cstSqlSessionTemplate")
public class DataSourceCstConfig {
@Bean(name = "cstDataSource")
@ConfigurationProperties(prefix = "spring.datasource.cst")
@Primary
public DataSource cstDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "cstSqlSessionFactory")
@Primary
public SqlSessionFactory cstSqlSessionFactory(@Qualifier("cstDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/cst/*.xml"));
return bean.getObject();
}
@Bean(name = "cstTransactionManager")
@Primary
public DataSourceTransactionManager cstTransactionManager(@Qualifier("cstDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "cstSqlSessionTemplate")
@Primary
public SqlSessionTemplate cstSqlSessionTemplate(@Qualifier("cstSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
package cn.cdhy.pdf2image.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.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
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;
/**
* @Description: 数据源二配置
*/
@Configuration
@MapperScan(basePackages = "cn.cdhy.pdf2image.mapper.dzda", sqlSessionTemplateRef = "dzdaSqlSessionTemplate")
public class DataSourceDzdaConfig {
@Bean(name = "dzdaDataSource")
@ConfigurationProperties(prefix = "spring.datasource.dzda")
public DataSource dzdaDataSource() {
return DataSourceBuilder.create( ).build( );
}
@Bean(name = "dzdaSqlSessionFactory")
public SqlSessionFactory dzdaSqlSessionFactory(@Qualifier("dzdaDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean( );
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver( ).getResources("classpath:mybatis/mapper/dzda/*.xml"));
return bean.getObject( );
}
@Bean(name = "dzdaTransactionManager")
public DataSourceTransactionManager dzdaTransactionManager(@Qualifier("dzdaDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "dzdaSqlSessionTemplate")
public SqlSessionTemplate dzdaSqlSessionTemplate(@Qualifier("dzdaSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}