maven依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.4</version>
<relativePath/>
</parent>
我这里用的数据库是Oracle
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>21.5.0.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.17</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
application.yml
server:
port: 8088
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
druid:
db1:
driver-class-name: oracle.jdbc.driver.OracleDriver
url: jdbc:oracle:thin:@xx.xx.xxx.xx:1521/orcl
username: portal
# 生产环境提供的密码是加密后的这里假设是加密后的密码
password: port,al
db2:
driver-class-name: oracle.jdbc.driver.OracleDriver
url: jdbc:oracle:thin:@xx.xx.xxx.xx:1521/orcl
username: bass
# 生产环境提供的密码是加密后的这里假设是加密后的密码
password: bas,s
mybatis:
mapper-locations: [classpath:mapper/db1/*.xml,classpath:mapper/db2/*.xml]
configuration:
map-underscore-to-camel-case: true
配置类
DataSourceConfigCommon
import com.alibaba.druid.pool.DruidDataSource;
public class DataSourceConfigCommon extends DruidDataSource {
@Override
public String getPassword() {
/*这里假设对密码解密*/
password = super.getPassword().replace(",", "");
return password;
}
}
//配置数据源1
Db1DataSourceConfig
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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
@Configuration
@MapperScan(basePackages = "com.study.boot.boot2.mapper.db1", sqlSessionFactoryRef = "db1SqlSessionFactory")
public class Db1DataSourceConfig {
@Bean("db1DateSource")
@ConfigurationProperties(prefix = "spring.datasource.druid.db1")
public DataSourceConfigCommon db1DateSource() {
return new DataSourceConfigCommon();
}
@Bean("db1SqlSessionFactory")
public SqlSessionFactory db1SqlSessionFactory(@Qualifier("db1DateSource") DataSourceConfigCommon db1DateSource) throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(db1DateSource);
sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/db1/*.xml"));
return sqlSessionFactoryBean.getObject();
}
@Bean("db1SqlSessionTemplate")
public SqlSessionTemplate db1SqlSessionTemplate(@Qualifier("db1SqlSessionFactory") SqlSessionFactory db1SqlSessionFactory) {
return new SqlSessionTemplate(db1SqlSessionFactory);
}
}
//配置数据源2
Db2DataSourceConfig
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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
@Configuration
@MapperScan(basePackages = "com.study.boot.boot2.mapper.db2",sqlSessionFactoryRef = "db2SqlSessionFactory")
public class Db2DataSourceConfig {
@Bean("db2DateSource")
@ConfigurationProperties(prefix = "spring.datasource.druid.db2")
public DataSourceConfigCommon db2DateSource() {
return new DataSourceConfigCommon();
}
@Bean("db2SqlSessionFactory")
public SqlSessionFactory db2SqlSessionFactory(@Qualifier("db2DateSource") DataSourceConfigCommon db2DateSource) throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(db2DateSource);
sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/db2/*.xml"));
return sqlSessionFactoryBean.getObject();
}
@Bean("db2SqlSessionTemplate")
public SqlSessionTemplate db2SqlSessionTemplate(@Qualifier("db2SqlSessionFactory") SqlSessionFactory db2SqlSessionFactory){
return new SqlSessionTemplate(db2SqlSessionFactory);
}
}