社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  DATABASE

还在 MySQL 中使用枚举?这些陷阱一定要注意!

码小辫 • 2 年前 • 333 次点击  

码小辫
专注更多编程视频和电子书
天天在用钱

作者:Alchain奇

来源:blog.csdn.net/u011442682/article/details/79078199


为什么使用枚举

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

枚举类型使用陷阱

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

使用例子

建表语句为
 CREATE TABLE test4 (  
     id BIGINT UNSIGNED  PRIMARY KEY AUTO_INCREMENT,  
     brand VARCHAR(255NOT 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<ClothesRightLong>{
}
@Autowired
 private Test4RightRepository t4R;
/**
  * 使用@Enumrated()标注字段为枚举的数据
  * 结果 正确插入RED
  */

 @GetMapping(value="/addclothesright")
 public void GetTest4Right(){
  List entities = new ArrayList<>();
  ClothesRight clothes = new ClothesRight();  
  //clothes.setId(1L);
  clothes.setBrand("佐丹奴");  
  clothes.setColor(Color.RED); 
  entities.add(clothes);
  t4R.save(entities);
 }
结果为:

插入数字例子(不推荐)

建表
CREATE TABLE test5num (  
 id BIGINT UNSIGNED  PRIMARY KEY AUTO_INCREMENT,  
  used int(11DEFAULT 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<Test5NumLong>{
}
@Autowired
private Test5NumRepository t5N;
     /**
 * mysql枚举的字段类型不宜插入数字,但是需求就是要用数字,怎么办?
 * 解决:mysql数据类型定义为int,枚举限定在java代码中解决
 * 
 */

@GetMapping("/test5insert")
public void insertT5(){
 Test5Num t5 = new Test5Num();
 t5.setUsed(Used.USED);
 List list = new ArrayList();
 list.add(t5);
 t5N.save(list);
}
结果:


-END-

一个认真分享的小编

前沿技术定期送书 /干货分享

商务合作:dot3721
长按左侧二维码添加

点分享

点点赞

点在看




    


Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/136629
 
333 次点击