mybatis 使用foreach 数据类型不对导致报错

起因

使用mybatis动态sql进行遍历条件的时候报了下面这个错误:

Caused by: java.lang.IllegalStateException: Type handler was null on parameter mapping for property '__frch_item_0'. It was either not specified and/or could not be found for the javaType (com.test.Report) : jdbcType (null) combination.
    at org.apache.ibatis.mapping.ParameterMapping$Builder.validate (ParameterMapping.java:117)
    at org.apache.ibatis.mapping.ParameterMapping$Builder.build (ParameterMapping.java:104)
    at org.apache.ibatis.builder.SqlSourceBuilder$ParameterMappingTokenHandler.buildParameterMapping (SqlSourceBuilder.java:123)
    at org.apache.ibatis.builder.SqlSourceBuilder$ParameterMappingTokenHandler.handleToken (SqlSourceBuilder.java:67)
    at org.apache.ibatis.parsing.GenericTokenParser.parse (GenericTokenParser.java:69)
    at org.apache.ibatis.builder.SqlSourceBuilder.parse (SqlSourceBuilder.java:45)
    at org.apache.ibatis.scripting.xmltags.DynamicSqlSource.getBoundSql (DynamicSqlSource.java:44)
    at org.apache.ibatis.mapping.MappedStatement.getBoundSql (MappedStatement.java:292)
    at org.apache.ibatis.executor.CachingExecutor.query (CachingExecutor.java:81)
    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:434)
    at com.sun.proxy.$Proxy57.selectList (Unknown Source)
    at org.mybatis.spring.SqlSessionTemplate.selectList (SqlSessionTemplate.java:231)
<select id="getById" parameterType="map" resultType="Report">
        select * from test
        where orderid in
        <foreach collection="idlist" item="item" index="index" open="(" separator="," close=")">
            #{item}
        </foreach>
        
    </select>

怎么找都没找到原因,最后发现这个sql之所以报错并不是这个sql写错了,而是传入的参数报错了,我认为参数是这个样子的:

{
  "name":"张三",
  "idlsit":{1,2,3}
}

最后发现,参数是这个样子的的:

{
  "name":"张三",
  "idlsit":{Report对象,Report对象,Report对象}
}

看到这里可能会觉得怎么可能出现这种问题,编译都不会通过,但是通过特殊的情况确实产生了,我的代码如下:

List<String> idList = reportDao.getIdList(start, end);

        if (null != idList && idList.size() >= 1) {
            result = orderReportDao.getById(idList, start);
        }

sql:

<select id="getIdList" parameterType="map" resultType="Report">//这个返回值类型错了
        select id from test_id
    </select>

    <select id="getById" parameterType="map" resultType="Report">
        select * from test
        where orderid in
        <foreach collection="idlist" item="item" index="index" open="(" separator="," close=")">
            #{item}
        </foreach>
        
    </select>

实际上在执行过程中getIdList的返回类型错了,但是没有报错误,返回值List < String > idList实际上是List < Report > idList,这竟然没有报错,可能反射有关。

相关推荐