当我们的参数类型为String或者为java.lang.String的时候,Mybatis会抛出异常,There is no getter for property named 'XXX' in 'class java.lang.String'。
写法大致如下:
<select id="xxx" resultType="String" parameter="String">
select xxx
from xxx
where xxx=#{xxx}
</select>
原因是Mybatis默认采用OGNL解析参数,所以会自动采用对象树的形式取 string.xxx 值,如果没在在方法中定义,则会抛异常报错。
解决办法1:把#{xxx}修改为 #{_parameter} 即可
<select id="xxx" resultType="String" parameter="String">
select xxx
from xxx
where xxx=#{_parameter}
</select>
解决办法2:在各个方法中提前定义
public String selectNameById(@Param(value="xxx") String xxx ){
}
如果以上方法全部失效,还是报错。
注意点!因为各个Mybatis版本不相同,在需要写dao层实现的版本中,以上方法不适用!
粗暴解决办法:使用Map进行传参!
<select id="xxx" resultType="String" parameter="java.util.HashMap">
select xxx
from xxx
where xxx=#{xxx}
</select>
在controller中实例化参数Map,将参数传入即可:
Map<String,Object> paraMap = new HashMap<>();
paraMap.put("xxx",xxx);
service.selectNameById(paraMap);