常见的数据源配置依赖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 包。