mybatis 使用foreach 數據類型不對致使報錯

原由

使用mybatis動態sql進行遍歷條件的時候報了下面這個錯誤:java

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寫錯了,而是傳入的參數報錯了,我認爲參數是這個樣子的:spring

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

最後發現,參數是這個樣子的的:sql

{
  "name":"張三",
  "idlsit":{Report對象,Report對象,Report對象}
}

看到這裏可能會以爲怎麼可能出現這種問題,編譯都不會經過,可是經過特殊的狀況確實產生了,個人代碼以下:apache

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

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

sql:session

<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,這居然沒有報錯,可能反射有關。mybatis

相關文章
相關標籤/搜索