Mybatis条件if test如何使用枚举值

网友投稿 368 2022-07-26


目录Mybatis条件if test使用枚举值1.正确2.错误Mybatis里使用枚举Enum判断TestTypeEnum定义如下

Mybatis条件if test使用枚举值

1.正确

package com.weather.weatherexpert.common.utils;

/**

*

Title:

*

Description:

*

* @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:

area_table

where 1=1

and city_name=#{cityName}

2.错误

package com.weather.weatherexpert.common.utils;

/**

*

Title:

*

Description:

*

* @Author

* @CreateTime

*/

public class CityClass {

public static enum CityEnum {

XINZHOU(100002, "忻州"),

DATONG(100003, "大同"),

TAIYUAN(100001, "太原");

phttp://rivate 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]*/

area_table

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

如下正确:

name = #{username}

Mybatis里使用枚举Enum判断

DATE_FORMAT(TM,'%Y-%m-%d %H') as keyStr,

TestTypeEnum定义如下

HOUR("hour"),DAY("day"),MONTH("month"),YEAR("year");


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:java实现飞机大战小游戏
下一篇:MyBatis多对多关联映射创建示例
相关文章

 发表评论

暂时没有评论,来抢沙发吧~