MyBatis if test judges that the string is equal does not take effect

  When using the MyBatis framework to operate the MySQL database, when it is judged that the value of the incoming string priceFlag is "0", it will be sorted in descending order of the price attribute. The following xml statements are not effective:

<if test="priceFlag != null and priceFlag == '0'">
      ORDER BY price DESC
</if>

  Reason analysis:
  MyBatis uses OGNL expressions for parsing. In OGNL expressions, '0' will be parsed into characters. Because java is strongly typed, char and String are not equal, so the SQL in the if tag will not Be parsed.

  Solution: To
  solve this problem, you can modify the if test judgment statement to any of the following ways:

<if test='"0" == priceFlag'>

  or

<if test='"0".equals(priceFlag)'>

  or

<if test="'0'.toString() == priceFlag">

  After the modification is completed, the SQL statement can be parsed.

Guess you like

Origin blog.csdn.net/piaoranyuji/article/details/108280023