server.port=80
# Hibernate 相关配置
## 方言
#hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
## 显示Sql
hibernate.show_sql=true
## 自动建表方式
#hibernate.hbm2ddl.auto= update
## 自动扫描的包前缀
entitymanager.packagesToScan= com.zslin
## 数据库连接
spring.datasource.url=jdbc:mysql://localhost:3306/study05?\
useUnicode=true&characterEncoding=utf-8&useSSL=true&autoReconnect=true
## 用户名
spring.datasource.username=root
## 密码
spring.datasource.password=123
## 数据库驱动
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
## 建表方式
spring.jpa.properties.hibernate.hbm2ddl.auto=update
# 方言
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
注意:最关键的是最后两个配置,spring.jpa.properties.hibernate.hbm2ddl.auto=update
而不是hibernate.hbm2ddl.auto=update
,使用了Jpa所以键名称需要有相应调整,否则不会自动建表
spring.jpa.properties.hibernate.hbm2ddl.auto
有几种配置:
-
create
:每次加载Hibernate时都会删除上一次生成的表,然后重新生成新表,即使两次没有任何修改也会这样执行,这就导致每次启动都是一个新的数据库,也是导致数据丢失的重要原因。 -
create-drop
:每次加载Hibernate时都会生成表,但当SessionFactory关闭时,所生成的表将自动删除。 -
update
:最常用的属性值,第一次加载Hibernate时创建数据表(前提是需要先有数据库),以后加载HIbernate时只会根据model更新,即使model已经删除了某些属性,数据表也不会随之删除字段。 -
validate
:每次加载Hibernate时都会验证数据表结构,只会和已经存在的数据表进行比较,根据model修改表结构,但不会创建新表。