Mybatis if tag judges whether two values are equal

Use scenarios compared with "==":

No matter what type of variable you use, as long as the value of the variable is a character type, use "=="

cause:

In mybatis, if the <if> tag uses a "=" to determine whether the values ​​on both sides are equal, then mybatis will automatically unbox the values ​​on both sides of the "=" to the basic data type (Number type)

Examples are as follows:

Parameters: String queryKwd = "a";

<select id = "getFactoryInfoList" resultMap = "BaseResultMap"> 
   select * from PM_FACTORY_INFO 
   where DEL_FLAG = '0' <if test = "queryKwd == 'a'"> <!-correct writing-> 
           and 1 = 1 < / if> 
       <if test = "queryKwd = 'a'"> <!-wrong way of writing: The character type can not be judged whether it is equal with a "=", it will report an error: Caused by: java.lang.NumberFormatException: For input string: "a"-> 
           and 2 = 2 </ if> </ select> <-java framework platform www.1b23.com->

2. Use "=" to compare values ​​of basic data types

Use scenarios compared with "=":

1. No matter what type of variable you use, as long as the value of the variable is the basic numeric type, use "="

2. When the parameter type is Object, when the value of Object is a single uppercase or lowercase letter or some special character strings will be converted to ASCII code, then use "="

cause:

1. In mybatis, if the <if> tag uses a "=" to determine whether the values ​​on the left and right sides are equal, then mybatis will automatically unbox the values ​​on both sides of the "=" to the basic data type (Number type)

2. When using Object as a parameter variable, and the value is the following single uppercase and lowercase letters or strings, it will be converted to the corresponding decimal number (ASCII code table only intercepts part, you can go online to find more)

Examples are as follows:

Parameters: String queryKwd = "1";

<select id = "getFactoryInfoList" resultMap = "BaseResultMap"> 
   select * from PM_FACTORY_INFO 
   where DEL_FLAG = '0' <if test = "queryKwd == '1'"> <!-Wrong way of writing, although no error is reported, but never Will be established-> 
           and 1 = 1 </ if> 
       <if test = "queryKwd = '1'"> <!-Correct writing-> 
           and 2 = 2 </ if> </ select> <-java framework Platform www.1b23.com->

 3. Important reminder

在使用mybatis时,需要特别注意,当使用的参数不管是String/Object/int等类型的变量,我们需要清楚业务变量的值会是字符型还是数值型

只要变量值是字符型就用“==”!!!

只要变量值是数值型就用“=”!!!

只要变量值是单个大写或小写字母就用“=”!!!


Guess you like

Origin blog.51cto.com/14622073/2486849