解决MyBatis的报错 There is no getter for property named ‘*‘ in ‘class java.lang.String‘

2022-09-02 16:28:17 浏览数 (1)

ERROR信息:

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

遇到此问题时,首先网上查询得到以下两篇文章,找到了解决办法:

解决MyBatis的报错 There is no getter for property named ‘*‘ in ‘class java.lang.String‘_吾欲乘风归去,又恐琼楼玉宇的博客-CSDN博客报错样式:核心问题就是这一句:

https://blog.csdn.net/NeiHan2020/article/details/117556666源码分析 There is no getter for property named '*' in 'class java.lang.String_沉默王二的博客-CSDN博客There is no getter for property named '*' in 'class java.lang.String',此错误之所以出现,是因为mybatis在对parameterType="String"的sql语句做了限制,假如你使用这样的条件判断时,就会出现该错误,不过今天我们来刨根问底一下。

https://blog.csdn.net/qing_gee/article/details/47122227

经过自己动手尝试,下面两种写法不会出错:

1.没有判断是否为空的语句:

代码语言:javascript复制
<select id="getListByName" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List" />
        FROM myTable
        WHERE name= #{name}
</select>


<select id="getListByName" resultMap="BaseResultMap" parameterType="String">
        SELECT <include refid="Base_Column_List" />
        FROM myTable
        WHERE name= #{name}
</select>

2.有判断是否为空的语句:

代码语言:javascript复制
<select id="getListByName" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List" />
        FROM myTable
        <where>
            <if test="null != _parameter and '' != _parameter">
                name= #{name}
            </if>
        </where>
</select>



<select id="getListByName" resultMap="BaseResultMap" parameterType="String">
        SELECT <include refid="Base_Column_List" />
        FROM myTable
        <where>
            <if test="null != _parameter and '' != _parameter">
                name= #{name}
            </if>
        </where>
</select>

得出结论:

当查询SQL的传入参数为字符串String类型时,如果写了空值判断条件,需要在if判断条件里用_parameter替换参数名。

0 人点赞