springdata jpa自动建表

Posted Rain_in_Summer

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springdata jpa自动建表相关的知识,希望对你有一定的参考价值。

application.properties文件内容为:

spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/demo?characterEncoding=utf-8&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true

自动建表实体对象为:

package com.ysoo.entity;

import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;
import java.io.Serializable;

@Entity
@Table(name = "t_user")
public class User implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "uuid")
    @GenericGenerator(name = "uuid", strategy = "uuid2")
    @Column(name = "id")
    private String id;

    @Column(name = "name")
    private String name;

    @Column(name = "password")
    private String password;

    public String getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}
@GeneratedValue和@GenericGenerator注解需要如上配置 否则会出现建表不成功或者生成hibernate_sequence多余的表

 

以上是关于springdata jpa自动建表的主要内容,如果未能解决你的问题,请参考以下文章

JPA 自动建表- @Column

SpringDataJAP:黑马 SpringData JPA 学习笔记

SpringDataJAP:黑马 SpringData JPA 学习笔记

Sprindata jpa

JPA 自动建表- @Id,@GeneratedValue 与 @GenericGenerator 设置主键生成策略

快速入门JPA+SpringData 视频 学习笔记