org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'userId' in 'class java.lang.Integer'


最近在使用mybaties查詢的時候報了一個錯:

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'userId' in 'class java.lang.Integer'

 

后來一頓度娘,找到一篇文章:

在使用mybatis時經常碰到這些錯誤:

org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'code' in 'class java.lang.String'; (應該是多個參數的情況下會出現)
nested exception is org.apache.ibatis.binding.BindingException: Parameter 'code' not found. Available parameters are [0, 1, param1, param2];
其實都是接口的參數命名導致mybatis參數綁定出錯引起的。默認情況下mybatis把參數按順序轉化為[0, 1, param1, param2],也就是說#{0} 和 #{param1} 是一樣的,都是取第一個參數,以此類推。

下面經過實驗驗證,首先分兩種情況:

使用if判斷
如果使用了< if test="codee != null and codee !=''"> if判斷,那么接口參數就需要加@Param注解。使用了if判斷,mybatis取值的時候參數就成了[param1, codee],所以你不得不加注解@Param("codee"),這個時候不再有0或1了(Available parameters are [param1, codee]),或者param1… 也可用,比如:

< if test="param1 != null and param1 !=''">
and FAULTPOSITION_CODE like CONCAT('%',CONCAT(#{param1,jdbcType=VARCHAR},'%')) ESCAPE '*'
< /if>

沒有使用if判斷
這個時候也分兩種情況:
(1)接口參數只有一個,不管接口參數名是什么,這個時候#{xxx}沒有限制,可以是0,param1,也可以是aaa,bbb。可以不加注解。
(2)當接口參數大於一個的時候,mybatis的參數集就是上邊說的默認值[0, 1, param1, param2],如果你不用默認值,就需要加上@Param注解起別名。一旦加了注解,mybatis的參數集就和第一種情況一樣了,這個時候也就不再區分是否使用if判斷。

————————————————
版權聲明:本文為CSDN博主「一杯星空一個世界」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/liyaowen505/article/details/84621722


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM