在学习springboot时,需要将项目中的entity映射到MySQL数据库中,因此需要进行一些配置
步骤一:在pom.xml添加如下的依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
步骤二:在application.propertIies中添加如下内容
#DataSource
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url= jdbc:mysql://127.0.0.1:3306/test
#?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
#JPA
spring.jpa.database=MYSQL
spring.jpa.show-sql= true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.DefaultNamingStrategy
spring.jpa.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jackson.serialization.indent_output=false
解释一下:
spring.datasource.driver-class-name为使用的驱动,
com.mysql.jdbc.Driver
是 mysql-connector-java 5中的,com.mysql.cj.jdbc.Driver
是 mysql-connector-java 6中的
spring.datasource.url为所连接数据库,上面指的就是开放端口3306的本地数据库test,此外还可以设置字符编码和时区,当JDBC连接Mysql6 com.mysql.cj.jdbc.Driver
, 需要指定时区serverTimezone
springdatasource.username是数据库用户名,此处为root
spring.datasource.password是密码,此处为123456
参考:https://blog.csdn.net/superdangbo/article/details/78732700