where-if
<select id="listBySearch" resultType="java.lang.Integer">
SELECT id FROM hospital
<where>
<if test="search.province!=null and search.province!=''">
province=#{search.province}
</if>
<if test="search.city!=null and search.city!=''">
AND city=#{search.city}
</if>
</where>
</select>
test里面是条件 多个条件用and连接,if外加where标签后如果第一个if不符合条件,第二个if会自动去掉开头的 AND (不会去掉结尾的AND)SQL:
SELECT id FROM hospital WHERE city='xxx'
set-if
<update id="updateHospitalById">
UPDATE user
<set >
<if test="name != null" >
name = #{name},
</if>
<if test="sex != null" >
sex = #{sex},
</if>
</set>
WHERE type = #{type}
</update>
test里面是条件 多个条件用and连接,if外加set标签后编译出来的sql会去掉sex = #{sex},
中的,
,如果条件都满足则SQL为:
UPDATE user SET name='xxx' ,sex='xxx' WHERE type='xxx'
for
<select id="listByIds" resultType="hospital">
SELECT * FROM hospital
<foreach collection="list" item="item" index="index" open="WHERE id IN (" separator="," close=")">
#{item.id}
</foreach>
</select>
collection:要循环的集合
item:当前迭代对象
index:当前迭代次数
open:以什么字符开始
separator:分割符
close:以什么结束
如果list 的size为4,id分别为 1,2,3,4则SQL为:
SELECT * FROM hospital WHERE id IN(1,2,3,4)