1. 程式人生 > >springdata jpa自動建表

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多餘的表