2

想在部署的时候随应用的启动而初始化数据脚本,这不就是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方法省略
}

用到的注解简要说明一下:

  1. @javax.persistence.Table 修改默认ORM规则,属性name设置表名;
  2. @org.hibernate.annotations.Table 建表时的描述, 属性comment修改表描述;
  3. @Id 主键
  4. @GenericGenerator 该注解为Hibernate的注解,用来生成表的主键策略,属性strategy的值在类DefaultIdentifierGeneratorFactory中定义:

    • uuid2: UUIDGenerator.class;
    • guid: GUIDGenerator.class;
    • uuid: UUIDHexGenerator.class;
    • uuid.hex: UUIDHexGenerator.class;
    • assigned: Assigned.class;
    • identity: IdentityGenerator.class;
    • select: SelectGenerator.class;
    • sequence: SequenceStyleGenerator.class;
    • seqhilo: SequenceHiLoGenerator.class;
    • increment: IncrementGenerator.class;
    • foreign: ForeignGenerator.class;
    • sequence-identity: SequenceIdentityGenerator.class;
    • enhanced-sequence: SequenceStyleGenerator.class;
    • enhanced-table: TableGenerator.class;
  5. @GeneratedValue 设置主键策略,这里属性generator指向@GenericGenerator策略的name,属性strategy有四个枚举值:

    • GenerationType.TABLE 使用一个额外的表来存储主键;
    • GenerationType.SEQUENCE 使用序列的方式存储,且需要数据库底层支持;
    • GenerationType.IDENTITY 由数据库生成,一般为主键自增等;
    • GenerationType.AUTO 表示由程序生成,不声明则默认为该属性;
  6. @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配置(注意书写格式,这里使用的是yml文件):

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/techno?useUnicode=true&characterEncoding=utf8
    username: root
    password: root
  jpa:
    hibernate:
      ddl-auto: update
    database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
    properties:
      hibernate:
        dialect: com.jason.config.MySQL5TableType

jpa.hibernate.ddl-auto使用update,其它值及说明为:

  • create: 启动服务时都会重新创建表,且不管表存不存在;
  • create-drop: 启动服务时都会重新创建表,且不管表存不存在,服务停止时删除所有表,不管表中是否有数据;
  • update: 启动服务时,自动更新表结构,但数据库表中存在的旧字段不会删除;
  • validate: 启动服务时验证表结构,若表结构存在差异则抛出异常;

至此,Sprign Data JPA生成表结构就完成了,当我们建立数据库后,启动服务就可以在MySQL中得到表结构了,应用可以通过ApplicaitonRunner或者CommandLineRunner接口一键部署,省去了初始化SQL等不必要的操作,这两个接口的简单使用可以参考我的另外一篇文章

最后我们再上一个例子,主要是写入默认值等,部分说明就直接以注释的形式写到代码里,还有@ColumnDefault注解这里就不做说明,大家可以自己了解下:

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.hibernate.annotations.*;
import org.springframework.data.jpa.repository.*;

import javax.persistence.*;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Temporal;
import java.util.Date;

@ApiModel(value = "账号基础信息")
@Entity
@Table(name = "basic_account")
@org.hibernate.annotations.Table(appliesTo = "basic_account", comment = "账号基础信息表")
public class AccountDO {

    @ApiModelProperty(name = "accountId", value = "账号Id", hidden = true)
    @Id
    @GenericGenerator(name = "idGenerator", strategy = "uuid")
    @GeneratedValue(generator = "idGenerator")
    @Column(name = "ACCOUNT_ID", length = 32)
    private String accountId;
    
    @ApiModelProperty(name = "username", value = "账号", dataType = "String", required = true)
    @Column(length = 32, nullable = false)
    private String username;

    // 假设密码加密后长度为128
    @ApiModelProperty(name = "password", value = "密码", dataType = "String", required = true)
    @Column(length = 128, nullable = false)
    private String password;

    // 新建的账号未过期,默认值给1,这个值由数据库生成,则设置insertable为false
    @ApiModelProperty(name = "isAccountNonExpired", value = "账号是否过期", hidden = true)
    @Column(name = "IS_ACCOUNT_EXPIRED", insertable = false, columnDefinition = "CHAR(1) NOT NULL DEFAULT '1' COMMENT '账号 0:过期;1:未过期'")
    private Integer isAccountNonExpired;

    // 新建的账号未锁定,默认值给1,这个值由数据库生成,则设置insertable为false
    @Column(name = "IS_ACCOUNT_LOCKED", insertable = false, columnDefinition = "CHAR(1) NOT NULL DEFAULT '1' COMMENT '账号 0:锁定;1:未锁定'")
    @ApiModelProperty(name = "isAccountNonLocked", value = "账号是否锁定", hidden = true)
    private Integer isAccountNonLocked;

    // 新建的账号密码未过期,默认值给1,这个值由数据库生成,则设置insertable为false
    @Column(name = "IS_CREDENTIALS_EXPIRED", insertable = false, columnDefinition = "CHAR(1) NOT NULL DEFAULT '1' COMMENT '密码 0:已过期;1:未过期'")
    @ApiModelProperty(name = "isCredentialsNonExpired", value = "密码是否过期", hidden = true)
    private Integer isCredentialsNonExpired;

    // 新建的账号需要激活,默认值给0,这个值由数据库生成,设置insertable为false
    @ApiModelProperty(name = "isEnabled", value = "账号是否可用", hidden = true)
    @Column(name = "IS_ENABLE", insertable = false, columnDefinition = "CHAR(1) NOT NULL DEFAULT '0' COMMENT '账号 0:不可用;1:可用'")
    private Integer isEnabled;

    // 新建的账号,默认值给1,这个值由数据库生成,设置insertable为false
    @ApiModelProperty(name = "isDelete", value = "账号是否删除", hidden = true)
    @Column(name = "IS_DELETE", insertable = false, columnDefinition = "CHAR(1) NOT NULL DEFAULT '1' COMMENT '账号 0:已删除;1:未删除'")
    private Integer isDelete;

    // 新建账号时间不能修改,设置updatable为false,但此处不能设置insertable = false
    // @Temporal(TemporalType.TIMESTAMP) 由于表字段类型为TIMESTAMP,所以将Date转换为TIMESTAMP
    @ApiModelProperty(name = "createTimestamp", value = "创建时间")
    @Column(name = "CREATE_TIMESTAMP", nullable = false, updatable = false)
    @Temporal(TemporalType.TIMESTAMP)
    @CreationTimestamp
    private Date createTimestamp;
    
    public AccountDO(String username,String password) {
        this.username = username;
        this.password = password;
    }
    
    // 其他构造方法及get/set方法省略
}

注:@ApiModel以及@ApiModelPropertyswagger注解。

我们注册账号的单元测试就可以直接写成下面这样,仅填写账号和密码,其他值则由数据库生成:

    @Test
    public void saveAccount() throws Exception {
        accountService.saveAccount(new AccountDO("123456", "654321"));
    }

原创不易,感谢支持。


虚惊一百场
19 声望7 粉丝

1 + 1 = 2