我們都知道mybatis在進行參數判斷的時候,直接可以用<if test=""></if> 就可以了,如下:
1、常規代碼
1
2
3
4
5
6
7
8
9
10
11
12
|
<update id= "update" parameterType= "com.cq2022.zago.order.entity.Test" > update t_test_l <set > < if test= "trnsctWayId != null" > trnsct_way_id = #{trnsctWayId,jdbcType=TINYINT}, </ if > < if test= "langId != null" > lang_id = #{langId,jdbcType=INTEGER}, </ if > </set> where trnsct_way_l_id = #{trnsctWayLId,jdbcType=INTEGER} </update> |
但是單個參數和多參數的判斷有個不同點,當我們的入參為entity
實體,或者map
的時候,使用if
參數判斷沒任何問題。
但是當我們的入參為java.lang.Integer
或者 java.lang.String
的時候,這時候就需要注意一些事情了
具體代碼如下(咱們看著代碼說,先展示錯誤代碼):
2、錯誤代碼
1
2
3
4
5
6
7
8
|
<select id= "getTrnsctListByLangId" parameterType= "java.lang.Integer" resultType= "java.lang.Integer" > select trnsct_id from t_trnsct_way_l where < if test= "langId != null" > and lang_id = #{langId} </ if > </select> |
上述代碼存在一些問題,首先入參是java.lang.Integer, 而不是map或者實體的入參方式,對于這類單個入參然后用if判斷的,mybatis有自己的內置對象,如果你在if判斷里面 寫的是你的入參的對象名,那就報異常:Internal error : nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'langId' in 'class java.lang.Integer'
3、正確代碼:
這里就涉及到mybatis
的內置對象_parameter
,單個參數判斷的時候,就不像1、 2那樣直接用參數對象名判斷了。還有就是數據類型最好加上
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對服務器之家的支持。如果你想了解更多相關內容請查看下面相關鏈接
原文鏈接:https://blog.csdn.net/moneyshi/article/details/45914467