mysql enum字段類型的謹(jǐn)慎使用
為什么使用枚舉
限定值的取值范圍,比如性別(男,女,未知)等。
枚舉類型使用陷阱
1.超級(jí)不推薦在mysql中設(shè)置某一字段類型為enum,但是存的值為數(shù)字,比如‘0’,‘1’,‘2’;
- 解釋1:你會(huì)混淆,因?yàn)閑num可以通過角標(biāo)取值,但它的角標(biāo)是從1開始,對(duì)于不熟悉這個(gè)字段的人這里會(huì)出錯(cuò)
- 解釋2:enum類型的字段對(duì)于0與‘0’有非常大的區(qū)別,如果你是用0當(dāng)角標(biāo)做操作,因它沒有這個(gè)角標(biāo),所要會(huì)報(bào)錯(cuò);如果你使用‘0’這個(gè)值去取枚舉值,并做插入操作,你會(huì)發(fā)現(xiàn)它竟然會(huì)成功,但是插入的結(jié)果是一個(gè)“空”(不是null)
- 解釋3:enum類型對(duì)于php等弱語言類型的支持很差,弱語言類型打引號(hào)和不打引號(hào)的值可能是同一類型,但是對(duì)于mysql中enum類型的字段來說,那就不一定是一回事了
結(jié)論:總之,不要拿mysql的enum類型取存一些數(shù)字;如果你一定要使用這個(gè)字段去存數(shù)字,請(qǐng)把這個(gè)字段定義為int,然后在java代碼中使用枚舉類做一個(gè)對(duì)于這個(gè)字段值范圍的一個(gè)限定!(后面有代碼)
2.你可能會(huì)報(bào)這個(gè)錯(cuò)——Caused by: java.sql.SQLException: Data truncated for column 'Color' at row 1 ;
- 原因:Jpa默認(rèn)使用整數(shù)順序值持久化枚舉類型;
- Mysql中枚舉類型Color定義取值的順序是RED、GREEN、BLUE,因此,當(dāng)這三個(gè)取值持久化到數(shù)據(jù)庫(kù)表時(shí),取值分別是0、1、2;
- 意思就是我們這里存往數(shù)據(jù)庫(kù)的數(shù)據(jù)是0、1、2這樣的數(shù)字,而不是RED、GREEN、BLUE字符串, 但是Mysql數(shù)據(jù)庫(kù)中定義的是RED、GREEN、BLUE,并沒有其它值所以報(bào)錯(cuò)
解決:在entity中使用@Enumerated(EnumType.STRING)標(biāo)注你的枚舉類型屬性,如果標(biāo)注,默認(rèn)是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(); } }
簡(jiǎn)單使用:
public interface Test4RightRepository extends JpaRepository<ClothesRight, Long>{ }
@Autowired private Test4RightRepository t4R; /** * 使用@Enumrated()標(biāo)注字段為枚舉的數(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字段類型的謹(jǐn)慎使用的文章就介紹到這了,更多相關(guān)mysql enum字段類型內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MySQL數(shù)據(jù)庫(kù)入門之多實(shí)例配置方法詳解
這篇文章主要介紹了MySQL數(shù)據(jù)庫(kù)入門之多實(shí)例配置方法,結(jié)合實(shí)例形式分析了MySQL數(shù)據(jù)庫(kù)多實(shí)例配置相關(guān)概念、原理、操作方法與注意事項(xiàng),需要的朋友可以參考下2020-05-05Mysql8報(bào)錯(cuò)this is incompatible with sql_mo
這篇文章主要介紹了Mysql8報(bào)錯(cuò)this is incompatible with sql_mode=only_full_group_by問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01詳解sql中的參照完整性(一對(duì)一,一對(duì)多,多對(duì)多)
這篇文章主要介紹了sql中的參照完整性,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04大數(shù)據(jù)小內(nèi)存排序問題如何巧妙解決
文章介紹了大數(shù)據(jù)小內(nèi)存排序的三種方法:數(shù)據(jù)庫(kù)排序、分治法和位圖法,數(shù)據(jù)庫(kù)排序簡(jiǎn)單但速度慢,對(duì)設(shè)備要求高;分治法高效但實(shí)現(xiàn)復(fù)雜;位圖法可讀性差,但存儲(chǔ)空間受限2025-01-01MySQL之undo日志頁(yè)結(jié)構(gòu)詳解
這篇文章主要介紹了MySQL之undo日志頁(yè)結(jié)構(gòu),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-06-06mysql中g(shù)eneral_log日志知識(shí)點(diǎn)介紹
這篇文章主要介紹了mysql中g(shù)eneral_log日志知識(shí)點(diǎn)的介紹以及其他相關(guān)內(nèi)容,以后興趣的朋友們學(xué)習(xí)下。2019-08-08