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>
parameterType="String"
,这一点是必须得,参数类型必须是string。- 该sql对应的mapper class中对应的方法为 List<NoteBook> inquiryNoteBook(String title)
,也就是说,传递的参数名为title,正常情况下,这样的配置合情合理。
<if test="title!= null">,有一个对应的test判断语句。
- 那么这个时候,项目运行该查询语句时,就会抛出
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>