Mybatis條件if test如何使用枚舉值
Mybatis條件if test使用枚舉值
1.正確
package com.weather.weatherexpert.common.utils; /** * <p>Title: </p> * <p>Description: </p> * * @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:
<!--<if test="cityName == @com.weather.weatherexpert.common.utils.City.XINZHOU@getName"><!–wrong,java.lang.ClassNotFoundException: Unable to resolve class: com.weather.weatherexpert.common.utils.City.XINZHOU–>--> <!--<if test="cityName == @com.weather.weatherexpert.common.utils.City@XINZHOU@getName"><!–wrong,[org.apache.ibatis.ognl.ParseException: Encountered " "@" "@ "" at line 1, column 65.–>--> <if test="cityName == @com.weather.weatherexpert.common.utils.City@XINZHOU.getName"><!--right--> area_table </if> where 1=1 <if test="cityName == @com.weather.weatherexpert.common.utils.City@XINZHOU.getName"><!--right--> and city_name=#{cityName} </if>
2.錯(cuò)誤
package com.weather.weatherexpert.common.utils; /** * <p>Title: </p> * <p>Description: </p> * * @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]*/ <if test="cityName == @com.weather.weatherexpert.common.utils.CityClass@CityEnum.XINZHOU.getName"><!--wrong--> area_table </if>
可見(jiàn),直接定義的枚舉類(lèi)可以正常使用,在類(lèi)中定義的枚舉類(lèi)這樣使用會(huì)報(bào)錯(cuò),可能方法還沒(méi)有找到。
如下正確:
<if test="cityName == @com.a.b.c.CityClass$CityEnum@XINZHOU.getName"><!--right--> name = #{username} </if>
Mybatis里使用枚舉Enum判斷
<if test="dtEnum == @com.xxx.xxx.TestTypeEnum@HOUR"> ? DATE_FORMAT(TM,'%Y-%m-%d %H') as keyStr, </if>
TestTypeEnum定義如下
HOUR
("hour"),DAY
("day"),MONTH
("month"),YEAR
("year");
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java中equals()方法重寫(xiě)實(shí)現(xiàn)代碼
這篇文章主要介紹了Java中equals()方法重寫(xiě)實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2017-05-05Mybatis注解開(kāi)發(fā)單表、多表操作的實(shí)現(xiàn)代碼
這篇文章主要介紹了Mybatis高級(jí):Mybatis注解開(kāi)發(fā)單表操作,Mybatis注解開(kāi)發(fā)多表操作,構(gòu)建sql語(yǔ)句,綜合案例學(xué)生管理系統(tǒng)使用接口注解方式優(yōu)化,需要的朋友可以參考下2021-02-02詳解Mybatis核心類(lèi)SqlSessionFactory的構(gòu)建
這篇文章主要為大家詳細(xì)介紹了Mybatis核心類(lèi)SqlSessionFactory的構(gòu)建過(guò)程,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-12-12java中的Io(input與output)操作總結(jié)(一)
所謂IO,也就是Input與Output的縮寫(xiě)。在java中,IO涉及的范圍比較大,這里主要討論針對(duì)文件內(nèi)容的讀寫(xiě),感興趣的朋友可以了解下2013-01-01Java面向?qū)ο蟪绦蛟O(shè)計(jì):類(lèi)的定義,靜態(tài)變量,成員變量,構(gòu)造函數(shù),封裝與私有,this概念與用法詳解
這篇文章主要介紹了Java面向?qū)ο箢?lèi)的定義,靜態(tài)變量,成員變量,構(gòu)造函數(shù),封裝與私有,this概念與用法,較為詳細(xì)的分析了Java類(lèi)的定義,靜態(tài)變量,成員變量,構(gòu)造函數(shù),封裝,私有等相關(guān)原理、用法及操作注意事項(xiàng),需要的朋友可以參考下2020-04-04springboot集成ftp實(shí)現(xiàn)文件上傳
這篇文章主要為大家詳細(xì)介紹了springboot集成ftp實(shí)現(xiàn)文件上傳,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05JAVA 自定義線程池的最大線程數(shù)設(shè)置方法
這篇文章主要介紹了JAVA 自定義線程池的最大線程數(shù)設(shè)置方法,文中示例代碼非常詳細(xì),供大家參考和學(xué)習(xí),感興趣的朋友可以了解下2020-06-06各種格式的編碼解碼工具類(lèi)分享(hex解碼 base64編碼)
這篇文章主要介紹了各種格式的編碼解碼工具類(lèi),集成Commons-Codec、Commons-Lang及JDK提供的編解碼方法2014-01-01Java?SpringBoot?獲取接口實(shí)現(xiàn)類(lèi)匯總
這篇文章主要介紹了Java?SpringBoot?獲取接口實(shí)現(xiàn)類(lèi)匯總,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09