一篇搞定SpringBoot整合JDBC以及配置Druid数据源难道不香么?

冷不防 2022-12-17 10:54 86阅读 0赞

整合JDBC


只需要两个依赖,在pom.xml文件中导入:

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-jdbc</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>mysql</groupId>
  7. <artifactId>mysql-connector-java</artifactId>
  8. <scope>runtime</scope>
  9. </dependency>

application.yml文件配置数据库连接:

  1. spring:
  2. datasource:
  3. username: jektong
  4. password: 123456
  5. url: jdbc:mysql://localhost:3306/jektong?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
  6. driver-class-name: com.mysql.jdbc.Driver

找个测试类测试一下:

  1. @SpringBootTest
  2. class StudyVideoApplicationTests {
  3. @Autowired
  4. private DataSource dataSource;
  5. @Test
  6. void contextLoads() throws SQLException {
  7. Connection connection = dataSource.getConnection();
  8. System.out.println(connection);
  9. }
  10. }

输出类似这样的:说明配置没有问题

  1. HikariProxyConnection@1957530885 wrapping com.mysql.cj.jdbc.ConnectionImpl@676ff3b0

整合Druid

使用阿里的Druid主要就是因为Druid的监控系统有点强大
主要依赖

  1. <!-- 配置阿里巴巴连接池 -->
  2. <dependency>
  3. <groupId>com.alibaba</groupId>
  4. <artifactId>druid</artifactId>
  5. <version>1.1.21</version>
  6. </dependency>

Druid数据源的配置:这些配置拿来即用

  1. spring:
  2. datasource:
  3. driver-class-name: com.mysql.jdbc.Driver
  4. url: jdbc:mysql://localhost:3306/jektong?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
  5. username: jektong
  6. password: 123456
  7. # 指定数据源
  8. type: com.alibaba.druid.pool.DruidDataSource
  9. # 初始化连接个数
  10. initialSize: 5
  11. # 最小连接池
  12. minIdle: 5
  13. # 最大连接池
  14. maxActive: 20
  15. # 连接等待超时的等待时间
  16. maxWait: 60000
  17. # 多久检测一次
  18. timeBetweenEvictionRunsMillis: 60000
  19. minEvictableIdleTimeMillis: 300000
  20. validationQuery: SELECT 1 FROM DUAL
  21. testWhileIdle: true
  22. testOnBorrow: false
  23. testOnReturn: false
  24. filters: stat,wall,log4j
  25. logSlowSql: true
  26. poolPreparedStatements: true
  27. maxPoolPreparedStatementPerConnectionSize: 20
  28. connectionProperties: druid.statmergeSql=true;druid.stat.slowSqlMillis=5000

建立配置类DruidCofig.java配置文件

  1. package com.jektong.config;
  2. import com.alibaba.druid.pool.DruidDataSource;
  3. import com.alibaba.druid.support.http.StatViewServlet;
  4. import com.alibaba.druid.support.http.WebStatFilter;
  5. import org.springframework.boot.context.properties.ConfigurationProperties;
  6. import org.springframework.boot.web.servlet.FilterRegistrationBean;
  7. import org.springframework.boot.web.servlet.ServletRegistrationBean;
  8. import org.springframework.context.annotation.Bean;
  9. import org.springframework.context.annotation.Configuration;
  10. import javax.sql.DataSource;
  11. /** * @author jektong * @Date 2020/10/5 16:02 */
  12. @Configuration
  13. public class DruidConfig {
  14. @ConfigurationProperties(prefix = "spring.datasource")
  15. @Bean
  16. public DataSource druidDataSource() {
  17. return new DruidDataSource();
  18. }
  19. // 后台监控
  20. @Bean
  21. public ServletRegistrationBean statViewServlet() {
  22. // 现在要进行druid监控的配置处理操作
  23. ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(
  24. new StatViewServlet(), "/druid/*");
  25. // 白名单,多个用逗号分割, 如果allow没有配置或者为空,则允许所有访问
  26. servletRegistrationBean.addInitParameter("allow", "127.0.0.1,172.29.32.54");
  27. // 黑名单,多个用逗号分割 (共同存在时,deny优先于allow)
  28. servletRegistrationBean.addInitParameter("deny", "192.168.1.110");
  29. // 控制台管理用户名
  30. servletRegistrationBean.addInitParameter("loginUsername", "admin");
  31. // 控制台管理密码
  32. servletRegistrationBean.addInitParameter("loginPassword", "eju1314");
  33. // 是否可以重置数据源,禁用HTML页面上的“Reset All”功能
  34. servletRegistrationBean.addInitParameter("resetEnable", "false");
  35. return servletRegistrationBean;
  36. }
  37. @Bean
  38. public FilterRegistrationBean filterRegistrationBean() {
  39. FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
  40. filterRegistrationBean.setFilter(new WebStatFilter());
  41. //所有请求进行监控处理
  42. filterRegistrationBean.addUrlPatterns("/*");
  43. //添加不需要忽略的格式信息
  44. filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.css,/druid/*");
  45. return filterRegistrationBean;
  46. }
  47. }

输入网址:
http://localhost:8080/druid/login.html
就会进入Druid后台监控登录的页面,输入密码就登录成功了
在这里插入图片描述
只要触发了数据库语句就会有相应的记录了!!!
在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,86人围观)

还没有评论,来说两句吧...

相关阅读