1.配置文件application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/bootTable?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.database=mysql
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
2.pom.xml引入包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
3.编写实体类
import javax.persistence.*;
import java.io.Serializable;
import java.util.Objects;
@Entity
//声明实体类
public class User implements Serializable {
@Id
//声明了实体唯一标识对应的属性
@GeneratedValue
//自增
private Integer id;
@Column(nullable = false, unique = true, length = 32)
//长度32,唯一索引,nullable表示true可以为空,false不可以
//用来声明实体属性的表字段的定义
private String userName;
private String passWord;
private String email;
private String nickName;
private String regTime;
@Transient
//不映射成列的字段
private String desc;
//省略get和set方法
}
4.运行项目,启动即可生成
5.针对项目启动以后数据库并未生成数据库表问题:
包导的不对: import javax.persistence.*;
配置文件不对: spring.jpa.hibernate.ddl-auto=update
注解写的不对:不要忘记@Entity
…
还可能有一种原因:
Sprint的入口文件在子目录里了,应该比其他诸如service、dao、controller、entity高一级。
例如:service文件所在为com.demo.metaService,那么入口文件xxxApplication.java应该在com.demo下