知识点:在Java中,使用枚举类,当遇到实例类型有限的类时,并且数据库中用状态码代表一种含义时,如星期,性别,员工登陆某系统的状态等等,
可以考虑使用枚举类
本例子可以仿照,也可以使用自定义的类型处理器,处理枚举类型,使用方法参考另一篇博客:https://www.cnblogs.com/shuaifing/p/9330913.html
(1)枚举类
EmpsStatus.java一种状态码代码一种登录状态,如100代表用户已登录,其中getEmpStatusByCode,getEmpEnum都是根据用户的状态码获取对应的枚举类,实际中可以调用作用一致
/*
*
* 保存数据库100,200,状态吗,不是默认的0,或者枚举名
* */
public enum EmpsStatus {
LOGIN(100,"用户登录"),LOGINOUT(200,"用户退出"),REMOVE(300,"用户不存在");
private Integer code;
private String msg;
private EmpsStatus(Integer code,String msg) {
this.code=code;
this.msg=msg;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
//按照状态码返回枚举对象
public static EmpsStatus getEmpStatusByCode(Integer code){
switch (code) {
case 100:
return LOGIN;
case 200:
return LOGINOUT;
case 300:
return REMOVE;
default:
return LOGINOUT;
}
}
public static EmpsStatus getEmpEnum(int code) { //使用int类型
for(EmpsStatus sourceEnum: EmpsStatus.values()) {
if(sourceEnum.getCode() == code) return sourceEnum;
}
return null;
}
}
(2)实体类
Employee.java
public class Employee {
private Integer id;
private String lastName;
private String email;
private String gender;
//员工状态
private EmpsStatus empStatus=EmpsStatus.LOGINOUT;
private String empstsus;//改字段为员工状态码字段,与数据中的字段对应
public Employee() {
// TODO Auto-generated constructor stub
}
public Employee(String lastName, String email, String gender) {
super();
this.lastName = lastName;
this.email = email;
this.gender = gender;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public EmpsStatus getEmpsStatus() {
return empStatus;
}
public void setEmpsStatus(EmpsStatus empsStatus) {
this.empStatus = empsStatus;
}
public String getEmpstsus() {
return empstsus;
}
public void setEmpstsus(String empstsus) {
this.empstsus = empstsus;
}
@Override
public String toString() {
return "Employee [id=" + id + ", lastName=" + lastName + ", email="
+ email + ", gender=" + gender +", empstsus=" + empstsus+ "]";
}
}
(3)测试方法调用
@Test
public void testEnumStaus()throws IOException{
SqlSessionFactory sqlSessionFactory= getSqlSessionFactory();
SqlSession openSession= sqlSessionFactory.openSession();
try{
EmployeeMapper mapper=openSession.getMapper(EmployeeMapper.class);
List<Employee> emplist= mapper.getEmps();
Employee emp=new Employee();
//利用枚举类,对员工登录状态类型字段进行翻译
EmpsStatus empsStatus=null;
for(int i=0;i<emplist.size();i++){
emp=emplist.get(i);
empsStatus= empsStatus.getEmpEnum(Integer.parseInt(emp.getEmpstsus()));
System.out.println("code"+Integer.parseInt(emp.getEmpstsus()));
System.out.println("msg1"+empsStatus.getMsg());
if(empsStatus!=null) {
emp.setEmpstsus(empsStatus.getMsg());//数据库,查询emp对象后,根据员工状态字段调用getEmpEnum枚举类,方法,得到相应含义,再次赋值给emp对象的相应字段
System.out.println("msg2"+empsStatus.getMsg());
}
}
for (Employee employee : emplist) {
System.out.println(employee);
}
/*openSession.commit();*/
}finally{
openSession.close();
}
}
源码:https://github.com/shuaishuaihand/enumdemo.git