Mybatis条件if test怎么使用枚举值(mybatis,开发技术)

时间:2024-04-30 14:48:01 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :

Mybatis条件if test使用枚举值

1.正确

packagecom.weather.weatherexpert.common.utils;/***<p>Title:</p>*<p>Description:</p>**@Author*@CreateTime*/publicenumCity{XINZHOU(100002,"忻州"),DATONG(100003,"大同"),TAIYUAN(100001,"太原");privatefinalIntegercode;privatefinalStringname;City(Integervalue,Stringdesc){this.code=value;this.name=desc;}publicIntegergetCode(){returncode;}publicStringgetName(){returnname;}}

xml:

<!--<iftest="cityName==@com.weather.weatherexpert.common.utils.City.XINZHOU@getName">&lt;!&ndash;wrong,java.lang.ClassNotFoundException:Unabletoresolveclass:com.weather.weatherexpert.common.utils.City.XINZHOU&ndash;&gt;--><!--<iftest="cityName==@com.weather.weatherexpert.common.utils.City@XINZHOU@getName">&lt;!&ndash;wrong,[org.apache.ibatis.ognl.ParseException:Encountered""@""@""atline1,column65.&ndash;&gt;--><iftest="cityName==@com.weather.weatherexpert.common.utils.City@XINZHOU.getName"><!--right--> area_table</if>where1=1<iftest="cityName==@com.weather.weatherexpert.common.utils.City@XINZHOU.getName"><!--right--> andcity_name=#{cityName}</if> 

Mybatis条件if test怎么使用枚举值

2.错误

packagecom.weather.weatherexpert.common.utils;/***<p>Title:</p>*<p>Description:</p>**@Author*@CreateTime*/publicclassCityClass{publicstaticenumCityEnum{XINZHOU(100002,"忻州"),DATONG(100003,"大同"),TAIYUAN(100001,"太原");privatefinalIntegercode;privatefinalStringname;CityEnum(Integervalue,Stringdesc){this.code=value;this.name=desc;}publicIntegergetCode(){returncode;}publicStringgetName(){returnname;}}}

xml:

/*Causedby:org.apache.ibatis.builder.BuilderException:Errorevaluatingexpression'cityName==@com.weather.weatherexpert.common.utils.CityClass@CityEnum.XINZHOU.getName'.Cause:org.apache.ibatis.ognl.OgnlException:CouldnotgetstaticfieldCityEnumfromclasscom.weather.weatherexpert.common.utils.CityClass[java.lang.NoSuchFieldException:CityEnum]*/<iftest="cityName==@com.weather.weatherexpert.common.utils.CityClass@CityEnum.XINZHOU.getName"><!--wrong-->area_table</if> 

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

如下正确:

<iftest="cityName==@com.a.b.c.CityClass$CityEnum@XINZHOU.getName"><!--right-->name=#{username}</if>

Mybatis里使用枚举Enum判断

<iftest="dtEnum==@com.xxx.xxx.TestTypeEnum@HOUR">DATE_FORMAT(TM,'%Y-%m-%d%H')askeyStr,</if>

TestTypeEnum定义如下

  • HOUR("hour"),

  • DAY("day"),

  • MONTH("month"),

  • YEAR("year");

 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:Mybatis条件if test怎么使用枚举值的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:Spring事务失效的场景分析下一篇:

9 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18