像这样,用include标签来引入id为xx的sql片段去实现sql复用。
<sql id="if-title-author-views">
<if test="title != null">
title = #{title},
</if>
<if test="author != null">
author = #{author},
</if>
<if test="views != null">
views = #{views}
</if>
</sql>
<update id="updateBlog" parameterType="map">
update blog
<trim prefix="SET" suffixOverrides=",">
<include refid="if-title-author-views"></include>
</trim>
where id = #{id}
</update>
值得注意的是sql片段中建议不要含有where标签,where包裹sql片段。