There is no getter for property named 'id' in 'class java.lang.Integer
問題描述:
使用mybatis傳入參數, 當參數類型是String ,Integer 等這些時。如果用他的<if test="id != null and id != ''">
標簽判斷該參數是否為空,通常會爆There is no getter for property named ‘id’ in ‘class java.lang.Integer異常。
解決思路:
- 在接口中該方法的參數前加上@Param(“參數名”)注解。
例如:
List<String> query(@Param(value="id") Integer id);
- 我百度的時候,發現還有人這樣解決,不知是否可行,一並摘錄下來:
在 xml文件中應該使用_parameter來代替參數名。
例如:
<select id="query" resultMap="BaseResultMap" parameterType="java.lang.Integer" > select <include refid="Base_Column_List" /> from t_user where 1=1 <if test="_parameter!= null" > and id = #{_parameter} </if> </select>
總結:
其實這個問題並不是很大,但是容易一不小心就誤犯;所以一定要小心謹慎。