根据条件在ibatis中追加OR

Appending OR in ibatis based on the condition

我正在使用 iBATIS 创建 select 语句。

我有 4 个条件,例如 aUsers、bUsers、cUsers 和 dUsers,我们将我的 table 称为 b

现在我想用 iBATIS 实现以下 SQL 语句:

b.aUsers=aUsers OR b.bUsers=bUsers OR b.cUsers=cUsers OR b.dUsers=dUsers

如果 aUsers 为 null 而 rest 不是,那么我希望查询为

b.bUsers=b用户或b.cUsers=c用户或b.dUsers=d用户

//---------------------------------------- ----

如果 cUsers 为 null 而 rest 不是,那么我希望查询为

b.aUsers=aUsers OR b.bUsers=bUsers OR b.dUsers=dUsers

等等

如何在 ibatis 中创建这样的查询?

在这种情况下,您需要创建 Dynamic Queries.

您可以使用 Unary Conditional Elements 检查属性是否为空。

<isNull> Checks if a property is null.

<isNotNull> Checks if a property is not null

此外,您的情况可能需要嵌套 isNotNull || isNull 贬义。所以你只需要做一些类似的事情,比如检查 aUsers 是否为 null,然后检查其他属性:

<select id="getUsers" parameterClass="USER" resultMap="your-result-map" >
  select * from b
    <dynamic prepend="WHERE">
      <isNull prepend="AND" property="aUsers">
          <isNotNull property="bUsers">
                  <!-- ... and so on -->
                  <!-- your condition -->
          </isNotNull>
      </isNull>
      <isNotNull prepend="AND" property="aUsers">
         <isNull  property="bUsers">
              <!-- ... and so on -->
              <!-- your condition -->
         </isNull>
      </isNotNull>
    </dynamic>
</select>