使用动态SQL可以避免写一些重载函数,以下给出了 select 和 update 的例子
1. select
当有条件时,按条件进行选择;没有条件时,返回全部数据
例子:当传入的map中包含了title时,按title进行查询,如果没有则返回全部数据
<select id="selectBlog" parameterType="map" resultType="">
select * from blog
<where>
<if test="title != null">
and title = #{title}
</if>
</where>
</select>
2. update
当有参数时,更新相应的列;没有参数时忽略
例子:当传入了title时修改title,传入了author时修改author(如果没有传入参数的话会报错)
<update id="updateBlog" parameterType="map">
update blog
<set>
<if test="title != null">
title = #{title},
</if>
<if test="author != null">
author = #{author}
</if>
</set>
where id = #{id}
</update>