如何在 iBATIS 中使用 "or" 语句编写查询?



我想使用iBATIS通过动态查询或语句获取数据。

例如

select * from USERS where ID=1 or ID=12 or ID= 3 or ID=27.....

并且我想将一组ID作为列表对象传递。

您可以使用IN语句

<select id="selectKeys" parameterType="list"
        resultMap="selectKeysResultMap">
        SELECT COL1,COL2
        FROM
        TABLE1
        WHERE COL1 IN
        <foreach item="item" index="index" collection="list" open="("
            separator="," close=")">
            #{item}
        </foreach>
    </select>

在您的DataConnector中添加此;

Map<String,Object> inputMap = new HashMap<String,Object>();
Map<String,Object> inputMap = new HashMap<String,Object>();
inputMap.put("idList", idList);
mapper.getMcqAnswers(inputMap);

在您的DBMapper.xml中添加以下内容;

<select id="getMcqAnswers" resultType="your result type">
select id,answers from mcqs where id in
<foreach item="item" index="index" collection="idList" open="(" separator="," close=")">
${item}
</foreach>
</select>

最新更新