<!-- 该语句结合and or 来查询 是否是老年或者儿童 -->
    <select id="selectCount" resultType="java.lang.Integer" parameterType="cn.xx.xx.User">
        <!-- SELECT COUNT(s.id)
        FROM user s
        <where>
            <if test="id != null">
                AND s.id = #{id,jdbcType=INTEGER}
            </if>
            <if test="age != null">
                AND s.age = #{age,jdbcType=INTEGER}
            </if>
        </where> -->
        SELECT COUNT(s.id)
        FROM user s
        where 1=1
        <if test="id != null">
            AND s.id = #{id,jdbcType=INTEGER}
        </if>
        <if test="flag != null and flag == 1">
            AND s.age &lt; 60
            AND s.age &gt; 14
        </if>
        <if test="flag != null and flag == 2">
            AND ( s.age &gt; 60
            OR s.age = 60
            OR s.age = 14
            OR s.age &lt; 14 )
        </if>
    </select>

 此查询中必须将 xxx OR xxx OR xxx 放入 (),否则之前的and 条件将不起作用

posted on 2018-09-27 14:55  XT_小嘎  阅读(5540)  评论(0编辑  收藏  举报