解決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 中 用 == 不能用 = ,否則報錯。
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持好吧啦網。如有錯誤或未考慮完全的地方,望不吝賜教。
相關文章:
