配置数据访问
GitHub Druid官方文档:
https://github.com/alibaba/druid/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98
导入jdbc场景依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
通过修改依赖从而修改数据库连接池的数据源Druid(德鲁伊)
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
修改数据源相关配置(Mysql 8以上):
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: root
url: jdbc:mysql://localhost:3306/mypratices
jdbc:
template:
# 设置三秒超时
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("记录数{}",aLong);
// jdbcTemplate.queryForList("select * from student");
log.info("数据源类型{}",dataSource.getClass());
}
}
数据源配置类
@Configuration
public class MyDataSourceConfig {
//绑定配置文件中的配置属性
@ConfigurationProperties("spring.datasource")
@Bean
public DataSource dataSource() throws SQLException {
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setFilters("stat");
return druidDataSource;
}
/**
* 开启Druid的监控页功能
* 通过访问http://localhost:8080/druid/ 打开Druid监控页
* @return
*/
@Bean
public ServletRegistrationBean statViewServlet(){
StatViewServlet viewServlet = new StatViewServlet();
ServletRegistrationBean<StatViewServlet> registrationBean = new ServletRegistrationBean<>(viewServlet,"/druid/*");
return registrationBean;
}
}
设置Druid页面的登录功能
@Bean
public ServletRegistrationBean statViewServlet(){
StatViewServlet viewServlet = new StatViewServlet();
ServletRegistrationBean<StatViewServlet> registrationBean = new ServletRegistrationBean<>(viewServlet,"/druid/*");
//设置Druid页面登录拦截
registrationBean.addInitParameter("loginUsername","admin");
registrationBean.addInitParameter("loginPassword","admin");
return registrationBean;
}
Druid绑定了spring.datasource 所以在.yml配置文件中可以直接进行配置
druidDataSource.setFilters("stat,wall");//加入监控功能,添加防火墙防止SQL注入
等同于
spring:
datasource:
filter: stat,wall
max-active: 10
经典白学...(知其然,更应知其所以然)
以上所有的配置都可以通过添加依赖并在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:
# 监控页的配置
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的查询都是慢查询 默认是3*1000ms
log-slow-sql: true #记录慢查询的SQL
wall: #防火墙
enabled: true
config:
drop-table-allow: false #表示不可以删表,所有删除表的操作都被防火墙拦截
jdbc:
template:
# 设置三秒超时
query-timeout: 3