where标签、if标签

    1. <select id="selectCondition" resultType="Envs" parameterType="Envs">
    2. SELECT * FROM http_envs
    3. <where>
    4. <if test="id != null">
    5. id = #{id}
    6. </if>
    7. <if test="name != null">
    8. AND name = #{name}
    9. </if>
    10. <if test="desc != null">
    11. AND `desc` = #{desc}
    12. </if>
    13. </where>
    14. </select>

    判断数据对象Envs里id、name、desc不为空则动态添加sql语句

    foreach标签

    1. <select id="selectByIds" resultType="Envs" parameterType="list">
    2. SELECT * FROM http_envs
    3. <where>
    4. <foreach collection="list" open="id IN (" close=")" item="id" separator=",">
    5. #{id}
    6. </foreach>
    7. </where>
    8. </select>

    属性:

    • collection:指定参数容器类型,(list-集合,array-数组)
    • open:指定开始sql语句
    • close:指定结束sql语句
    • item:参数变量名
    • separator:指定分隔符