Solve the problem of mybatis case when error reporting

  • 2021-08-12 02:54:50
  • OfStack

Encountered when using case when in mybatis for conditional screening judgment

Failed to process, please exclude the tableName or statementId.

Such an error message, the error message is a grammatical error

But I have no problem running the sql statement on the command line of mysql


// Mine case when Statement 
WHERE dept.type = 1
AND 
(
CASE agent.dept_type
WHEN "agent" THEN dept.id=30
END
)
// When agent Adj. dept_type For "agent" Is added when the dept.id = 30 Judgment of 

This sql statement is fine to run on the command line, but it will report an error when executed on mybatis


// After modification 
WHERE dept.type = 1
AND dept.id=
(
CASE agent.dept_type
WHEN "agent" THEN 30
END
)

Later, putting dept. id outside solved this problem

20190718-Supplementary record: I encountered another problem. If the table dept is from associated query, there may be no data. When there is no data in dept, we can't assign any parameters to dept. id, and it can't affect the query of the original table data. I changed it to the following:


// After modification 
WHERE dept.type = 1
AND (dept.id=
(
CASE agent.dept_type
WHEN "agent" THEN 30
ELSE 0
END
) or dept.id is null)

Add the judgment that dept. id is empty

(There are many ways to solve this in the mysql statement, but an error will be reported on mybatis-_-)

2019-7-30-Supplementary notes:

If it is an empty string, you can't use ''to change it to single quotation marks''


CASE WHEN *** THEN ***
ELSE "" => This will also report errors , Need to be changed to => ELSE''

Supplement: Mybatis case when test Precautions


<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>

In test, = = cannot be used, otherwise an error will be reported.


Related articles: