溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

當MyBatis 參數(shù)類型為String時如何解決

發(fā)布時間:2020-11-12 16:25:13 來源:億速云 閱讀:375 作者:Leah 欄目:編程語言

當MyBatis 參數(shù)類型為String時如何解決?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。

1. 參數(shù)為String時的插值問題

假設有下面一Dao接口方法

public Account findByAccountType (String type)throws DaoException;

對應的Mapper.xml

<select id="findByAccountType " parameterType="string" resultType="account">
  select *
  form account
  <where>
    <if test ="type != null">
      type=#{type}
    </if>
  </where>
</select>

一般我們都是按這樣的方式來寫的,對于其他類型是沒錯的,但是如果為String的話會拋下面的異常:

There is no getter for property named 'type ' in 'class java.lang.String'

因為MyBatis要求如果參數(shù)為String的話,不管接口方法的形參是什么,在Mapper.xml中引用時需要改變?yōu)開parameter才能識別 :

<select id="findByAccountType " parameterType="string" resultType="account">
  select *
  form account
  <where>
    <if test ="_parameter!= null">
      type=#{_parameter}
    </if>
  </where>
</select>

2. 對字符串參數(shù)進行是否相等 比較時的問題

錯誤:

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

正確:

<if test='_parameter == "1" '>
  type=#{_parameter}
</if>
<if test="_parameter == '1'.toString() ">
  type=#{_parameter}
</if>

注:上述問題不僅限于<if>標簽,其他動態(tài)sql標簽在對String進行處理時也會出現(xiàn)同樣的問題。

看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業(yè)資訊頻道,感謝您對億速云的支持。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。

AI