使用DriverManagerDataSource
修改yml
#端口
server:
port: 8080
spring:
#数据源
datasource:
url: jdbc:mysql://127.0.0.1:3306/dd?useUnicode=true&characterEncoding=utf8&useSSL=true
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: root
#配置数据源的类型
type: org.springframework.jdbc.datasource.DriverManagerDataSource
使用DBCP
修改pom.xml引入dbcp的包
<!--加入Dbcp的依赖-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
</dependency>
#端口
server:
port: 8080
spring:
#数据源
datasource:
url: jdbc:mysql://127.0.0.1:3306/dym11?useUnicode=true&characterEncoding=utf8&useSSL=true
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: root
#配置数据源的类型
type: org.apache.commons.dbcp2.BasicDataSource
type: org.springframework.jdbc.datasource.DriverManagerDataSource
使用druid【自己写自动配置类】
修改pom.xml加入druid的依赖
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.21</version>
</dependency>
创建配置类MyDruidAutoConfiguration
package com.sxt.config;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConditionalOnClass(value = {DruidDataSource.class})
@EnableConfigurationProperties(MyDruidProperties.class) //把MyDruidProperties类注入到ioc容器中
public class MyDruidAutoConfiguration {
private Log log= LogFactory.getLog(MyDruidAutoConfiguration.class);
@Autowired
private MyDruidProperties properties;
/**
* 创建DataSource
*/
@Bean(initMethod = "init")
public DruidDataSource druidDataSource(){
DruidDataSource dataSource=new DruidDataSource();
if(null==properties.getUrl()){
log.error("url can not be null");
throw new RuntimeException("url can not be null");
}
dataSource.setDriverClassName(properties.getDriverClassName());
dataSource.setUrl(properties.getUrl());
dataSource.setUsername(properties.getUsername());
dataSource.setPassword(properties.getPassword());
dataSource.setMaxActive(properties.getMaxActive());
dataSource.setMinIdle(properties.getMinIdle());
dataSource.setMaxIdle(properties.getMaxIdle());
dataSource.setInitialSize(properties.getInitialSize());
dataSource.setValidationQuery(properties.getValidationQuery());
return dataSource;
}
/**
* 注册监听页面的Servlet
*/
@Bean
@ConditionalOnClass(value = {StatViewServlet.class})
public ServletRegistrationBean<StatViewServlet> registrationBeanStatViewServlet(){
//创建注册器
ServletRegistrationBean<StatViewServlet> bean=new ServletRegistrationBean<>();
//创建Servlet
StatViewServlet servlet=new StatViewServlet();
//注册
bean.setServlet(servlet);
//注入相关参数
bean.addInitParameter("loginUsername",properties.getStatView().getLoginUsername());
bean.addInitParameter("loginPassword",properties.getStatView().getLoginPassword());
bean.addInitParameter("allow",properties.getStatView().getAllow());
bean.addInitParameter("deny",properties.getStatView().getDeny());
if(properties.getStatView().getUrlMapping()==null||properties.getStatView().getUrlMapping().length==0){
log.error("监控的urlMapping不能为空");
throw new RuntimeException("监控的urlMapping不能为空");
}
//设置映射
bean.addUrlMappings(properties.getStatView().getUrlMapping());
return bean;
}
}
创建属性类MyDruidProperties
package com.sxt.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
@Data
@ConfigurationProperties(prefix = "spring.druid")
public class MyDruidProperties {
private String driverClassName;
private String url;
private String username;
private String password;
private Integer initialSize;
private Integer maxActive;
private Integer minIdle;
private Integer maxIdle;
private String validationQuery; //连接检查语句
private StatView statView;
@Data
static class StatView{
//监控的属性
private String loginUsername;
private String loginPassword;
private String allow;
private String deny;
private String [] urlMapping;
}
}
修改yml
spring:
#数据源
druid:
url: jdbc:mysql://127.0.0.1:3306/dd?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=UTC
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: root
max-active: 20
initial-size: 5
min-idle: 5
max-idle: 10
validation-query: select 1
stat-view:
login-username: admin
login-password: admin
allow:
deny:
url-mapping:
- "/druid/*"
排除DataSoruceAutoConfiguration
package com.sxt;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
//启动时排除DataSourceAutoConfiguration自动配置类的加载
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}