dataSource配置oracle,SpringBoot数据源配置DataSourceConfig

常见的数据源配置依赖druid。

步骤一:添加依赖。具体版本请自行查询。

com.alibaba

druid-spring-boot-starter

1.1.9

步骤二:编写配置类DataSourceConfig,并加注释。

@Slf4j

@Configuration

@MapperScan(basePackages = "com.xxx.xxx.xxx.dao",sqlSessionTemplateRef="dddddSqlSessionTemplate")

public class DataSourceConfig{

//数据源

@Primary

@Bean(name = "dddddDataSource")

@ConfigurationProperties(prefix = "spring.datasource.ddddd")

public DataSource dataSouce(){

log.info("开始配置数据源信息--------");

return DruidDataSourceBuilder.create().bulid();

}

@Primary

@Bean(name = "dddddSqlSessionFactory")

public SqlSessionFactory sqlSessionFactory(@Qualifier("dddddDataSource")DataSource dataSource){

SqlSessionFactoryBaen bean = new SqlSessionFactoryBean();

bean.setDataSource(dataSource);

ResourcePatternResolver reslover = new PathMatchingResoucePatternResolver();

try{

bean.setMapperLocations(resolver.getResource("classpath:mapper/*Mapper.xml"));

return bean.getObject();

}catch(Exception e){

e.printStackTrace();

throw new RuntimeException();

}

}

@Bean(name = "dddddSqlSessionTemplate")

public SqlSessionTemplate sqlSessionTemplate(@Qualifier("SqlSessionFactory")SqlSessionFactory sqlSessionFactory){

SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory);

return template;

}

}

步骤三:application.properties属性文件中添加数据源的配置。

#url根据数据库类型去填写,mysql格式:jdbc:mysql://xxxxx:3306/dbname?useSSL=false,oracle格式:jdbc:oracle:thin:@//ip:port/dbname

spring.datasource.ddddd.url =

spring.datasource.ddddd.username =

spring.datasource.ddddd.password =

#mysql:com.mysl.jdbc.Driver;oracle:oracle.jdbc.OracleDriver

spring.datasource.ddddd.driver-class-name =

spring.datasource.type = com.alibaba.druid.pool.DruidDataSource

###############durid

#初始化时建立的物理连接个数

spring.datasource.druid.initial-size=5

#最大连接池数量

spring.datasource.druid.max-active=30

#最小连接池数量

spring.datasource.druid.min-idle=5

#获取连接时最大等待时间,单位是毫秒

spring.datasource.druid.max-wait=60000

#配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒

spring.datasource.druid.time-between-eviction-runs-millis=60000

#连接保持空闲而不被驱逐的最小时间

spring.datasource.druid.min-evictable-idle-time-millis=300000

#用来检测连接是否有效的sql,要求是一个查询语句

spring.datasource.druid.validation-query=SELECT 1 FROM DUAL

属性文件中一般要求不会使用密码明文。因此使用密码转换方式转换一下,命令:java -cp druid-1.0.14.jar com.alibaba.druid.filter.config.ConfigTools 密码   ,回车即可生成密文。使用的jar包是druid-1.0.14.jar 包。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值