起因
使用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,這竟然沒有報錯,可能反射有關。