目录
- 1. pom.xml添加依赖
- 2. application.properties添加数据库配置
- 3. 添加实体类
- 4. 添加Dao
- 5. 添加Controller
- 6. 新建数据库
- 7. 测试
上篇 只需两步!Eclipse+Maven快速构建第一个Spring Boot项目 已经构建了一个Spring Boot项目,本文在此基础上进行连接MySQL数据库的操作。
1. pom.xml添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
2. application.properties添加数据库配置
spring.datasource.url=jdbc:mysql://localhost:3306/spring_boot?serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql= true
如果数据库连接写成spring.datasource.url=
jdbc:mysql://localhost:3306/spring_boot ,由于MySQL版本的问题,可能会有以下的错误,在后面加上“?serverTimezone=GMT%2B8”,设置下时区,解决。
设置驱动,
spring.datasource.driver-class-name=com.mysql.jdbc.Driver会有下面红色的警告信息。说的是com.mysql.jdbc.Driver'被弃用了,要使用新的驱动
com