问题描述
使用mybatis时出现错误There is no getter for property named 'tablename' in 'class java.lang.String'
.
I got the error There is no getter for property named 'tablename' in 'class java.lang.String'
when I'm using mybatis.
我的映射器xml是这样的:
My mapper xml is like this:
<mapper namespace="com.company.mapper.BasicMapper">
<update id="dropTable" parameterType="String">
DROP TABLE ${tablename}
</update>
</mapper>
界面是这样的:
public interface BasicMapper {
void dropTable(String tablename);
}
我以这种方式使用它:
public void dropTable(String tablename) {
basicMapper.dropTable(tablename);
}
我尝试将${tablename}
替换为#{tablename}
,但没有帮助.我该如何运作?
I tried to replace ${tablename}
with #{tablename}
, but it didn't help. How can I make it work?
推荐答案
如果您只希望传递String对象并像映射函数中那样使用传递的参数的名称,则需要使用#{}而不是$ { },并且仅适用于具有一个参数的函数.
If you want to only pass a String object and use that name of the passed parameter as in the mapping function you need to use #{} instead of ${}, and it only works with function having one parameter.
例如:
List<ItemMeta> getItemMetaByName(String itemName);
映射为:
<select id="getItemMetaByName" parameterType="String" resultMap="itemMapper">
select * from ItemMeta where name like #{itemName}
</select>
以下映射将引发提及的异常:
The following mapping will throw the exception metioned:
<select id="getItemByName" parameterType="String" resultMap="itemMapper">
select * from ItemMeta where name like ${itemName}
</select>
org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'itemName' in 'class java.lang.String'
at org.apache.ibatis.reflection.Reflector.getGetInvoker(Reflector.java:377)
at org.apache.ibatis.reflection.MetaClass.getGetInvoker(MetaClass.java:167)
at org.apache.ibatis.reflection.wrapper.BeanWrapper.getBeanProperty(BeanWrapper.java:149)
这篇关于在“类java.lang.String"中没有名为"tablename"的属性的获取器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!