mybatis中循环嵌套

来源:互联网 发布:施耐庵子孙 哑巴 知乎 编辑:程序博客网 时间:2024/06/16 16:28

       关于mybatis的条件选择语句,一般用<if>sql语句</if>,类似于java中的if(){do something}else{do something},会使用<choose><when>ddd</when><choose>这样,但是一般不会使用的循环嵌套的写法。

如果有一个sql,先判断外层,外层判断完事在判断里层,这就牵扯到了循环嵌套。我们来看一个例子:

 <select id="getAllHotelData" resultType="hotelInfoModel" parameterType="com.elong.vrmanage.model.SearchParamModel">        SELECT * from vote_hotel_detail        <where>            1=1            <if test="hotelName !=null">                and hotelName like CONCAT('%',#{hotelName},'%')            </if>            <if test="hotelId !=null">                and  hotelId=#{hotelId}            </if>            <if test="activityId!=0">                <choose>                    <when  test="hotelids !=null">                        AND hotelId in                        <foreach collection="hotelids" item="item" open="(" separator="," close=")">                            #{item}                        </foreach>                    </when>                <otherwise>                       AND hotelId in (SELECT hotel_id FROM vr_activity_hotel where activity_id=#{activityId})                </otherwise>                </choose>            </if>        </where>        ORDER BY id DESC limit #{page},#{pageSize}    </select>

这个就是判断了activityid不为空,然后再次判断hotelids是不是为空,双重条件决定执行语句


更多文章参考:http://lanyinbin.cn

0 0