Common problems and solutions when MyBatis parameter type is String

1. Interpolation problem when the parameter is String
Suppose there is the following Dao interface method
public Account findByAccountType (String type) throws DaoException;
corresponding Mapper.xml
<select id="findByAccountType " parameterType="string" resultType="account">
  select *
  form account
  <where>
    <if test ="type != null">
      type=#{type}
    </if>
  </where>
</select>
generally we write in this way, for other types Yes, but if it is String, the following exception will be thrown:
There is no getter for property named 'type ' in 'class java.lang.String'
because MyBatis requires that if the parameter is String, regardless of the formal parameters of the interface method What is it, it needs to be changed to _parameter when referenced in Mapper.xml to recognize:
<select id="findByAccountType " parameterType="string" resultType="account">
  select *
  form account
  <where>
    <if test ="_parameter!= null">
      type=#{_parameter}
    </if>
  </where>
</select>
2. Problems when comparing string parameters for equality
Error :

< if test="_parameter == '1' ">
  type=#{_parameter}
</if>
Correct:

<if test='_parameter == "1" '>
  type=#{_parameter}
</if>
<if test ="_parameter == '1'.toString() ">
  type=#{_parameter}
</if>
Note: The above problem is not limited to the <if> tag, other dynamic sql tags will also appear the same when processing String question.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326223194&siteId=291194637