SpringBoot2.7.4整合MySql

一、MySql整合

1.打开pom.xml添加依赖

<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.22</version> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency>

SpringBoot2.7.4整合MySql2.将application.properties更改成application.yml

SpringBoot2.7.4整合MySql

SpringBoot2.7.4整合MySql

3.在application.yml文件,添加启动的端口号(8888)和数据库配置

server: port: 8888 #指定启动的端口 spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver #数据库驱动 url: jdbc:mysql://localhost:3306/springboot_study?serverTimezone=Asia/Shanghai&characterEncoding=utf-8&useSSL=false #数据库的url username: root #数据库的用户名 password: 123456 #数据库的密码

SpringBoot2.7.4整合MySql

4.打开类SpringBootStudyApplication,右键运行

SpringBoot2.7.4整合MySql

Druid连接池整合

1.打开pom.xml,引入依赖

<dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.10</version> </dependency>

SpringBoot2.7.4整合MySql

2.在application.yml文件,添加druid配置

server: port: 8888 #指定启动的端口 spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver #数据库驱动 url: jdbc:mysql://localhost:3306/springboot_study?serverTimezone=Asia/Shanghai&characterEncoding=utf-8&useSSL=false #数据库的url username: root #数据库的用户名 password: 123456 #数据库的密码 type: com.alibaba.druid.pool.DruidDataSource #指定数据源 druid: initial-size: 5 # 初始化大小 min-idle: 10 # 最小连接数 max-active: 20 # 最大连接数 max-wait: 60000 # 获取连接时的最大等待时间 min-evictable-idle-time-millis: 300000 # 一个连接在池中最小生存的时间,单位是毫秒 time-between-eviction-runs-millis: 60000 # 多久才进行一次检测需要关闭的空闲连接,单位是毫秒 filters: stat,wall # 配置扩展插件:stat-监控统计,log4j-日志,wall-防火墙(防止SQL注入),去掉后,监控界面的sql无法统计 validation-query: SELECT 1 # 检测连接是否有效的 SQL语句,为空时以下三个配置均无效 test-on-borrow: true # 申请连接时执行validationQuery检测连接是否有效,默认true,开启后会降低性能 test-on-return: true # 归还连接时执行validationQuery检测连接是否有效,默认false,开启后会降低性能 test-while-idle: true # 申请连接时如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效,默认false,建议开启,不影响性能 stat-view-servlet: enabled: true # 是否开启 StatViewServlet allow: 127.0.0.1 # 访问监控页面的白名单,默认127.0.0.1(多个以逗号隔开) deny: 192.168.0.1 # 访问监控页面的黑名单(存在共同时,deny优先于allow) login-username: admin # 访问监控页面的登陆账号 login-password: 123456 # 访问监控页面的登陆密码 filter: stat: enabled: true # 是否开启 FilterStat,默认true log-slow-sql: true # 是否开启 慢SQL 记录,默认false slow-sql-millis: 5000 # 慢 SQL 的标准,默认 3000,单位:毫秒 merge-sql: false # 合并多个连接池的监控数据,默认false

SpringBoot2.7.4整合MySql

3.打开类SpringBootStudyApplication,右键运行.

SpringBoot2.7.4整合MySql

4.打开浏览器.访问 http://localhost:8888/druid/login.html

SpringBoot2.7.4整合MySql

5.输入步骤2设置的账号admin和密码123456

SpringBoot2.7.4整合MySql

以上内容由“WiFi之家网”整理收藏!。

原创文章,作者:192.168.1.1,如若转载,请注明出处:https://www.224m.com/232136.html

(0)
192.168.1.1192.168.1.1
上一篇 2022年11月9日
下一篇 2022年11月9日

相关推荐