Java MyBatis-将查询检查从单个对象转换为对集合的检查

Java MyBatis-将查询检查从单个对象转换为对集合的检查,java,mybatis,Java,Mybatis,有下面的代码 从表中选择一组列,其中。。。左接…在哪里 在本例中,taskowner是我的组合框中的单个选定对象 要求已经改变,现在我需要组合框多选。如何将该查询调整为组合框multi-selectI我想在集合中的每个对象上运行相同的条件 我想到了这个: <if test="taskowners != null and taskowners.empty == false"> AND o.primary_taskowner_id <foreach item=

有下面的代码

从表中选择一组列,其中。。。左接…在哪里

在本例中,taskowner是我的组合框中的单个选定对象

要求已经改变,现在我需要组合框多选。如何将该查询调整为组合框multi-selectI我想在集合中的每个对象上运行相同的条件

我想到了这个:

<if test="taskowners != null and taskowners.empty == false">

     AND o.primary_taskowner_id 
    <foreach item="taskowner" collection="taskowners" open="(" separator="," close=")">
         #{taskowner.id}
   </foreach>

    OR o.secondary_taskowner_id 
    <foreach item="taskowner" collection="taskowners" open="(" separator="," close=")">
         #{taskowner.id}
    </foreach>
</if> 
但结果并不是我所期望的。我尝试了该解决方案的多种变体,但都不起作用。在网上也没有找到任何有用的东西


提前谢谢

您想要的SQL似乎是以下内容的变体:

... and
(o.prim_id = '7' or o.second_id = '7') or
(o.prim_id = '8' or o.second_id = '8') or
(o.prim_id = '9' or o.second_id = '9') ...
使用一个foreach循环来生成这个。 MyBatis代码如下所示:

AND
(
<foreach ...blah>
   (o.prim_id = {tid} or o.second_id = {tid})
   <conditional or> (I'm sure mybatis provides this, but I dont remember the syntax.
</foreach>
)

我找到了解决办法。谢谢你给我指明了正确的方向

<if test="taskowners != null and taskowners.empty == false">

    AND (o.primary_taskowner_id IN
    <foreach item="taskowner" collection="taskowners" open="(" separator="," close=")">
        #{taskowner.id}
    </foreach>

    OR o.secondary_taskowner_id IN
    <foreach item="taskowner" collection="taskowners" open="(" separator="," close=")">
        #{taskowner.id}
    </foreach>)
</if>

解释所需的逻辑,可能使用示例SQL。你想要a、b、c中的x还是q、r、s中的y?这似乎就是您所写的。步骤1是创建一个SQL DML,它可以为3个或多个元素执行您想要的操作。@DwB I有一个任务所有者的集合;每个任务所有者都有一些\u id。我想加入一些\u id=主要\u taskowner\u id的行,如果使用该主要\u taskowner\u id未找到任何内容,则检查一些\u id=次要\u taskowner\u id的位置。我想对每个集合条目执行该检查。确切地说,为此编写SQL,MyBatis实现将变得简单。
<if test="taskowners != null and taskowners.empty == false">

    AND (o.primary_taskowner_id IN
    <foreach item="taskowner" collection="taskowners" open="(" separator="," close=")">
        #{taskowner.id}
    </foreach>

    OR o.secondary_taskowner_id IN
    <foreach item="taskowner" collection="taskowners" open="(" separator="," close=")">
        #{taskowner.id}
    </foreach>)
</if>