iBatis prepend iterate 标签,in字句的一点总结

1 篇文章 0 订阅

先看一段代码:

<select id="select" resultMap="HotelinfoResult" parameterClass="java.util.Map">
    select 
    id,
    HotelId as hotelId,
    Selectflag as selectflag
    from Hotelinfokuxun
    <dynamic prepend=" WHERE ">
        <isPropertyAvailable property="idList">
            <isNotNull property="idList" prepend=" and id in ">
                <iterate property="idList" conjunction="," close=")" open="(">
                    #idList[]#
                </iterate>
            </isNotNull>
        </isPropertyAvailable>                   
    </dynamic>  
</select> 
预计预计产生的SQL是:
select id, HotelId as hotelId,Selectflag as selectflag from Hotelinfok WHERE id in (?,?,?,?) 
需要注意的是:iBatis对于第一个prepend=" and " 是要移除的,如果不移除,则SQL 语句就是:

select id, HotelId as hotelId,Selectflag as selectflag from Hotelinfok WHERE and id in (?,?,?,?)    //显然这样的SQL是错误的,iBatis移除了字句中第一个and
显然and是不能存在的,iBatis对于动态SQL移除第一个and,但是本例中in字句还包含” and id in",也同时被移除了,所以程序产生报错:

org.springframework.jdbc.BadSqlGrammarException: SqlMapClient operation; bad SQL grammar []; nested exception is com.ibatis.common.jdbc.exception.NestedSQLException:   
--- The error occurred while applying a parameter map.  
--- Check the Hotelinfokuxun.select-InlineParameterMap.  
--- Check the statement (query failed).  
--- Cause: java.sql.SQLException: Operand should contain 1 column(s)

当前面的<isPropertyAvilable>都没有匹配上, prepend="and id in "子句作为第一个prepend时,就会被移除,程序就报错

实际产生的SQL语句是:

select id, HotelId as hotelId,Selectflag as selectflag from Hotelinfok WHERE (?,?,?,?)    //少了应该存在的" id in "

解决方法:

一:增加一个无效的1='1'

 <isPropertyAvailable property="idList">
	<isNotNull prepend=" and ">
		1='1'
	</isNotNull>
	<isNotNull property="idList" prepend=" and id in ">
		<iterate property="idList" conjunction="," close=")" open="(">
			#idList[]#
		</iterate>
	</isNotNull>
</isPropertyAvailable>	 

产生的SQL语句:

select id, HotelId as hotelId,Selectflag as selectflag from Hotelinfok WHERE 1='1'and id in (?,?,?,?) 

二:放弃使用In子句

<isPropertyAvailable property="idList">
	<isNotNull property="idList" prepend=" and ">
		<iterate property="idList" conjunction="OR" close=")" open="(">
			id=#idList[]#
		</iterate>
	</isNotNull>	 	
</isPropertyAvailable>
产生的SQL语句是:

select id, HotelId as hotelId,Selectflag as selectflag from Hotelinfok WHERE ( id=? OR id=? OR id=? OR id=?) 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值