溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何解決springboot jpa@Column columnDefinition等屬性失效問(wèn)題

發(fā)布時(shí)間:2021-10-25 10:07:52 來(lái)源:億速云 閱讀:259 作者:iii 欄目:開發(fā)技術(shù)

本篇內(nèi)容主要講解“如何解決springboot jpa@Column columnDefinition等屬性失效問(wèn)題”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“如何解決springboot jpa@Column columnDefinition等屬性失效問(wèn)題”吧!

jpa @Column columnDefinition屬性失效

刪除一條屬性,默認(rèn)false

#spring.jpa.properties.hibernate.globally_quoted_identifiers=true

原因

開啟后, 創(chuàng)建sql語(yǔ)句執(zhí)行時(shí)會(huì)添加'`', 會(huì)造成columnDefinition 屬性失效, author: dreamlu

例如

1.屬性設(shè)置為true

alter table `xxx` add column `xxx` `varchar(50) default ''`
// sql 語(yǔ)法錯(cuò)誤

2.屬性為false

alter table xxx add column xx varchar(50) default ''
// 執(zhí)行成功

可以看出: 有舍有得,第二種要求字段/表等命名不能和mysql或其他數(shù)據(jù)庫(kù)中關(guān)鍵字重名

jpa column注解

知識(shí)點(diǎn)

@Column注解一共有10個(gè)屬性,這10個(gè)屬性均為可選屬性,各屬性含義分別如下:

  • name:name屬性定義了被標(biāo)注字段在數(shù)據(jù)庫(kù)表中所對(duì)應(yīng)字段的名稱;

  • unique:unique屬性表示該字段是否為唯一標(biāo)識(shí),默認(rèn)為false。如果表中有一個(gè)字段需要唯一標(biāo)識(shí),則既可以使用該標(biāo)記,也可以使用@Table標(biāo)記中的@UniqueConstraint。

  • nullable :nullable屬性表示該字段是否可以為null值,默認(rèn)為true。

  • insertable :insertable屬性表示在使用“INSERT”腳本插入數(shù)據(jù)時(shí),是否需要插入該字段的值。

  • updatable:updatable屬性表示在使用“UPDATE”腳本插入數(shù)據(jù)時(shí),是否需要更新該字段的值。insertable和updatable屬性一般多用于只讀的屬性,例如主鍵和外鍵等。這些字段的值通常是自動(dòng)生成的。

  • columnDefinition :columnDefinition屬性表示創(chuàng)建表時(shí),該字段創(chuàng)建的SQL語(yǔ)句,一般用于通過(guò)Entity生成表定義時(shí)使用。(也就是說(shuō),如果DB中表已經(jīng)建好,該屬性沒(méi)有必要使用。)

  • table :table屬性定義了包含當(dāng)前字段的表名。

  • length :length屬性表示字段的長(zhǎng)度,當(dāng)字段的類型為varchar時(shí),該屬性才有效,默認(rèn)為255個(gè)字符。

  • precisionscale :precision屬性和scale屬性表示精度,當(dāng)字段類型為double時(shí),precision表示數(shù)值的總長(zhǎng)度,scale表示小數(shù)點(diǎn)所占的位數(shù)。

precision和scale疑點(diǎn)

@Table(name = "CUSTOMERS")
@Entity
public class Customer {
    @Column(name = "ID")
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Id
    private Integer id; 
    @Column(name = "Name")
    private String name;
 
    @Column(name = "Email", nullable = true, length = 128)
    private String email;
 
    @Column(name = "Age")
    private int age;
 
    @Column(name = "Remark", columnDefinition = "text")
    private String remark;
 
    @Column(name = "Salary1", columnDefinition = "decimal(5,2)")
    private double salary1;
 
    @Column(name = "Salary2", precision = 5, scale = 2)
    private double salary2;
 
    @Column(name = "Salary3", columnDefinition = "decimal(5,2)")
    private BigDecimal salary3;
 
    @Column(name = "Salary4", precision = 5, scale = 2)
    private BigDecimal salary4;
    ......
}

數(shù)據(jù)庫(kù)DDL:

CREATE TABLE `customers` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `Age` int(11) DEFAULT NULL,
  `Email` varchar(128) DEFAULT NULL,
  `Name` varchar(255) DEFAULT NULL,
  `Remark` text,
  `Salary1` decimal(5,2) DEFAULT NULL,
  `Salary2` double DEFAULT NULL,
  `Salary3` decimal(5,2) DEFAULT NULL,
  `Salary4` decimal(5,2) DEFAULT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

小結(jié)一下

1.double類型若在columnDefinition屬性中指定數(shù)字類型為decimal并指定精度,則最終以columnDefinition為準(zhǔn) (oracle數(shù)據(jù)庫(kù)中除外,其指定為float類型,因?yàn)閛racle數(shù)據(jù)庫(kù)沒(méi)有double類型,若針對(duì)oracle數(shù)據(jù)庫(kù)進(jìn)行精確,則改為

@Column(name = "Salary1", columnDefinition = "decimal(5,2)")  //或columnDefinition = "number(5,2)"
    private Float salary1;

2.double類型將在數(shù)據(jù)庫(kù)中映射為double類型,precision和scale屬性無(wú)效

3.BigDecimal類型在數(shù)據(jù)庫(kù)中映射為decimal類型,precision和scale屬性有效

4.precision和scale屬性只在BigDecimal類型中有效

到此,相信大家對(duì)“如何解決springboot jpa@Column columnDefinition等屬性失效問(wèn)題”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI