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

栏目: Java · 发布时间: 7年前

内容简介:使用mybatis动态sql进行遍历条件的时候报了下面这个错误:怎么找都没找到原因,最后发现这个sql之所以报错并不是这个sql写错了,而是传入的参数报错了,我认为参数是这个样子的:最后发现,参数是这个样子的的:

起因

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


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Defensive Design for the Web

Defensive Design for the Web

37signals、Matthew Linderman、Jason Fried / New Riders / 2004-3-2 / GBP 18.99

Let's admit it: Things will go wrong online. No matter how carefully you design a site, no matter how much testing you do, customers still encounter problems. So how do you handle these inevitable bre......一起来看看 《Defensive Design for the Web》 这本书的介绍吧!

MD5 加密
MD5 加密

MD5 加密工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具