1、添加對應依賴
1 <!--jpa依賴--> 2 <dependency> 3 <groupId>org.springframework.boot</groupId> 4 <artifactId>spring-boot-starter-data-jpa</artifactId> 5 </dependency> 6 <!--mysql數據庫驅動程序--> 7 <dependency> 8 <groupId>mysql</groupId> 9 <artifactId>mysql-connector-java</artifactId> 10 </dependency> 11 </dependencies>
2、在application.properties中添加數據庫連接配置配置
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/demo spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.username=root spring.datasource.password=root
#每次運行程序,沒有表格會新建表格,表內有數據不會清空,只會更新
#每次運行程序,沒有表格會新建表格,表內數據不會清空,只會更新
spring.jpa.hibernate.ddl-auto=update
ddl-auto:create----每次運行該程序,沒有表格會新建表格,表內有數據會清空
ddl-auto:create-drop----每次程序結束的時候會清空表
ddl-auto:update----每次運行程序,沒有表格會新建表格,表內有數據不會清空,只會更新
ddl-auto:validate----運行程序會校驗數據與數據庫的字段類型是否相同,不同會報錯
.properties 中采用【.】來配置層級關系 如:spring.datasource.url,
而yml配置文件中 則使用換行縮進來配置層級關系,如:
spring:
datasource:
url:
上述配置完成后,啟動報錯:the server time zone value '�й���ʱ��' is unrecognized
在將spring.datasource.url改為如下配置后啟動成功
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/demo?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8
3、在啟動程序統計文件夾下新建java類
@Entity public class Family { @Id @GeneratedValue private Integer id; private String name; }
之后重新啟動程序,數據庫中將會出現一個family表。