配置數(shù)據(jù)庫信息
- 因?yàn)橐玫紻ruid讀取配置,所以將druid提到了first后面
spring:
datasource:
first:
druid:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.sap.db.jdbc.Driver
url: jdbc:sap://
username:
password:
initialSize: 5
minIdle: 5
maxActive: 20
# 配置獲取連接等待超時的時間
maxWait: 60000
# 配置間隔多久才進(jìn)行一次檢測,檢測需要關(guān)閉的空閑連接,單位是毫秒
timeBetweenEvictionRunsMillis: 60000
# 配置一個連接在池中最小生存的時間,單位是毫秒
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUMMY
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
# 打開PSCache,并且指定每個連接上PSCache的大小
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
# 通過connectProperties屬性來打開mergeSql功能;慢SQL記錄
connectionProperties: druid.stat.mergeSql\=true;druid.stat.slowSqlMillis\=5000
second:
druid:
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:oracle:
username:
password:
initialSize: 5
minIdle: 5
maxActive: 20
# 配置獲取連接等待超時的時間
maxWait: 60000
# 配置間隔多久才進(jìn)行一次檢測,檢測需要關(guān)閉的空閑連接,單位是毫秒
timeBetweenEvictionRunsMillis: 60000
# 配置一個連接在池中最小生存的時間,單位是毫秒
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
# 打開PSCache,并且指定每個連接上PSCache的大小
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
# 通過connectProperties屬性來打開mergeSql功能;慢SQL記錄
connectionProperties: druid.stat.mergeSql\=true;druid.stat.slowSqlMillis\=5000
寫兩個Dataconfig文件,通過MapperScan掃描來指定那些用哪個數(shù)據(jù)源
@Configuration
@MapperScan(basePackages = {"com.xxxx.xx.xx.xxxx"},sqlSessionFactoryRef = "SqlSessionFactory1")
public class DataSource1Config {
@Bean(name = "Properties1")
@ConfigurationProperties(prefix = "spring.datasource.first")
public Properties Properties1(){
return new Properties();
}
/*
* 創(chuàng)建數(shù)據(jù)源datasource
* */
@Bean(name = "DataSource1")
@Primary
public DruidDataSource masterDataSource(@Qualifier("Properties1")Properties properties) {
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.configFromPropety(properties);
try {
druidDataSource.init();
} catch (SQLException e) {
e.printStackTrace();
}
return druidDataSource;
}
/**
* 創(chuàng)建sqlsessionfactory
*/
@Bean(name = {"SqlSessionFactory1"})
@Primary
public SqlSessionFactory Hana1SqlSessionFactory(@Qualifier("DataSource1") DruidDataSource druidDataSource) throws Exception {
final SqlSessionFactoryBean sessionFactoryBean=new SqlSessionFactoryBean();
sessionFactoryBean.setDataSource(druidDataSource);
//分頁插件
Interceptor interceptor = new PageInterceptor();
Properties properties = new Properties();
properties.setProperty("helperDialect", "hsqldb");
properties.setProperty("offsetAsPageNum", "true");
properties.setProperty("rowBoundsWithCount", "true");
properties.setProperty("reasonable", "true");
properties.setProperty("supportMethodsArguments","true");
properties.setProperty("params","pageNum=pageNum;pageSize=pageSize;");
interceptor.setProperties(properties);
sessionFactoryBean.setPlugins(new Interceptor[] {interceptor});
return sessionFactoryBean.getObject();
}
/*
* 創(chuàng)建事務(wù)DataSourceTransactionManager
* */
@Bean(name = {"TransactionManager1"})
@Primary
public DataSourceTransactionManager Hana1TransactionManager(){
return new DataSourceTransactionManager(masterDataSource(Properties1()));
}
@Bean(name = {"SqlSessionTemplate1"})
@Primary
public SqlSessionTemplate Hana1SqlSessionTemplate(@Qualifier("SqlSessionFactory1") SqlSessionFactory sqlSessionFactory){
return new SqlSessionTemplate(sqlSessionFactory);
}
}
第二個dataconfig也是一樣寫,就不重復(fù)上代碼了。
最后需要注意在module的pom文件下加上依賴并將druid.jar放到resource下
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.19</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/druid-1.1.19.jar</systemPath>
</dependency>
下面這是我的jar目錄

druid.jar
如果報(bào)如下錯就是上面的jar和pom沒有配好,會在druidDatasource.init();的時候報(bào)錯。
java.sql.SQLException: unknown jdbc driver : jdbc:sap://xx.xx.xx