Mybatis条件if test如何使用枚举值
作者:二十六画生的博客 发布时间:2023-11-19 14:15:33
标签: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.错误
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


猜你喜欢
- 十年前,Java 还是计算机科学的入门课程中的必学语言。如果你想学其他语言,比如 C、Python、PHP,你就得专门选那个语言的课程,或者
- 1.使用的注意事项本节给大家带来基础UI控件部分的最后一个控件:DrawerLayout,官方给我们提供的一个侧滑菜单控件,和上一节的Vie
- 前天在做批量数据导入新增时,要对数据进行有效性判断,其中还要去除重复,如果没出现linq的话可能会新声明一个临时对象集合,然后遍历原始数据判
- 本文实例为大家分享了Android实现扫描和生成二维码的具体代码,供大家参考,具体内容如下目标效果:该例子可以扫描二维码和条形码,扫描后会将
- JDK12的五大重要新特性Java12在March 19, 2019发布了。在2017年发布Java 9之后,Java平台发布节奏已从每3年
- 1、谷歌浏览器配置管理在代理服务器中,按上图进行设置,可以把localhost换成 127.0.0.1 ,端口换成你想设置的,但是不要与别的
- Android开发笔记:关于SeekBar在刷新使用中的一些问题问题今天在用Navigation 在两个Fragment之间导航时发现了从第
- java如何实现ftp上传?如何创建文件夹?最佳答案:准备条件:java实现ftp上传用到了commons-net-3.3.jar包首先建立
- Java.util.Collections类下有一个静态的shuffle()方法,如下:1)static void shuffle(List
- 哈希 Hash 算法介绍哈希算法也叫散列算法, 不过英文单词都是 Hash, 简单一句话概括, 就是可以把任意长度的输入信息通过算法变换成固
- 一、前言:垃圾回收:在未来的JDK中可能G1会为ZGC所取代先问自己几个问题:什么是垃圾?垃圾就是堆内存中(范指)没有任何指针指向的对象实体
- public class PullToLoadListView extends ListView implements OnScrollLi
- 群主发普通红包,某群有多名成员,群主给成员发普通红包,普通红包的规则:群主的一笔金额,从群主余额中扣除,平均分成n等份,让成员领取;成员领取
- BeanPostProcessor接口作用:如果我们想在Spring容器中完成bean实例化、配置以及其他初始化方法前后要添加一些自己逻辑处
- 先从本地把图片上传到服务器,然后根据URL把头像处理成圆形头像。因为上传图片用到bmob的平台,所以要到bmob(http://www.bm
- 多对多表之间关系表models.py文件代码from django.db import models# Create your models
- 一、简介相信大家用eclipse上的模拟器会觉得很慢很卡,这里给大家介绍个好东西安卓模拟器genymotion。了解更多,可到此网站http
- 之前使用Retrofit都是将JSON串转化为POJO对象,针对不同的业务协议,定义相应的接口和参数列表。但是此种方式一般用在自己内部协议基
- 1.Quartz是什么?Quartz是一个开源的Java调度框架,可以用来实现在指定的时间或时间间隔触发任务执行的功能。它支持多种方式的作业
- 实现需求:两个线程交替打印1,0,打印10轮java多线程口诀:高内聚,低耦合线程操作资源类判断干活通知防止虚假唤醒方式一:使用synchr