Skip to main content

数据库

spring-data-jpa

1. Maven 添加两个组件

        <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. 添加配置

spring:
profiles:
active: dev
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test?&serverTimezone=UTC&characterEncoding=utf8&useSSL=false
username: root
password: root
jpa:
hibernate:
ddl-auto: create
show-sql: true

jpa.hibernate.ddl-auto 的参数

ddl-auto:create----每次运行该程序,没有表格会新建表格,有表会删除表再创建表。

ddl-auto:create-drop----每次程序结束的时候会清空表

ddl-auto:update----每次运行程序,没有表格会新建表格,表内有数据不会清空,只会更新

ddl-auto:validate----运行程序会校验数据与数据库的字段类型是否相同,不同会报错

创建表结构

package cn.asdasd.demo;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Girl {
@Id
@GeneratedValue
private Integer id;
private String cupSize;
private Integer age;

public Girl(){

}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getCupSize() {
return cupSize;
}

public void setCupSize(String cupSize) {
this.cupSize = cupSize;
}

public Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}
}

==注意要加 @Entity 注解==