Mybatis条件if test怎么使用枚举值
导读:本文共1864.5字符,通常情况下阅读需要6分钟。同时您也可以点击右侧朗读,来听本文内容。按键盘←(左) →(右) 方向键可以翻页。
摘要: Mybatis条件if test使用枚举值1.正确packagecom.weather.weatherexpert.common.utils;/***<p>Title:</p>*<p>Description:</p>**@Author*@CreateTime*/publicenumCity{XIN... ...
音频解说
目录
(为您整理了一些要点),点击可以直达。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"><!–wrong,java.lang.ClassNotFoundException:Unabletoresolveclass:com.weather.weatherexpert.common.utils.City.XINZHOU–>--><!--<iftest="cityName==@com.weather.weatherexpert.common.utils.City@XINZHOU@getName"><!–wrong,[org.apache.ibatis.ognl.ParseException:Encountered""@""@""atline1,column65.–>--><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>
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怎么使用枚举值的详细内容,希望对您有所帮助,信息来源于网络。