您的位置:首页技术文章
文章详情页

解决mybatis case when 报错的问题

浏览:2日期:2023-10-20 08:40:01
在mybatis中使用case when进行条件筛选判断时遇到

Failed to process, please exclude the tableName or statementId.

这样的报错信息,报错的信息是语法错误

但是我在mysql的命令行中运行sql语句是没问题的

//我的case when语句WHERE dept.type = 1AND (CASE agent.dept_typeWHEN 'agent' THEN dept.id=30END)//当agent的dept_type为'agent'时,将添加dept.id = 30的判断

这段sql语句在命令行内运行没问题但是放到mybatis上执行就会报错

//修改后WHERE dept.type = 1AND dept.id=(CASE agent.dept_typeWHEN 'agent' THEN 30END)后来将dept.id放到外面就解决了这个问题

20190718-补充记录 :遇到另一个问题,如果dept这个表是联查来的有可能会没有数据,在dept无数据的时候我们就无法给dept.id赋上啥参数了,并且不可以影响原表数据的查询,我改成了下面这样:

//修改后WHERE dept.type = 1AND (dept.id=(CASE agent.dept_typeWHEN 'agent' THEN 30ELSE 0END) or dept.id is null)

添加dept.id为空的判断即可

(在mysql语句里可以有很多方法解决,但是在mybatis上就会报错 -_-||)

2019-7-30-补充说明:

如果是空字符串不可以使用''要改成单引号’’

CASE WHEN *** THEN ***ELSE '' =>这样也会报错,需要改成=> ELSE’’

补充:Mybatis case when test 注意事项

<choose> <when test='groupBy!=null and groupBy==1'>p_id areaId, </when> <when test='groupBy!=null and groupBy==2'>c_id areaId, </when> <when test='groupBy!=null and groupBy==3'>r_id areaId, </when> </choose>

test 中 用 == 不能用 = ,否则报错。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持好吧啦网。如有错误或未考虑完全的地方,望不吝赐教。

标签: Mybatis 数据库
相关文章: