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,这竟然没有报错,可能反射有关。

原文 

https://segmentfault.com/a/1190000018339596

本站部分文章源于互联网,本着传播知识、有益学习和研究的目的进行的转载,为网友免费提供。如有著作权人或出版方提出异议,本站将立即删除。如果您对文章转载有任何疑问请告之我们,以便我们及时纠正。

PS:推荐一个微信公众号: askHarries 或者qq群:474807195,里面会分享一些资深架构师录制的视频录像:有Spring,MyBatis,Netty源码分析,高并发、高性能、分布式、微服务架构的原理,JVM性能优化这些成为架构师必备的知识体系。还能领取免费的学习资源,目前受益良多

转载请注明原文出处:Harries Blog™ » mybatis 使用foreach 数据类型不对导致报错

赞 (0)
分享到:更多 ()

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址