1. yml配置
注意:
- yml配置库中,springboot2.0以上不要使用url,而是使用jdbc-url
spring
datasource:
db1:
jdbc-url: jdbc:oracle:thin:@10.132.119.102:1521:qmsdb
username: mobile
password: mobile
driver-class-name: oracle.jdbc.OracleDriver
hikari:
idle-timeout: 30000
connection-timeout: 15000
connection-test-query: select 1 from dual
login-timeout: 5
max-lifetime: 900000
maximum-pool-size: 20
minimum-idle: 5
db2:
jdbc-url: jdbc:oracle:thin:@(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = 10.132.118.159)(PORT = 1521))(CONNECT_DATA = (SERVER = DEDICATED)(SERVICE_NAME = mesdbstb)))
username: Kj
password: kjmes
driver-class-name: oracle.jdbc.OracleDriver
hikari:
idle-timeout: 30000
connection-timeout: 15000
connection-test-query: select 1 from dual
login-timeout: 5
max-lifetime: 900000
maximum-pool-size: 20
minimum-idle: 5
2. 配置类
注意:
- 要把service层和dao层放到指定的数据源包下,当service只有实现类没接口时,可以配置一级目录
- 当service有接口时,必须配置指向dao层接口路径
package cn.rp.config;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
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 tk.mybatis.spring.annotation.MapperScan;
import javax.sql.DataSource;
import java.io.IOException;
import java.sql.SQLClientInfoException;
/**
* @version V1.0.0
* @author: WangQingLong
* @date: 2020/8/11 10:20
* @description:
*
**/
@Configuration
@MapperScan(basePackages = {"配置dao层接口路径"},sqlSessionFactoryRef = "sqlFact")
public class db1Config {
/**
* bean 里面的名字随便取
* primary代表默认数据源
* configuarationProperties配置的是yml里面配置的数据库前缀
* 注意,下面方式只在spring当中有效
* @return
*/
@Bean(name = "dbOne")
@Primary
@ConfigurationProperties(prefix = "spring.datasource.db1")
public DataSource db1(){
return DataSourceBuilder.create().build();
}
/**
* 这里要注意的是,classpath:db1/*.xml是指resource资源文件夹下的db1的包下的xml文件
* 也就是映射mapper.xml文件
*
* @param dataSource
* @return
* @throws Exception
*/
@Bean(name = "sqlFact")
@Primary
public SqlSessionFactory sqlSessionFactory(@Qualifier("dbOne")DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
//配置mybatis的驼峰映射
org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
configuration.setMapUnderscoreToCamelCase(true);
bean.setConfiguration(configuration);
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath: db1/*.xml"));
return bean.getObject();
}
/**配置事务管理器
* @param dataSource
* @return
*/
@Bean(name = "db1Tx")
@Primary
public DataSourceTransactionManager db1Tx(@Qualifier("dbOne")DataSource dataSource){
return new DataSourceTransactionManager(dataSource);
}
/**模板
* @param sqlSessionFactory
* @return
*/
@Bean(name = "db1Temp")
@Primary
public SqlSessionTemplate tb1Temp(@Qualifier("sqlFact")SqlSessionFactory sqlSessionFactory){
return new SqlSessionTemplate(sqlSessionFactory);
}
}