“class java.lang.String”中没有名为“tablename”的属性的getter
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38948298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
There is no getter for property named 'tablename' in 'class java.lang.String'
提问by Searene
I got the error There is no getter for property named 'tablename' in 'class java.lang.String'
when I'm using mybatis.
我There is no getter for property named 'tablename' in 'class java.lang.String'
在使用 mybatis 时遇到错误。
My mapper xml is like this:
我的映射器 xml 是这样的:
<mapper namespace="com.company.mapper.BasicMapper">
<update id="dropTable" parameterType="String">
DROP TABLE ${tablename}
</update>
</mapper>
The interface is like this:
界面是这样的:
public interface BasicMapper {
void dropTable(String tablename);
}
I use it in this way:
我是这样使用的:
public void dropTable(String tablename) {
basicMapper.dropTable(tablename);
}
I tried to replace ${tablename}
with #{tablename}
, but it didn't help. How can I make it work?
我试图取代${tablename}
用#{tablename}
,但它并没有帮助。我怎样才能让它工作?
回答by Linyun Liu
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.
如果您只想传递一个 String 对象并在映射函数中使用传递参数的名称,您需要使用 #{} 而不是 ${},并且它仅适用于具有一个参数的函数。
Such as:
如:
List<ItemMeta> getItemMetaByName(String itemName);
with mapping as:
映射为:
<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)
回答by vbezhenar
Use the following annotation for parameter:
对参数使用以下注释:
public interface BasicMapper {
void dropTable(@Param("tablename") String tablename);
}
回答by hewei
use ${_parameter}
instead of ${tablename}
使用${_parameter}
代替${tablename}
回答by W.Q.Lin
<mapper namespace="com.company.mapper.BasicMapper">
<update id="dropTable" parameterType="string">
DROP TABLE ${tablename}
</update>
use "string" not "String" http://www.mybatis.org/mybatis-3/configuration.html#typeAliasesjavatype "String" alias as "string" in mybatis.
在 mybatis 中使用“字符串”而不是“字符串” http://www.mybatis.org/mybatis-3/configuration.html#typeAliasesjavatype“字符串”别名作为“字符串”。
回答by zht
when your parameterType is "string", use ${value}
instead of ${tablename}
.
当你的参数类型为“字符串”,使用${value}
代替${tablename}
。