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