1.枚举类优雅写法
@AllArgsConstructor
@Getter
public enum TypeEnum {
DL( "0", "类别1"),
ZT( "1", "类别2");
private final String type;
private final String description;
private static Map<String, TypeEnum > cache;
static {
cache = Arrays.stream(TypeEnum .values()).collect(Collectors.toMap(TypeEnum ::getType, Function.identity()));
}
public static TypeEnum of(String type) {
return cache.get(type);
}
/**
* 获取枚举类List<Enum>集合
* @return java.util.List<java.lang.Enum>
*/
public static List<Enum> getEnumList(){
// 获取枚举类型的所有值
Class<? extends Enum> enumClass = TypeEnum .class;
Enum[] enumValues = enumClass.getEnumConstants();
// 将枚举值添加到List中
List<Enum> enumList = new ArrayList<>();
Collections.addAll(enumList, enumValues);
return enumList;
}
}
当然getEnumList方法和TypeEnum.values()有异曲同工之妙,此处仁者见仁智者见智。
2.请求入参判断是否为枚举类类型
public static void main(String[] args) {
//检查select类型是否正确
String selectType = selTabReqVo.getSelectType();
if (Arrays.stream(QueryTypeEnum.values()).noneMatch(logistics -> logistics.getType().equals(selectType))) {
throw new IllegalArgumentException("请提供正确的selectType类型!");
}
}
3.根据参数类型获取对应的枚举类
public QueryTypeEnumisHotRoom() {
return QueryTypeEnum.of(this.hotFlag);
}
4、SQL条件判断是否为枚举类类型
<select id="getChudanNo" resultType="org.entity.vo.resp.ChudanRespVo">
SELECT distrJobno,
chudanNo
FROM op WITH(nolock)
<where>
<if test="distrJobno !=null and distrJobno!='' ">
distrJobno = #{distrJobno}
</if>
<include refid="dl"/>
</where>
</select>
<sql id="dl">
<if test="selectType!=null and selectType.trim()!='' and @org.enums.QueryTypeEnum@DL.getType == selectType">
AND (policyState = 0 OR policyState = 1 OR policyState = 2 )
</if>
</sql>
重点:@org.enums.QueryTypeEnum@DL.getType == selectType