Mybatis条件if test如何使用枚举值
作者:二十六画生的博客 时间:2023-11-19 14:15:33
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.错误
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>
可见,直接定义的枚举类可以正常使用,在类中定义的枚举类这样使用会报错,可能方法还没有找到。
如下正确:
<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");
来源:https://programskills.blog.csdn.net/article/details/86490787
标签:Mybatis,条件,if,test,枚举值
0
投稿
猜你喜欢
Android自定义view实现电影票在线选座功能
2022-04-26 22:26:59
Maven安装及MyEclipse中使用Maven
2023-06-20 04:29:07
常见Android选项菜单样式集合
2021-10-12 04:42:39
C#+MO实现一个道路编辑软件(刚开始)
2023-03-28 08:52:05
Android RecyclerView缓存复用原理解析
2022-04-29 10:51:03
android 控件同时监听单击和双击实例
2022-11-16 15:45:33
SpringBoot中使用Redis Stream实现消息监听示例
2021-09-02 04:42:07
Java使用自定义注解实现为事件源绑定事件监听器操作示例
2023-06-21 01:17:55
C#编程自学之开篇介绍
2023-12-16 07:58:55
Java实现简单无界面五子棋
2022-07-10 12:06:02
mybatis教程之resultmap_动力节点Java学院整理
2022-09-05 22:38:37
使用注解解决ShardingJdbc不支持复杂SQL方法
2022-08-03 02:15:12
Android串口通信封装之OkUSB的示例代码
2023-08-22 21:17:29
C#数据结构之双向链表(DbLinkList)实例详解
2023-08-23 08:56:44
java利用jacob将word转pdf
2023-02-05 08:23:03
Kotlin基础教程之控制流(顺序,分支,循环)
2022-03-15 11:37:33
Java concurrency集合之LinkedBlockingDeque_动力节点Java学院整理
2022-12-06 12:28:38
Springboot2.1.6集成activiti7出现登录验证的实现
2022-08-03 10:44:48
C#二维数组与多维数组的具体使用
2023-07-30 23:53:49
Spring bean的实例化和IOC依赖注入详解
2023-11-23 23:57:15