Hibernate建表——将MySQL的JSON列映射到Java字段(Jpa/Hibernate——Java)
背景
- 从 MySQL 5.7.8 开始,MySQL 支持原生的 JSON 数据类型。
- 那使用ORM框架时,如何把MySQL的JSON类型映射到Java字段上?
开发环境
- Maven3.5
- Spring Boot 2.2.1 RELEASE
- Spring Data JPA 2.2.1 RELEASE
- Hibernate 5.4.10 Final
使用如下
- pom.xml引入(只显示和文章有关的依赖)
-
<properties> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> <resource.delimiter>@</resource.delimiter> <spring-boot-starter.version>2.2.1.RELEASE</spring-boot-starter.version> </properties> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>${spring-boot-starter.version}</version> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> <version>${spring-boot-starter.version}</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.4.10.Final</version> </dependency> <dependency> <groupId>com.vladmihalcea</groupId> <artifactId>hibernate-types-52</artifactId> <version>2.9.4</version> </dependency>
- 注意:一定要引入hibernate-types-XXXX这个依赖!!
- Java实体配置
-
/** * @author ******** * @Description: 审计记录 * @date 2020/4/27 */ @Entity @Table(name = "audit_record", uniqueConstraints = { @UniqueConstraint(name = "audit_record_mid",columnNames = {"audit_record_mid"}) }) @Getter @Setter @DynamicInsert @DynamicUpdate @TypeDef(name="json",typeClass = JsonStringType.class) public class AuditRecordTable implements Serializable { @JsonIgnore @Id @GeneratedValue(strategy = GenerationType.SEQUENCE) @Column(name = "audit_record_id", columnDefinition = "BIGINT", updatable = false, nullable = false, unique = true) private BigInteger auditRecordId; @JsonIgnore @Column(name = "audit_record_mid", columnDefinition = "varchar(36) NOT NULL COMMENT '记录编号'") private String auditRecordMid; @JsonIgnore @Column(name = "audit_date", columnDefinition = "varchar(36) NOT NULL COMMENT '审计日期'") private String auditDate; @JsonIgnore @Column(name = "order_total_amount", columnDefinition = "decimal(20,6) NOT NULL COMMENT '订单总金额'") private BigDecimal orderTotalAmount; @JsonIgnore @Column(name = "payment_total_amount", columnDefinition = "decimal(20,6) NOT NULL COMMENT '支付总金额'") private BigDecimal paymentTotalAmount; @JsonIgnore @Type(type="json") @Column(name = "paied_order_mid_list", columnDefinition = "json") private List<String> paiedOrderMidList; @JsonIgnore @CreationTimestamp @Column(name = "cdate", updatable = false, columnDefinition = "timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP") private LocalDateTime cdate; @JsonIgnore @UpdateTimestamp @Column(name = "mdate", columnDefinition = "timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP") private LocalDateTime mdate; }
- Java实体中,对应MySQL中的JSON类型配置已标记颜色!!!
- MySQL自动建表如下图
-