配置数据库信息
- 因为要用到Druid读取配置,所以将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
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
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
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
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扫描来指定那些用哪个数据源
@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();
}
/*
* 创建数据源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;
}
/**
* 创建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();
}
/*
* 创建事务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也是一样写,就不重复上代码了。
最后需要注意在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>
druid.jar下面这是我的jar目录
如果报如下错就是上面的jar和pom没有配好,会在druidDatasource.init();的时候报错。
java.sql.SQLException: unknown jdbc driver : jdbc:sap://xx.xx.xx
网友评论