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

Spring Data Jpa 自动生成表结构的方法示例

程序员文章站 2023-12-10 17:19:22
想在部署的时候随应用的启动而初始化数据脚本,这不就是spring data jpa中的自动生成表结构,听起来特别简单,不就是配置hibernate的ddl-auto嘛,有什...

想在部署的时候随应用的启动而初始化数据脚本,这不就是spring data jpa中的自动生成表结构,听起来特别简单,不就是配置hibernateddl-auto嘛,有什么好说的,是个人都知道。当初我也是这样认为,实际操作了一把,虽然表是创建成功了,但是字段注释,字符集以及数据库引擎都不对,没想到在这些细节上翻车了。

毕竟开翻的车还要自己扶起来,于是在这记录一下。

注:本文中使用的spring data jpa版本为2.1.4.release

以mysql为例,我这边举个例子:

import com.fasterxml.jackson.annotation.*;
import org.hibernate.annotations.*;
import org.springframework.data.annotation.*;

import javax.persistence.*;
import javax.persistence.entity;
import javax.persistence.id;
import java.math.bigdecimal;

@entity
@javax.persistence.table(name = "basic_city")
@org.hibernate.annotations.table(appliesto = "basic_city", comment = "城市基本信息")
public class citydo {
  
  @id
  @genericgenerator(name = "idgenerator", strategy = "uuid")
  @generatedvalue(generator = "idgenerator")
  @column(name = "city_id", length = 32)
  private string cityid;
 
  @column(name = "city_name_cn", columndefinition = "varchar(255) not null comment '名称(中文)'")
  private string citynamecn;
  
  @column(name = "city_name_en", columndefinition = "varchar(255) not null comment '名称(英文)'")
  private string citynameen;

  @column(name = "longitude", precision = 10, scale = 7)
  private bigdecimal longitude;

  @column(name = "latitude", precision = 10, scale = 7)
  private bigdecimal latitude;

  @column(name = "elevation", precision = 5)
  private integer elevation;

  @column(name = "city_description", length = 500)
  private string citydescription;
  
  // 构造方法及get/set方法省略
}

  • @javax.persistence.table 修改默认orm规则,属性name设置表名;
  • @org.hibernate.annotations.table 建表时的描述, 属性comment修改表描述;
  • @id 主键
  • @genericgenerator 设置主键策略,这里使用了hibernate的uuid来生成主键;
  • @generatedvalue 设置主键值,属性generator关联主键策略的name;
  • @column 修改默认orm规则;
    • name设置表中字段名称,表字段和实体类属性相同,则该属性可不写;
    • unique设置该字段在表中是否唯一,默认false;
    • nullable是否可为空,默认true;
    • insertable表示insert操作时该字段是否响应写入,默认为true;
    • updatable表示update操作时该字段是否响应修改,默认为true;
    • columndefinition是自定义字段,可以用这个属性来设置字段的注释;
    • table表示当映射多个表时,指定表的表中的字段,默认值为主表的表名;
    • length是长度,仅对varchar类型的字段生效,默认长度为255;
    • precision表示一共多少位;
    • scale表示小数部分占precision总位数的多少位,例子中两者共同使用来确保经纬度的精度;

接下来需要设置数据引擎和字符集,网上大把的继承mysql5innodbdialect,但是这个类已经过期了,我们用mysql5dialect来代替:

package com.jason.config;

import org.hibernate.dialect.mysql5dialect;
import org.springframework.stereotype.component;

@component
public class mysql5tabletype extends mysql5dialect {

  @override
  public string gettabletypestring() {
    return "engine=innodb default charset=utf8";
  }
}

然后在spring boot的配置文件中应用上面定义的mysql5tabletype ,使用spring.jpa.properties.hibernate.dialect配置:

spring:
 datasource:
  driver-class-name: com.mysql.jdbc.driver
  url: jdbc:mysql://localhost:3306/techno?useunicode=true&characterencoding=utf8
  username: root
  password: root
 jpa:
  show-sql: true
  hibernate:
   ddl-auto: update
  database-platform: org.hibernate.dialect.mysql5innodbdialect
  properties:
   hibernate:
    dialect: com.jason.config.mysql5tabletype

到此,sprign data jpa生成表就完成了,应用就可以通过applicaitonrunner或者commandlinerunner接口一键部署应用,省去初始化sql等不必要的操作了,这两个接口的简单使用可以参考我的。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。