亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Mybatis條件if test如何使用枚舉值

 更新時(shí)間:2022年06月06日 16:38:48   作者:二十六畫(huà)生的博客  
這篇文章主要介紹了Mybatis條件if test如何使用枚舉值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

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">&lt;!&ndash;wrong,java.lang.ClassNotFoundException: Unable to resolve class: com.weather.weatherexpert.common.utils.City.XINZHOU&ndash;&gt;-->
<!--<if test="cityName == @com.weather.weatherexpert.common.utils.City@XINZHOU@getName">&lt;!&ndash;wrong,[org.apache.ibatis.ognl.ParseException: Encountered " "@" "@ "" at line 1, column 65.&ndash;&gt;-->
<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)文章

最新評(píng)論