mysql enum字段类型的谨慎使用

2022-07-28,,,,

为什么使用枚举

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

枚举类型使用陷阱

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(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()标注字段为枚举的数据
     * 结果 正确插入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);
    }

结果为:

插入数字例子:

建表

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枚举的字段类型不宜插入数字,但是需求就是要用数字,怎么办?
     * 解决: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字段类型内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

《mysql enum字段类型的谨慎使用.doc》

下载本文的Word格式文档,以方便收藏与打印。