MyBatis version
3.4.5, 3.4.6 as well
Database vendor and version
mysql 5.6.21 (this issue is not related to db per se)
Test case or example project
Steps to reproduce
when querying
public List<QuestionnaireSent> selectByIds(Integer[] ids) {
return commonSession.selectList("QuestionnaireSentMapper.selectByIds", ImmutableMap.of("ids", ids));
}
SELECT *
FROMt_questionnaire<if test="ids.length > 0">
WHEREidin
<foreach collection="ids" open="(" separator="," close=")" item="id">
#{id}
</foreach>
</if>
LIMIT #{ids.length}
Expected result
Actual result
it throws Caused by: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'length' in 'class [Ljava.lang.Integer;' at org.apache.ibatis.reflection.Reflector.getGetInvoker(Reflector.java:419) at org.apache.ibatis.reflection.MetaClass.getGetInvoker(MetaClass.java:164) at org.apache.ibatis.reflection.wrapper.BeanWrapper.getBeanProperty(BeanWrapper.java:162) at org.apache.ibatis.reflection.wrapper.BeanWrapper.get(BeanWrapper.java:49) at org.apache.ibatis.reflection.MetaObject.getValue(MetaObject.java:122) at org.apache.ibatis.reflection.MetaObject.getValue(MetaObject.java:119) at org.apache.ibatis.executor.BaseExecutor.createCacheKey(BaseExecutor.java:219) at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:135) at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:148) at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:141) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:433)
Comment From: harawata
Thanks for the report, @sage417 !
This is not a bug, though.
ids.length is an OGNL expression, but the expression in #{} is evaluated by MyBatis, not by OGNL.
You may need to pass the array length as a statement parameter or use <bind />.
<bind name="idCount" value="ids.length" />
LIMIT #{idCount}