There is no getter for property named 'xxx' in 'class java.lang.String

本文介绍了一种在MyBatis中使用字符串参数时遇到的特定错误及其解决方案。当使用带有字符串参数类型的SQL语句,并包含条件判断时,可能会遇到关于无法获取属性getter的错误。文章提供了详细的错误示例及如何通过调整参数引用方式来解决这一问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

There is no getter for property named 'xxx' in 'class Java.lang.String',此错误之所以出现,是因为mybatis在对parameterType="String"的sql语句做了限制,假如你使用<where test="title != null">这样的条件判断时,就会出现该错误。

一.错误原因

想要追本溯源,就需要错误再现,假设有这样一个sql查询:

<select id="listNoteBooks" parameterType="String" resultMap="notebookResultMap">
        select id,title,creat_time,context from note_book
        <where>
            <if test="title!= null"></if>
            title like CONCAT('%',#{title},'%');
        </where>
</select>

  1. parameterType="String",这一点是必须得,参数类型必须是string。
  2. 该sql对应的mapper class中对应的方法为 List<NoteBook> inquiryNoteBook(String title),也就是说,传递的参数名为title,正常情况下,这样的配置合情合理。
  3.  <if test="title!= null">,有一个对应的test判断语句。
  4. 那么这个时候,项目运行该查询语句时,就会抛出There is no getter for property named 'title' in 'class java.lang.String'错误!

二.解决办法

解决办法很简单,只需要把<if test="title!= null">修改为<if test="_parameter != null">就好了,其他地方不需要改动,修改后的sql语句如下:

<select id="listNoteBooks"  parameterType="String" resultMap="notebookResultMap">
        select id,title,creat_time,context from note_book
        <where>
            <if test="_parameter != null"></if>
            title like CONCAT('%',#{title},'%');
        </where>

</select>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值