业务背景:
在我们业务场景中有时就会出现在mybatis的xml文件中使用枚举值,是因为有些值不想写死想集中统一管理。
枚举类:
package com.test.constant;
public enum AnimalEnum {
DOG("dog"),
CAT("cat"),
private String type;
AnimalEnum(String type) {
this.type= type;
}
public String type() {
return this.type;
}
public static boolean isDog(String type) {
return DOG.type().equals(type);
}
}
文件中使用:
在mapper文件中,想判断一个参数的值是否跟 的值是否一致
<if test="@@isDog(type)" >
AND p.animal_type=#{type}
</if>
其中:
- @后是枚举类的文件路径,第二个@后是枚举方法(其中是传递参数)type是传过来的参数
- 如果想直接获取枚举的可以这样:
@@
以下是工作中用到的:
select
distinct d.dept_id as deptId,
d.dept_name as deptName
from
sys_dept d
where
d.dept_type = '${@@()}'
order by d.dept_id asc