有时候,我们在数据库中会插入一些字段的值时固定的,比如性别字段,它的值固定只有2个,男或者女; 或者季节字段,只有春夏秋冬4种。有时候在Java类中会采用枚举类型来表达相应的数据库字段,比如如下数据表:

create table student(
id int auto_increment comment '学生ID',
name varchar(50),
gender varchar(10),
primary key(id));

 

枚举类定义如下:

package com.example.demo.dao;

public enum Gender {
    FEMAIL("女"),
    MAIL("男");

    private String sex;
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }

    Gender(String s){
        this.sex=s;
    }
}

实体类定义如下:

mybatis对枚举类型的处理
 1 package com.example.demo.dao;
 2 
 3 public class Student {
 4     private int id;
 5     private String name;
 6     private Gender gender;
 7 
 8     public int getId() {
 9         return id;
10     }
11 
12     public void setId(int id) {
13         this.id = id;
14     }
15 
16     public String getName() {
17         return name;
18     }
19 
20     public void setName(String name) {
21         this.name = name;
22     }
23 
24     public Gender getGender() {
25         return gender;
26     }
27 
28     public void setGender(Gender gender) {
29         this.gender = gender;
30     }
31 }
View Code

相关文章:

  • 2022-12-23
  • 2021-09-27
  • 2021-06-13
  • 2021-12-21
  • 2022-02-23
  • 2021-07-30
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-06-09
  • 2021-09-04
  • 2021-05-22
  • 2022-12-23
相关资源
相似解决方案