1. 參數為String時的插值問題
假設有下面一Dao接口方法
1
|
public Account findByAccountType (String type) throws DaoException; |
對應的Mapper.xml
1
2
3
4
5
6
7
8
9
|
<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要求如果參數為String的話,不管接口方法的形參是什么,在Mapper.xml中引用時需要改變為_parameter才能識別 :
1
2
3
4
5
6
7
8
9
|
<select id= "findByAccountType " parameterType= "string" resultType= "account" > select * form account <where> < if test = "_parameter!= null" > type=#{_parameter} </ if > </where> </select> |
2. 對字符串參數進行是否相等 比較時的問題
錯誤:
1
2
3
|
< if test= "_parameter == '1' " > type=#{_parameter} </ if > |
正確:
1
2
3
4
5
6
|
< if test= '_parameter == "1" ' > type=#{_parameter} </ if > < if test= "_parameter == '1'.toString() " > type=#{_parameter} </ if > |
注:上述問題不僅限于<if>標簽,其他動態sql標簽在對String進行處理時也會出現同樣的問題。
以上所述是小編給大家介紹的MyBatis 參數類型為String時常見問題及解決方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:http://blog.csdn.net/rain_git/article/details/68067509