近日开发中用到ibatis在使用的过程中出现了AutoResultMap的错误
配置如下:
<select id="statistic" parameterClass="map" resultClass="java.util.HashMap"> select <isNotNull property="category"> b.description as descriptionB, </isNotNull> <isNotNull property="serl"> c.description as descriptionC, </isNotNull> sum(1) as total from t_es_reginfo a <isNotNull property="category"> <isEqual compareValue="city" property="category"> left join t_bs_$category$ b on a.$category$=b.code </isEqual> <isNotEqual compareValue="city" property="category"> left join t_cd_$category$ b on a.$category$=b.id </isNotEqual> </isNotNull> <isNotNull property="serl"> <isEqual compareValue="city" property="serl"> left join t_bs_$serl$ c on a.$serl$=c.code </isEqual> <isNotEqual compareValue="city" property="serl"> left join t_cd_$serl$ c on a.$serl$=c.id </isNotEqual> </isNotNull> where a.examid=#examid# <isNotNull property="city"> and a.city=#city# </isNotNull> group by 1 <isNotNull property="category"> ,a.$category$ </isNotNull> <isNotNull property="serl"> ,a.$serl$ </isNotNull> order by -1 <isNotNull property="category"> ,a.$category$ </isNotNull> <isNotNull property="serl"> ,a.$serl$ </isNotNull> </select>
由于多次产生的列数可能会不一样,所以当每一次查出来是三列,第二次是两列的话就会报AutoResultMap的错( java.sql.SQLException: 列名无效),经查后发现有个remapResults的属性可以设置为true就可以了,原因如下:
在 【statement】, 【select】, 和【procedure】 标签中存在一个可选的属性【remapResults 】,默认值是false.如果每次查询的列不定的话,这个属性需要设置为true. 为了避免经常的对返回的结果进行内省,iBATIS 会记录上一次查询结果的元数据, iBATIS 会在每次查询的时候内省查询结果来设置元数据,来保证返回恰当的结果。这个属性会造成一定的性能损失,所以要谨慎使用,只在你需要的时候使用--查询列发生变化