如何在SQL Select语句(IBatis 2)中处理Empty IN子句?

时间:2015-02-06 06:30:09

标签: java sql oracle ibatis

我在IBatis第2版中编写了以下SQL语句:

<select id="mySelect" resultClass="long" >
    SELECT  COUNT(*)        
    FROM    myTable 
    WHERE   myTable.columnA IN
        <iterate property="myInClauseValues" open="(" close=")" conjunction=",">                    
                #myInClauseValues[]#                    
        </iterate>          
</select>

如果myInClauseValues(这是一个带有Long的ArrayList)包含至少一个值,那么该语句可以正常工作。但是如果myInClauseValues为空,我会得到一条错误消息(我使用的是oracle数据库):

 Check the statement (query failed). 
 Cause: java.sql.SQLException: ORA-00936: Expression is missing 

1 个答案:

答案 0 :(得分:0)

使用Mybatis动态查询是否正确? 例如:

<select id="mySelect" resultClass="long" >
    SELECT  COUNT(*)        
    FROM    myTable 
   <where>
    <isNotEmpty prepend="AND" property="myInClauseValues" >
       myTable.columnA IN
       <iterate property="myInClauseValues" open="(" close=")" conjunction=",">                    
                    #myInClauseValues[]#                    
       </iterate>       
    </isNotEmpty>   
   </where>      
</select>