事先作成一 enum,如下:

public enum CityCode {

ALL("000"),
BEIJING("010"),
SHANGHAI("021"),

private String code;

CityCode(String code) {
this.code = code;
}

public String val() {
return this.code;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
在mapper文件中,想判断一个参数的值是否跟 CityCode.ALL 的值一致的时候,我想当然的像下面这么写了:

<if test="area != com.test.constant.CityCode.ALL.val" >
AND
p.`city`=#{area}
</if>
1
2
3
4
我为什么会这么写?

因为之前有在 mapper 文件中判断过 List 对象的size 是不是为 0的,就是下面这么写的:

<if test="statusList.size > 0" >
// to do sth.
</if>con
1
2
3
但是,结果是行,mybatis 报了下面的错:

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter 'com' not found. Available parameters are [area, param1, param2, now]
at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:79) ~[mybatis-spring-1.3.0.jar:1.3.0]
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:447) ~[mybatis-spring-1.3.0.jar:1.3.0]
at com.sun.proxy.$Proxy22.selectList(Unknown Source) ~[?:?]
at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:231) ~[mybatis-spring-1.3.0.jar:1.3.0]
at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:128) ~[mybatis-3.4.0.jar:3.4.0]
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:68) ~[mybatis-3.4.0.jar:3.4.0]
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:53) ~[mybatis-3.4.0.jar:3.4.0]
at com.sun.proxy.$Proxy38.performance(Unknown Source) ~[?:?]
1
2
3
4
5
6
7
8
9
后来,又想起来,之前有过在 mybatis 中访问静态方法的示例,试了一下,果然可以,enum 修改如下:

public enum CityCode {

ALL("000"),
BEIJING("010"),
SHANGHAI("021"),

private String code;

CityCode(String code) {
this.code = code;
}

public String val() {
return this.code;
}

public static boolean isAll(String code) {
return ALL.val().equals(code);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
然后,mapper 文件修改如下:

<if test="!@com.test.constant.CityCode@isAll(area)" >
AND
p.`city`=#{area}
</if>
1
2
3
4
请注意一下语法:
@xxxxClass@method(parameter)
我上面那个test部分的第一个 ! 是用来对结果取反的,它不属于语法的一部分。当然,你可以不用 ! 改用 not 也一样的。

最新文章

  1. js常用关键字和函数
  2. 使用SDWebImage下载图片,sharedDownloader方法下载成功,new 方法下载失败
  3. windows防火墙命令详解
  4. Educational Codeforces Round 16 B
  5. Codeforces Round #303 (Div. 2) C dp 贪心
  6. Install wxWidgets-3.0.2 on GNU/Linux Debian
  7. Codevs No.2144 砝码称重2
  8. asp.net 点击按钮,页面没有任何变化,后台代码不触发
  9. Rainmeter 雨滴桌面 主题分享
  10. 第二章实例:ActionBar结合Fragment显示
  11. oracle 关于对时间操作的汇总
  12. 控制结构(11): Continuation passing style(CPS)
  13. Maven学习 八 采用Maven搭建SSM环境
  14. 五分钟轻松了解Hbase面向列的存储
  15. PAT A1102 Invert a Binary Tree (25 分)——静态树,层序遍历,先序遍历,后序遍历
  16. ${pageContext.request.contextPath}无法解析
  17. eclipse卡,相关优化配置
  18. js的event.preventDefault()与event.stopPropagation()
  19. Odoo9发行说明
  20. 20155327 java第四周学习笔记

热门文章

  1. PHP 动态输出 svgz 格式图片
  2. jquery 回车键 调用tab 事件
  3. LeetCode 257. Binary Tree Paths(二叉树根到叶子的全部路径)
  4. powershell 开windows 端口
  5. Nginx打印json日志
  6. 数据结构各种算法实现(C++模板)
  7. 【翻译】Flink Table Api &amp; SQL — 流概念
  8. vue-cli入门 - 搭建项目打包运行+webpack打包
  9. 使用termux在安卓手机上搭建python开发环境
  10. Error: Error occured while starting App. Original error: Activity used to start app doesn&#39;t exist or cannot be launched! Make sure it exists and is a launchable activity