ibatis使用IN而不是OR

时间:2014-10-31 10:47:27

标签: java postgresql ibatis

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

Integer
<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而不是OR,iterate被移除,以下内容会有效吗?

<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>

1 个答案:

答案 0 :(得分:0)

要使用IN子句,我会保留iterate元素:

<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#