SQL片段一般基于单表建立,这样SQL片段可重用性才高。SQL片段一般不要包含where条件。
引用其他Mapper的SQL片段时,需要在refid属性中加上其他Mapper的namespace属性!
<!--定义-->
<sql id="query_user_where">
<if test="id!=null and id!=''">
and User.id=#{id}
</if>
<if test="name!=null and name!=''">
and User.name=#{name};
</if>
<if test="age!=null and age!=''">
and User.age=#{age};
</if>
</sql>
<!--引用-->
<include refid="query_user_where"/>
where元素会自动去除以and或or开头的SQL。
<!--定义-->
<where>
<if test="id!=null and id!=''">
and User.id=#{id}
</if>
<if test="name!=null and name!=''">
and User.name=#{name};
</if>
<where>
原文:https://www.cnblogs.com/feiqiangsheng/p/11719916.html