zoukankan      html  css  js  c++  java
  • 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自动建表如下图
  • 相关阅读:
    2018-2019-1 20165307 20165327 20165332 实验一 开发环境的熟悉
    2018-2019-1 20165332 《信息安全系统设计基础》第4周学习总结
    2018-2019-2 20165329 《网络对抗技术》Exp0 Kali安装 Week1
    20165311 20165329 20165334 实验五 通讯协议设计-1
    2018-2019-1 20165329 《信息安全系统设计基础》第九周学习总结
    20165329 20165311 20165334 实验四外设驱动程序设计
    20165329 mybash的实现
    20165329 实现mypwd
    2018-2019-1 20165329 《信息安全系统设计基础》第七周学习总结
    2018-2019-1 20165329 《信息安全系统设计基础》第6周学习总结
  • 原文地址:https://www.cnblogs.com/zuiyue_jing/p/12797464.html
Copyright © 2011-2022 走看看