mybatis使用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>
      <otherwise>
        AND name = #{name2}
      </otherwise>
    </choose>
  </select>

多个if elseif 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>

 

posted @ 2021-10-13 12:02  浅笑19  阅读(841)  评论(0编辑  收藏  举报