MyBatis中if - else if - else 的使用

Posted _不正

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MyBatis中if - else if - else 的使用相关的知识,希望对你有一定的参考价值。

有表user(id, name, state, sex, age)

1、单个 if - else 使用。

  根据状态不同进行查询

  

<select id="selectUserByState" resultType="com.bz.model.entity.User">
    SELECT
      *
    FROM
      user
    WHERE
      1=1
    <choose>
      <when test="state == 1">
        AND name = #{name1}
      </when>
      <otherwise>
        AND name = #{name2}
      </otherwise>
    </choose>
  </select>

2、多个if -else if -else的使用。

  

<select id="selectUserByState" resultType="com.bz.model.entity.User">
    SELECT
      *
    FROM
      user
    WHERE
      1=1
    <choose>
      <when test="state == 1">
        AND name = #{name1}
      </when>
     <when test="state == 2">
        AND name = #{name2}
      </when>
      <otherwise>
        AND name = #{name3}
      </otherwise>
    </choose>
  </select>

 

以上是关于MyBatis中if - else if - else 的使用的主要内容,如果未能解决你的问题,请参考以下文章