mysql enum字段類型的謹慎使用
為什么使用枚舉
限定值的取值范圍,比如性別(男,女,未知)等。
枚舉類型使用陷阱
1.超級不推薦在mysql中設置某一字段類型為enum,但是存的值為數(shù)字,比如‘0’,‘1’,‘2’;
- 解釋1:你會混淆,因為enum可以通過角標取值,但它的角標是從1開始,對于不熟悉這個字段的人這里會出錯
- 解釋2:enum類型的字段對于0與‘0’有非常大的區(qū)別,如果你是用0當角標做操作,因它沒有這個角標,所要會報錯;如果你使用‘0’這個值去取枚舉值,并做插入操作,你會發(fā)現(xiàn)它竟然會成功,但是插入的結(jié)果是一個“空”(不是null)
- 解釋3:enum類型對于php等弱語言類型的支持很差,弱語言類型打引號和不打引號的值可能是同一類型,但是對于mysql中enum類型的字段來說,那就不一定是一回事了
結(jié)論:總之,不要拿mysql的enum類型取存一些數(shù)字;如果你一定要使用這個字段去存數(shù)字,請把這個字段定義為int,然后在java代碼中使用枚舉類做一個對于這個字段值范圍的一個限定?。ê竺嬗写a)
2.你可能會報這個錯——Caused by: java.sql.SQLException: Data truncated for column 'Color' at row 1 ;
- 原因:Jpa默認使用整數(shù)順序值持久化枚舉類型;
- Mysql中枚舉類型Color定義取值的順序是RED、GREEN、BLUE,因此,當這三個取值持久化到數(shù)據(jù)庫表時,取值分別是0、1、2;
- 意思就是我們這里存往數(shù)據(jù)庫的數(shù)據(jù)是0、1、2這樣的數(shù)字,而不是RED、GREEN、BLUE字符串, 但是Mysql數(shù)據(jù)庫中定義的是RED、GREEN、BLUE,并沒有其它值所以報錯
解決:在entity中使用@Enumerated(EnumType.STRING)標注你的枚舉類型屬性,如果標注,默認是integer
使用例子:
建表語句為
CREATE TABLE test4 ( id BIGINT UNSIGNED PRIMARY KEY AUTO_INCREMENT, brand VARCHAR(255) NOT NULL, color ENUM('RED','GREEN','BLUE') ) ENGINE = InnoDB;
Java代碼中,枚舉類
public enum Color { RED, GREEN, BLUE }
Java代碼中,Javabean
@Entity @Table(name="test4") public class ClothesRight { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Enumerated(EnumType.STRING) private Color color; private String brand; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public ClothesRight(Long id, Color color, String brand) { super(); this.id = id; this.color = color; this.brand = brand; } public Color getColor() { return color; } public void setColor(Color color) { this.color = color; } public ClothesRight() { super(); } }
簡單使用:
public interface Test4RightRepository extends JpaRepository<ClothesRight, Long>{ }
@Autowired private Test4RightRepository t4R; /** * 使用@Enumrated()標注字段為枚舉的數(shù)據(jù) * 結(jié)果 正確插入RED */ @GetMapping(value="/addclothesright") public void GetTest4Right(){ List<ClothesRight> entities = new ArrayList<>(); ClothesRight clothes = new ClothesRight(); //clothes.setId(1L); clothes.setBrand("佐丹奴"); clothes.setColor(Color.RED); entities.add(clothes); t4R.save(entities); }
結(jié)果為:
插入數(shù)字例子:
建表
CREATE TABLE test5num ( id BIGINT UNSIGNED PRIMARY KEY AUTO_INCREMENT, used int(11) DEFAULT NULL COMMENT '0:沒用過 1:已用過 2:不能用' )ENGINE = InnoDB;
Java代碼為:
@Entity @Table(name="test5num") public class Test5Num { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private Long id; private Used used; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public Used getUsed() { return used; } public void setUsed(Used used) { this.used = used; } public Test5Num() { super(); } public Test5Num(Long id, Used used) { super(); this.id = id; this.used = used; } }
/** *枚舉類 */ public enum Used { UNUSED(0,"沒用過"), USED(1,"已用過"), FORBIDDEN(2,"不能用"); private Integer code; private String discribe; public Integer getCode() { return code; } public String getDiscribe() { return discribe; } private Used(Integer code, String discribe) { this.code = code; this.discribe = discribe; } }
/** * dao層 */ public interface Test5NumRepository extends JpaRepository<Test5Num, Long>{ }
@Autowired private Test5NumRepository t5N; /** * mysql枚舉的字段類型不宜插入數(shù)字,但是需求就是要用數(shù)字,怎么辦? * 解決:mysql數(shù)據(jù)類型定義為int,枚舉限定在java代碼中解決 * */ @GetMapping("/test5insert") public void insertT5(){ Test5Num t5 = new Test5Num(); t5.setUsed(Used.USED); List<Test5Num> list = new ArrayList<Test5Num>(); list.add(t5); t5N.save(list); }
結(jié)果:
到此這篇關(guān)于mysql enum字段類型的謹慎使用的文章就介紹到這了,更多相關(guān)mysql enum字段類型內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務器上建立鏡像,否則將依法追究法律責任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學習參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。