Mybatis There is no getter for property named 'XXX' in 'class java.lang.XXX
程序员文章站
2022-07-15 13:50:42
...
Mybatis查询
使用Mybatis查询时,其参数可以是基本数据类型或者像Integer和String这样的简单的数据对象,也可以是复杂对象(一般是指JavaBean)或者map等,当使用基本数据类型的参数时,若这个参数的使用放在了判断条件中,如下查询语句:
<select id="getNameList" parameterType="String" resultType="String">
<![CDATA[
SELECT DISTINCT NAME
FROM T_PERSION
WHERE 1=1
]]>
<if test="age!=null">
AND AGE = #{age}
</if>
</select>
则可能会出现:
org.apache.ibatis.reflection.ReflectionException:There is no getter for property named 'XXX' in 'class java.lang.XXX
的异常,问题的直接原因是由于上面的parameterType是基本的简单类型而后面有判断的if条件,若没有后面的判断条件,而是:
<select id="getNameList" parameterType="String" resultType="String">
<![CDATA[
SELECT DISTINCT NAME
FROM T_PERSION
WHERE 1=1
AND AGE = #{age}
]]>
</select>
则没有任何异常。
对上面的异常,网上说问题原因是Mybatis默认采用ONGL解析参数,所以会自动采用对象树的形式获取传入的变量值,解决方法有两个:
1.将参数名(上面的例子为‘age’)替换为"_parameter" ,即:
<select id="getNameList" parameterType="String" resultType="String">
<![CDATA[
SELECT DISTINCT NAME
FROM T_PERSION
WHERE 1=1
]]>
<if test="_parameter!=null">
AND AGE = #{_parameter}
</if>
</select>
2,在接口中定义方法时 增加“@Param("参数名")” 标记,例如:
List<String> getNameList(@Param("age")String age);
上一篇: PAT乙级练习1002
推荐阅读
-
There is no getter for property named ‘name‘ in ‘class java.lang.String‘
-
There is no getter for property named ‘name‘ in ‘class java.lang.String
-
Mybatis报错:There is no getter for property named ‘xxxx‘ in ‘class xxxx
-
Mybatis报错:There is no getter for property named 'xxxx' in 'class xxxx
-
Mybatis报错:There is no getter for property named ‘xxxx‘ in ‘class xxxx
-
Mybatis报错:There is no getter for property named ‘xxxx‘ in ‘class xxxx
-
There is no getter for property named 'xxx' in 'class java.lang.xxxx'解决办法
-
Mybatis There is no getter for property named 'xxxx' in 'class java.lang.String'
-
There is no getter for property named 'xxx' in 'class com.ams.ams.entity.xxx'
-
Mybatis There is no getter for property named 'XXX' in 'class java.lang.XXX