MyBatis - 将单个对象的查询检查转换为检查集合
MyBatis - Convert query-check from a single object to check on a collection
有如下一段代码。
Select 一堆列 from table where ...左连接 ...哪里
<if test="taskowner != null">
AND (o.primary_taskowner_id = #{taskowner.id}
OR o.secondary_taskowner_id = #{taskowner.id})
</if>
在这种情况下,taskowner 是我的 combobox 中的单个 selected 对象。
要求已更改,现在我需要 combo box-multi select。我怎样才能将那段查询调整为组合框多 select(我想 运行 相同的条件,但在集合中的每个对象上)?
我想到了这个:
<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>
但是结果不是我所期待的。我尝试了该解决方案的多种变体,但其中 none 有效。并没有在网上找到任何有用的东西。
提前致谢。
你要的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 "code" 将是这样的:
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>
)
我找到了解决办法。感谢 DwB 为我指明了正确的方向。
<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>
有如下一段代码。
Select 一堆列 from table where ...左连接 ...哪里
<if test="taskowner != null">
AND (o.primary_taskowner_id = #{taskowner.id}
OR o.secondary_taskowner_id = #{taskowner.id})
</if>
在这种情况下,taskowner 是我的 combobox 中的单个 selected 对象。
要求已更改,现在我需要 combo box-multi select。我怎样才能将那段查询调整为组合框多 select(我想 运行 相同的条件,但在集合中的每个对象上)?
我想到了这个:
<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>
但是结果不是我所期待的。我尝试了该解决方案的多种变体,但其中 none 有效。并没有在网上找到任何有用的东西。
提前致谢。
你要的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 "code" 将是这样的:
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>
)
我找到了解决办法。感谢 DwB 为我指明了正确的方向。
<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>