Mybatis条件if test如何使用枚举值

Mybatis条件if test使用枚举值

1.正确

package com.weather.weatherexpert.common.utils;
/**
 * 

Title:

*

Description:

* * @Author * @CreateTime */ public enum City { XINZHOU(100002,"忻州"), DATONG(100003,"大同"), TAIYUAN(100001,"太原"); private final Integer code; private final String name; City(Integer value, String desc) { this.code = value; this.name = desc; } public Integer getCode() { return code; } public String getName() { return name; } }

xml:




	area_table

 
where 1=1

	and city_name=#{cityName}
	

2.错误

package com.weather.weatherexpert.common.utils;
/**
 * 

Title:

*

Description:

* * @Author * @CreateTime */ public class CityClass { public static enum CityEnum { XINZHOU(100002, "忻州"), DATONG(100003, "大同"), TAIYUAN(100001, "太原"); private final Integer code; private final String name; CityEnum(Integer value, String desc) { this.code = value; this.name = desc; } public Integer getCode() { return code; } public String getName() { return name; } } }

xml:

/*        Caused by: org.apache.ibatis.builder.BuilderException: Error evaluating expression
        'cityName == @com.weather.weatherexpert.common.utils.CityClass@CityEnum.XINZHOU.getName'. Cause: org.apache.ibatis.ognl.OgnlException:
        Could not get static field CityEnum from class com.weather.weatherexpert.common.utils.CityClass [java.lang.NoSuchFieldException: CityEnum]*/
        
            area_table
        	

可见,直接定义的枚举类可以正常使用,在类中定义的枚举类这样使用会报错,可能方法还没有找到。

如下正确:

 
  name = #{username}
 

Mybatis里使用枚举Enum判断


  DATE_FORMAT(TM,'%Y-%m-%d %H') as keyStr,

TestTypeEnum定义如下

  • HOUR("hour"),
  • DAY("day"),
  • MONTH("month"),
  • YEAR("year");

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

你可能感兴趣的:(Mybatis条件if test如何使用枚举值)