配置數(shù)據(jù)訪問
GitHub Druid官方文檔:
https://github.com/alibaba/druid/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98
導(dǎo)入jdbc場景依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
通過修改依賴從而修改數(shù)據(jù)庫連接池的數(shù)據(jù)源Druid(德魯伊)
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
修改數(shù)據(jù)源相關(guān)配置(Mysql 8以上):
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: root
url: jdbc:mysql://localhost:3306/mypratices
jdbc:
template:
# 設(shè)置三秒超時(shí)
query-timeout: 3
- 底層配置好的連接池是:HikariDataSource
測試SQL語句
@Slf4j
@SpringBootTest
class SpringbootWebAdminApplicationTests {
@Autowired
JdbcTemplate jdbcTemplate;
@Autowired
DataSource dataSource;
@Test
void contextLoads() {
Long aLong = jdbcTemplate.queryForObject("select count(*) from student",Long.class);
log.info("記錄數(shù){}",aLong);
// jdbcTemplate.queryForList("select * from student");
log.info("數(shù)據(jù)源類型{}",dataSource.getClass());
}
}
數(shù)據(jù)源配置類
@Configuration
public class MyDataSourceConfig {
//綁定配置文件中的配置屬性
@ConfigurationProperties("spring.datasource")
@Bean
public DataSource dataSource() throws SQLException {
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setFilters("stat");
return druidDataSource;
}
/**
* 開啟Druid的監(jiān)控頁功能
* 通過訪問http://localhost:8080/druid/ 打開Druid監(jiān)控頁
* @return
*/
@Bean
public ServletRegistrationBean statViewServlet(){
StatViewServlet viewServlet = new StatViewServlet();
ServletRegistrationBean<StatViewServlet> registrationBean = new ServletRegistrationBean<>(viewServlet,"/druid/*");
return registrationBean;
}
}
設(shè)置Druid頁面的登錄功能
@Bean
public ServletRegistrationBean statViewServlet(){
StatViewServlet viewServlet = new StatViewServlet();
ServletRegistrationBean<StatViewServlet> registrationBean = new ServletRegistrationBean<>(viewServlet,"/druid/*");
//設(shè)置Druid頁面登錄攔截
registrationBean.addInitParameter("loginUsername","admin");
registrationBean.addInitParameter("loginPassword","admin");
return registrationBean;
}
Druid綁定了spring.datasource 所以在.yml配置文件中可以直接進(jìn)行配置
druidDataSource.setFilters("stat,wall");//加入監(jiān)控功能,添加防火墻防止SQL注入
等同于
spring:
datasource:
filter: stat,wall
max-active: 10
經(jīng)典白學(xué)...(知其然屉符,更應(yīng)知其所以然)
以上所有的配置都可以通過添加依賴并在yml文件中添加配置完成
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.17</version>
</dependency>
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: root
url: jdbc:mysql://localhost:3306/mypratices
# filter: stat,wall
# tomcat:
# max-active: 10
druid:
# 監(jiān)控頁的配置
stat-view-servlet:
enabled: true
login-username: admin
login-password: admin
reset-enable: false
web-stat-filter:
enabled: true
url-pattern: /*
exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"
aop-patterns: com.laj.admin.*
filters: stat,wall
filter:
stat:
slow-sql-millis: 1000 #表示超過1000ms的查詢都是慢查詢 默認(rèn)是3*1000ms
log-slow-sql: true #記錄慢查詢的SQL
wall: #防火墻
enabled: true
config:
drop-table-allow: false #表示不可以刪表,所有刪除表的操作都被防火墻攔截
jdbc:
template:
# 設(shè)置三秒超時(shí)
query-timeout: 3