jpa配置根据实体类自动创建表
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>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</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下
jpa根据Entry自动生成表
1.加入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
若有依赖 spring-data-jpa 则删掉,否则会出现找不到 bootstrap 之类的错误
<!-- <dependency>-->
<!-- <groupId>org.springframework.data</groupId>-->
<!-- <artifactId>spring-data-jpa</artifactId>-->
<!-- <version>2.1.4.RELEASE</version>-->
<!-- </dependency>-->
2.配置 application.yml
增加Jpa 自动生成表的配置
spring:
jpa: ##配置自动建表:updata:没有表新建,有表更新操作,控制台显示建表语句
hibernate:
ddl-auto: update
show-sql: true
3. 创建Entity
个人建议创建一个基础Entity,用于表中常用字段创建配合 mybatisplus,jackson,SnowFlake,lombok 等库,自行导入相关注解请自行了解
BaseEntry.java
@Data//省略setget方法
@MappedSuperclass //标注父类
@EntityListeners(AuditingEntityListener.class) //jpa数据监听
@JsonIgnoreProperties(value={"hibernateLazyInitializer","handler","fieldHandler"}) //忽略解析的字段
public abstract class BaseEntry implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@TableId
@ApiModelProperty(value = "唯一标识")
private String id = String.valueOf(SnowFlakeUtil.getFlowIdInstance().nextId());
@ApiModelProperty(value = "创建者")
@CreatedBy
private String createBy;
@CreatedDate
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@ApiModelProperty(value = "创建时间")
private Date createTime;
@ApiModelProperty(value = "更新者")
@LastModifiedBy
private String updateBy;
@LastModifiedDate
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@ApiModelProperty(value = "更新时间")
private Date updateTime;
@ApiModelProperty(value = "删除标志 默认0")
@TableLogic
private Integer delFlag = CommonConstant.STATUS_NORMAL;
}
业务Entry ,仅做参考
@Data
@Entity
@Table(name = "tb_bussiness_up_record")
@TableName("tb_bussiness_up_record")
public class TbBussinessUpRecord extends BaseEntry {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "经销商")
private String bussinessId;
@ApiModelProperty(value = "审核时间")
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private String auditTime;
}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。