ibatis动态拼写sql语句自动缓存导致后续查询错误

在使用Ibatis动态拼写SQL语句时,一次查询后会将SQL自动缓存,导致如果后续查询参数变化,特别是字段选择不同,查询会报错。错误表现为尝试访问无效的字段。为解决此问题,可以设置`<select>`标签的`remapResults="true"`属性,确保每次查询都会重新组织SQL,避免使用缓存中的旧SQL导致错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

案例:
动态sql语句

<select id="queryRAWValue" parameterClass="java.util.HashMap" resultClass="java.util.HashMap" >
		<dynamic prepend="select">
		<isNotNull prepend="," property="attrName00"> $attrName00$</isNotNull>
		<isNotNull prepend="," property="attrName01"> $attrName01$</isNotNull>
		<isNotNull prepend="," property="attrName02"> $attrName02$</isNotNull>
		<isNotNull prepend="," property="attrName03"> $attrName03$</isNotNull>
		</dynamic>
		 from $TABLENAME$ 
		 where 1=1 
		 	<isNotNull prepend="and" property="CValue00"> $CAtt00$ $COptional00$ #CValue00#</isNotNull>
			<isNotNull prepend="and" property="CValue01"> $CAtt01$ $COptional01$ #CValue01#</isNotNull>
			<isNotNull prepend="and" property="CValue02"> $CAtt02$ $COptional02$ #CValue02#</isNotNull>
			<isNotNull prepend="and" property="CValue03"> $CAtt03$ $COptional03$ #CValue03#</isNotNull>
		
		<dynamic prepend="and" open="(" close=")">
			
			<isNotNull prepend="or" property="KeyValue00"> $KeyAtt$ = #KeyValue00#</isNotNull>
			<isNotNull prepend="or" property="KeyValue01"> $KeyAtt$ = #KeyValue01#</isNotNull>		
		</dynamic>		
	</select>

执行sql:

List<Map<String, Object>> retMap = (List<Map<String, Object>>) getSqlExe().queryForList("queryRAWValue", cIMMaps);

当执行一次查询后会自动把sql语句缓存,第二次执行时会把缓存中的sql执行,而不会重新组织sql。所以会出现当第二次传入的cIMMaps值变化时(主要是需要查询的字段),查询出现错误:字段无效。

解决方法:

<select id="queryRAWValue" parameterClass="java.util.HashMap" resultClass="java.util.HashMap" remapResults="true">

在select便签中加入remapResults=“true”,每次查询重组sql。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值