亲宝软件园·资讯

展开

mysql enum字段类型

Alchain奇 人气:0

为什么使用枚举

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

枚举类型使用陷阱

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

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

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

解决:在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);
    }

结果:

加载全部内容

相关教程
猜你喜欢
用户评论