sql片段

来源:互联网 发布:大乐透真假揭秘知乎 编辑:程序博客网 时间:2024/06/05 20:00
    <!-- 将用户查询条件定义为sql片段    建议对单表的查询条件单独抽取sql片段,提高公用性    注意:不要将where标签放在sql片段,where标签可能有其他条件      -->    <sql id="query_user_where">                <if test="username!=null and username!=''">                    and username like '%${username}%'                </if>                <if test="sex!=null and sex!=''">                    and sex = #{sex}                </if>                <!-- 还有很的查询条件 -->    </sql>    <!-- 自定义查询条件查询用户的信息     -->    <select id="findUserList" parameterType="userQueryVo" resultType="user">        select id,username,birthday from user        <!-- where标签相当 于where关键字,可以自动去除第一个and -->        <where>            <!-- 引用sql片段,如果sql片段和引用处不在同一个mapper必须前边加namespace -->            <include refid="query_user_where"></include>            <!-- 下边还有很其它的条件 -->            <!-- <include refid="其它的sql片段"></include> -->        </where>    </select>    <!-- 输出简单类型    功能:自定义查询条件,返回查询记录个数,通常用于实现 查询分页     -->     <select id="findUserCount" parameterType="userQueryVo" resultType="int">        select count(*) from user         <!-- where标签相当 于where关键字,可以自动去除第一个and -->        <where>            <!-- 引用sql片段,如果sql片段和引用处不在同一个mapper必须前边加namespace -->            <include refid="query_user_where"></include>            <!-- 下边还有很其它的条件 -->            <!-- <include refid="其它的sql片段"></include> -->        </where>     </select></mapper>