mysql enum字段类型的谨慎使用

吾爱主题 阅读:115 2024-04-01 23:19:53 评论:0
目录
  • 为什么使用枚举
  • 枚举类型使用陷阱
  • 使用例子:
  • 插入数字例子:

为什么使用枚举

限定值的取值范围,比如性别(男,女,未知)等。

枚举类型使用陷阱

1.超级不推荐在mysql中设置某一字段类型为enum,但是存的值为数字,比如‘0’,‘1’,‘2’;

  • 解释1:你会混淆,因为enum可以通过角标取值,但它的角标是从1开始,对于不熟悉这个字段的人这里会出错
  • 解释2:enum类型的字段对于0与‘0’有非常大的区别,如果你是用0当角标做操作,因它没有这个角标,所要会报错;如果你使用‘0’这个值去取枚举值,并做插入操作,你会发现它竟然会成功,但是插入的结果是一个“空”(不是null)
  • 解释3:enum类型对于php等弱语言类型的支持很差,弱语言类型打引号和不打引号的值可能是同一类型,但是对于mysql中enum类型的字段来说,那就不一定是一回事了

结论:总之,不要拿mysql的enum类型取存一些数字;如果你一定要使用这个字段去存数字,请把这个字段定义为int,然后在java代码中使用枚举类做一个对于这个字段值范围的一个限定!(后面有代码)

2.你可能会报这个错——Caused by: java.sql.SQLException: Data truncated for column 'Color' at row 1  ;

  • 原因:Jpa默认使用整数顺序值持久化枚举类型;
  • Mysql中枚举类型Color定义取值的顺序是RED、GREEN、BLUE,因此,当这三个取值持久化到数据库表时,取值分别是0、1、2;
  • 意思就是我们这里存往数据库的数据是0、1、2这样的数字,而不是RED、GREEN、BLUE字符串, 但是Mysql数据库中定义的是RED、GREEN、BLUE,并没有其它值所以报错

解决:在entity中使用@Enumerated(EnumType.STRING)标注你的枚举类型属性,如果标注,默认是integer

使用例子:

建表语句为

?
1 2 3 4 5 CREATE TABLE test4 (       id BIGINT UNSIGNED  PRIMARY KEY AUTO_INCREMENT,       brand VARCHAR (255) NOT NULL      color ENUM( 'RED' , 'GREEN' , 'BLUE' )   ) ENGINE = InnoDB;

Java代码中,枚举类

?
1 2 3 4 5 public enum Color {       RED,        GREEN,        BLUE  }

Java代码中,Javabean

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 @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 ();      }      }

简单使用:

?
1 2 3 public interface Test4RightRepository extends JpaRepository<ClothesRight, Long>{   }
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 @Autowired      private Test4RightRepository t4R; /**       * 使用@Enumrated()标注字段为枚举的数据       * 结果 正确插入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);      }

结果为:

插入数字例子:

建表

?
1 2 3 4 CREATE TABLE test5num (       id BIGINT UNSIGNED  PRIMARY KEY AUTO_INCREMENT,        used int (11) DEFAULT NULL COMMENT '0:没用过  1:已用过  2:不能用'   )ENGINE = InnoDB;

Java代码为:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 @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;      }      }
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 /** *枚举类 */ 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;      } }
?
1 2 3 4 5 6 /**   * dao层   */ public interface Test5NumRepository extends JpaRepository<Test5Num, Long>{   }
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 @Autowired      private Test5NumRepository t5N;         /**       * mysql枚举的字段类型不宜插入数字,但是需求就是要用数字,怎么办?       * 解决:mysql数据类型定义为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);      }

结果:

到此这篇关于mysql enum字段类型的谨慎使用的文章就介绍到这了,更多相关mysql enum字段类型内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/u011442682/article/details/79078199

可以去百度分享获取分享代码输入这里。
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

【腾讯云】云服务器产品特惠热卖中
搜索
标签列表
    关注我们

    了解等多精彩内容