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),直接定義的枚舉類可以正常使用,在類中定義的枚舉類這樣使用會(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靜態(tài)代理和動(dòng)態(tài)代理的實(shí)例和實(shí)現(xiàn)詳解
這篇文章主要給大家介紹了關(guān)于Java靜態(tài)代理和動(dòng)態(tài)代理的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-08-08
Intellj Idea中的maven工程Java文件顏色不對(duì),未被識(shí)別的解決
這篇文章主要介紹了Intellj Idea中的maven工程Java文件顏色不對(duì),未被識(shí)別的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08
基于java查找并打印輸出字符串中字符出現(xiàn)次數(shù)
這篇文章主要介紹了基于java查找并打印輸出字符串中字符出現(xiàn)次數(shù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11
JAVA 實(shí)現(xiàn)延遲隊(duì)列的方法
這篇文章主要介紹了JAVA 實(shí)現(xiàn)延遲隊(duì)列的方法,文中講解非常詳細(xì),供大家參考和學(xué)習(xí),感興趣的朋友可以了解下2020-06-06
Java實(shí)現(xiàn)讀取163郵箱,qq郵箱的郵件內(nèi)容
這篇文章主要利用Java語(yǔ)言實(shí)現(xiàn)讀取163郵箱和qq郵箱的郵件內(nèi)容,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起動(dòng)手試一試2022-02-02
SpringBoot使用WebSocket實(shí)現(xiàn)前后端交互的操作方法
springboot使用websocket有兩種方式,一種是實(shí)現(xiàn)簡(jiǎn)單的websocket,另外一種是實(shí)現(xiàn)STOMP協(xié)議,本篇講述如何使用springboot實(shí)現(xiàn)簡(jiǎn)單的websocket,需要的朋友可以參考下2022-04-04

