【mybatis】if-else即choose-when的使用

<select id="listChatMsgBriefDTO" parameterType="com.pubdef.bo.chatmsg.ChatMsgQueryBO" resultMap="BaseResultMap">
    select <include refid="Base_Column_List"/>
    from ${tblName}
    where category = #{category}
    <choose>
      <when test="voiceRoomFlag == true ">
        <if test="fromUid != null ">
          and from_id = #{fromUid}
        </if>
        <if test="toUid != null ">
          and to_id in (SELECT room_id FROM `voice_room_members` WHERE uid = #{toUid} and role=3)
        </if>
      </when>
      <when test="twoWay == true">
        and ((from_id = #{fromUid} and to_id = #{toUid}) or (from_id = #{toUid} and to_id = #{fromUid}))
      </when>
      <otherwise>
        <if test="fromUid != null ">
          and from_id = #{fromUid}
        </if>
        <if test="toUid != null ">
          and to_id = #{toUid}
        </if>
      </otherwise>
    </choose>
    <if test="startTime != null">
      and create_time <![CDATA[>]]> #{startTime}
    </if>
    <if test="endTime != null">
      and create_time <![CDATA[<]]> #{endTime}
    </if>
    order by create_time desc LIMIT #{offset}, #{limit}
  </select>

注意
整條語句中只能出現一次choose語句。
choose語句中只能選擇一個when結果

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章