sql片段提取引用


sql片段 

sql中可將重復的sql提取出來,使用時用include引用即可,最終達到sql重用的目的,如下:

<!-- 傳遞pojo綜合查詢用戶信息 -->
    <select id="findUserList" parameterType="user" resultType="user">
        select * from user 
        <where>
            <if test="id!=null and id!=''">
            and id=#{id}
            </if>

            <if test="username!=null and username!=''">
            and username like '%${username}%'
            </if>
        </where>
    </select>

將where條件抽取出來,並加上id:

<sql id="query_user_where">
    <if test="id!=null and id!=''">
        and id=#{id}
    </if>

    <if test="username!=null and username!=''">
        and username like '%${username}%'
    </if>
</sql>

使用include引用:

<select id="findUserList" parameterType="user" resultType="user">
        select * from user 
        <where>
            <include refid="query_user_where"/>
        </where>
    </select>

注意:如果引用其它mapper.xml的sql片段,則在引用時需要加上namespace


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM