1. 參數(shù)為String時(shí)的插值問(wèn)題
假設(shè)有下面一Dao接口方法
1
|
public Account findByAccountType (String type) throws DaoException; |
對(duì)應(yīng)的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> |
一般我們都是按這樣的方式來(lái)寫的,對(duì)于其他類型是沒(méi)錯(cuò)的,但是如果為String的話會(huì)拋下面的異常:
There is no getter for property named 'type ' in 'class java.lang.String'
因?yàn)?a href="/article/71358.html">MyBatis要求如果參數(shù)為String的話,不管接口方法的形參是什么,在Mapper.xml中引用時(shí)需要改變?yōu)開parameter才能識(shí)別 :
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. 對(duì)字符串參數(shù)進(jìn)行是否相等 比較時(shí)的問(wèn)題
錯(cuò)誤:
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 > |
注:上述問(wèn)題不僅限于<if>標(biāo)簽,其他動(dòng)態(tài)sql標(biāo)簽在對(duì)String進(jìn)行處理時(shí)也會(huì)出現(xiàn)同樣的問(wèn)題。
以上所述是小編給大家介紹的MyBatis 參數(shù)類型為String時(shí)常見問(wèn)題及解決方法,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)服務(wù)器之家網(wǎng)站的支持!
原文鏈接:http://blog.csdn.net/rain_git/article/details/68067509