Mybatis执行报错:There is no getter for property named “item” in class com.itzhimei.TestMobileListCondition

代码的实现目标是基于一个电话号码的List查询条件,去查询一组数据,但是报错如下:

org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'item' in 'class com.itzhimei.TestMobileListCondition'
	at org.apache.ibatis.reflection.Reflector.getGetInvoker(Reflector.java:375) ~[mybatis-3.5.7.jar:3.5.7]
	at org.apache.ibatis.reflection.MetaClass.getGetInvoker(MetaClass.java:164) ~[mybatis-3.5.7.jar:3.5.7]
	at org.apache.ibatis.reflection.wrapper.BeanWrapper.getBeanProperty(BeanWrapper.java:162) ~[mybatis-3.5.7.jar:3.5.7]
	at org.apache.ibatis.reflection.wrapper.BeanWrapper.get(BeanWrapper.java:49) ~[mybatis-3.5.7.jar:3.5.7]
	at org.apache.ibatis.reflection.MetaObject.getValue(MetaObject.java:122) ~[mybatis-3.5.7.jar:3.5.7]
	at org.apache.ibatis.executor.BaseExecutor.createCacheKey(BaseExecutor.java:219) ~[mybatis-3.5.7.jar:3.5.7]
	at org.apache.ibatis.executor.CachingExecutor.createCacheKey(CachingExecutor.java:146) ~[mybatis-3.5.7.jar:3.5.7]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_181]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_181]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_181]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_181]
	at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:64) ~[mybatis-3.5.7.jar:3.5.7]
	at com.sun.proxy.$Proxy282.createCacheKey(Unknown Source) ~[?:?]
	at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:62) ~[mybatis-3.5.7.jar:3.5.7]
	at com.sun.proxy.$Proxy282.query(Unknown Source) ~[?:?]
	at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:151) ~[mybatis-3.5.7.jar:3.5.7]
	at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:145) ~[mybatis-3.5.7.jar:3.5.7]
	at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:140) ~[mybatis-3.5.7.jar:3.5.7]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_181]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_181]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_181]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_181]
	at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:427) ~[mybatis-spring-2.0.6.jar:2.0.6]
	at com.sun.proxy.$Proxy166.selectList(Unknown Source) ~[?:?]

我是要循环组装查询条件,使用mybatis的动态sql去拼装一个in的sql语法,代码如下:

<foreach collection="mobileList" item="mobile" separator="," index="index" open="(" close=")">
  #{item}
</foreach>

细心的朋友可能已经发现问题所在了,问题在于我定义集合中每个元素的别名是item=”mobile”,结果在foreache循环体中使用的却是“#{item}”,显然mybatis是找不到item别名的变量的,应该使用#{mobile},代码改正如下:

<foreach collection="mobileList" item="mobile" separator="," index="index" open="(" close=")">
  #{mobile}
</foreach>	

这是一个粗心的问题,有时候陷入的思维定式,就需要一点时间去发现这个问题。