OracleDataSourceConfigOracleDataSourceConfig
前几天遇到遇到一个需求,需要从mysql库中查询的数据到pg库中,我选择在一个服务中集成两个不同的数据库,废话不多说,直接上教程,教程以Orcle和Postgressql为例
1.首先需要在pom文件中引入这两个库jar包
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.3</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.19</version>
</dependency>
2.编写两个配置类分别为OracleDataSourceConfig和PostgresqlDataSourceConfig
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;
/**
* @author :hanwenjian
* @date :Created in 2023/7/24 16:44
* 文件说明: </p>
*/
@Configuration
@MapperScan(basePackages = "com.inspur.softwaregroup.communication.nrms.dao.postgresql",sqlSessionTemplateRef ="postgreSqlSessionTemplate")
public class PostgresqlDataSourceConfig {
private String url = "jdbc:postgresql://127.0.0.1:5432/postgres";
private String username = "root";
private String password = "123456";
private String driverClassName = "org.postgresql.Driver";
private String mybatisLocations = "classpath*:mapper/postgresql/*Mapper.xml";
// 创建数据源
@Bean(name = "postgresqlDS")
@ConfigurationProperties(prefix = "spring.datasource.postgresql")
@Primary
public DataSource getFirstDataSource() {
DataSource build = DataSourceBuilder.create()
.driverClassName(driverClassName)
.url(url)
.username(username)
.password(password)
.build();
return build;
}
// 创建SessionFactory
@Bean(name = "postgreSqlSessionFactory")
@Primary
public SqlSessionFactory firstSqlSessionFactory(@Qualifier("postgresqlDS") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources(mybatisLocations));
bean.setDataSource(dataSource);
return bean.getObject();
}
// 创建事务管理器
@Bean("postgreTransactionManger")
@Primary
public DataSourceTransactionManager firstTransactionManger(@Qualifier("postgresqlDS") DataSource dataSource){
return new DataSourceTransactionManager(dataSource);
}
// 创建SqlSessionTemplate
@Bean(name = "postgreSqlSessionTemplate")
@Primary
public SqlSessionTemplate firstSqlSessionTemplate(@Qualifier("postgreSqlSessionFactory") SqlSessionFactory sqlSessionFactory){
return new SqlSessionTemplate(sqlSessionFactory);
}
private Class getType(String type) {
try {
return Class.forName(type);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}
}
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.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;
/**
* @author :hanwenjian
* @date :Created in 2023/7/24 16:44
* 文件说明: </p>
*/
@Configuration
@MapperScan(basePackages = "com.inspur.softwaregroup.communication.nrms.dao.oracle",
sqlSessionTemplateRef ="oracleSqlSessionTemplate")
public class OracleDataSourceConfig {
private String url = "jdbc:oracle:thin:@127.0.0.1:1521/pdblightbox2";
private String username = "root";
private String password = "123456";
private String driverClassName = "oracle.jdbc.driver.OracleDriver";
private String mybatisLocations = "classpath*:mapper/orcle/*Mapper.xml";
// 创建数据源
@Bean(name = "oracleDS")
@ConfigurationProperties(prefix = "spring.datasource.oracle")
@Primary
public DataSource getFirstDataSource() {
DataSource build = DataSourceBuilder.create()
.driverClassName(driverClassName)
.url(url)
.username(username)
.password(password)
.build();
return build;
}
// 创建SessionFactory
@Bean(name = "oracleSqlSessionFactory")
@Primary
public SqlSessionFactory firstSqlSessionFactory(@Qualifier("oracleDS") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources(mybatisLocations));
bean.setDataSource(dataSource);
return bean.getObject();
}
// 创建事务管理器
@Bean("oracleTransactionManger")
@Primary
public DataSourceTransactionManager firstTransactionManger(@Qualifier("oracleDS") DataSource dataSource){
return new DataSourceTransactionManager(dataSource);
}
// 创建SqlSessionTemplate
@Bean(name = "oracleSqlSessionTemplate")
@Primary
public SqlSessionTemplate firstSqlSessionTemplate(@Qualifier("oracleSqlSessionFactory") SqlSessionFactory sqlSessionFactory){
return new SqlSessionTemplate(sqlSessionFactory);
}
private Class getType(String type) {
try {
return Class.forName(type);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}
}
3.springboot启动类中的@SpringBootApplication这个注解中需要添加参数
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})