pom.xml导入mysql连接驱动:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
通过yml文件配置mysql数据库连接:
spring:
datasource:
username: root
password: 123456
url: jdbc:mysql://ip:port/mydb01
driver-class-name: com.mysql.jdbc.Driver
启动Application时,碰到红字提示:
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
原因是最新的mysql驱动名称已经发生了变化,将上面的 driver-class-name 改成 com.mysql.cj.jdbc.Driver 即可消除
使用spring boot,在启动时执行sql文件
1.按照命名规则,自动发现执行,schema-*.sql,data-*.sql
2.yml中指定schema参数,sql文件放在yml的同级目录下
schema:
- classpath:department.sql
实际操作时发现配置无效,没有去执行sql文件,还需要指定另一个参数配置:
initialization-mode: always
完整配置如下: