一、导入 Maven 依赖
<!--MySQL jdbc 驱动--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.17</version></dependency>
二、在 application.yml 中配置数据源
spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/csvswitch?serverTimezone=GMT%2B8&characterEncoding=utf8&useSSL=trueusername: rootpassword: password # 你的数据库密码
分析一下这个 url 的结构:
url : jdbc:mysql://localhost:3306/csvswitch?serverTimezone=GMT%2B8&characterEncoding=utf8&useSSL=true
- 多个参数之间用
&符号连接 3306是MySQL的端口号csvswitch是自己创建的数据库名称serverTimezone=GMT%2B8设置时区为东八区,即北京时间,不设置会报错。characterEncoding=utf8覆盖客户端自动检测到的编码,使用指定的utf8useSSL=true指定是否使用ssl连接。
