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

相关文章:

  • 2021-11-25
  • 2022-01-09
  • 2022-12-23
  • 2022-01-11
  • 2022-12-23
  • 2021-07-29
猜你喜欢
  • 2021-10-07
  • 2021-09-16
  • 2022-12-23
  • 2021-12-06
  • 2021-07-23
  • 2022-02-03
相关资源
相似解决方案