2014-10-31 50 views
0

我在我的sql映射文件中有以下查询,它在为FIELD3生成的SQL查询中使用OR。这是Integeribatis使用IN而不是OR

<select id="myCriteria" parameterClass="mySearchCriteria" resultMap="generated_MyResult"> 
     select * from MY_TABLE where MY_TABLE.FIELD1 = 1 
     <dynamic> 
      <isNotNull prepend="and" property="field2"> 
       MY_TABLE.FIELD2 = $field2$ 
      </isNotNull> 

      <isNotNull prepend="and" property="field3_list"> 
       <iterate close=")" conjunction="or" open="(" property="meIds"> 
        MY_TABLE.FIELD3= $field3_list[]$ 
       </iterate> 
      </isNotNull> 
     </dynamic> 
    </select> 

如果我想用IN代替或Listiterate被删除,下方将工作?

<select id="myCriteria" parameterClass="mySearchCriteria" resultMap="generated_MyResult"> 
     select * from MY_TABLE where MY_TABLE.FIELD1 = 1 
     <dynamic> 
      <isNotNull prepend="and" property="field2"> 
       MY_TABLE.FIELD2 = $field2$ 
      </isNotNull> 

      <isNotNull prepend="and" property="field3_list"> 

        MY_TABLE.FIELD3 IN $field3_list[]$ 

      </isNotNull> 
     </dynamic> 
    </select> 

回答

0

要使用IN子句,我会继续迭代元素:

<select id="myCriteria" parameterClass="mySearchCriteria" resultMap="generated_MyResult"> 
    select * from MY_TABLE where MY_TABLE.FIELD1 = 1 
    <dynamic> 
     <isNotNull prepend="and" property="field2"> 
      MY_TABLE.FIELD2 = #field2# 
     </isNotNull> 
     <isNotEmpty prepend="and" property="field3_list"> 
      MY_TABLE.FIELD3 IN 
      <iterate open="(" close=")" conjunction="," property="field3_list"> 
       #field3_list[]# 
      </iterate> 
     </isNotEmpty> 
    </dynamic> 
</select> 

或者,如果你确信你field3_list永远不能为null:

<select id="myCriteria" parameterClass="mySearchCriteria" resultMap="generated_MyResult"> 
    select * from MY_TABLE where MY_TABLE.FIELD1 = 1 
    <dynamic> 
     <isNotNull prepend="and" property="field2"> 
      MY_TABLE.FIELD2 = #field2# 
     </isNotNull> 
     <iterate open="(" close=")" conjunction="," property="field3_list" prepend="AND MY_TABLE.FIELD3 IN"> 
      #field3_list[]# 
     </iterate> 
    </dynamic> 
</select> 

而且,我切换您的参数从$ xx $(指表名)到#xx#

+0

'isNotEmpty'也在内部检查'isNotNull'吗? – Sridhar 2014-11-03 10:04:41

+0

是的,来自doc:'检查一个Collection,String属性的值是否不为空且不为空(“”或size()<1).'(https://ibatis.apache.org/docs /java/pdf/iBATIS-SqlMaps-2_en.pdf) – 2014-11-03 10:26:54