欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring JPA自动生成数据表

程序员文章站 2022-05-04 16:26:23
...

在暑期实习中,学习到了JPA自动生成数据表的方法,以下是方法的纪录:

spring:
  thymeleaf:
    mode: HTML5
  # 配置数据源
  datasource:
    url: jdbc:mysql://localhost:3306/community_management?serverTimezone=Asia/Shanghai&characterEnconding=utf-8
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: xxxxx
  # JPA 的配置
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true

创建要转为表的类:

@Entity
@Table(name = "users")
public class User {
    @Id                    //主键
    @GeneratedValue        //自增
    private Long id;
    private String username;
    private String password;
    private String sex;
    private Integer age;
    private Integer phone;
    private String avatar;
    private String role;
    private String createTime;
    private String address;
    private String user_health;
    }

运行后通过mysql-front查看表的建立情况:
Spring JPA自动生成数据表
创建成功