美文网首页java学习笔记整理
mybatis xml文件如何把几个sql公共部分提取

mybatis xml文件如何把几个sql公共部分提取

作者: _借东西的小人 | 来源:发表于2021-04-28 14:44 被阅读0次

1、可以将这部分移到专门的mapper.xml中,比如common.xml

<mapper namespace="Common">
    <sql id="Common.pagingStart">
    </sql>
    <sql id="Common.pagingEnd">
        <![CDATA[ limit #{startWith,jdbcType=INTEGER},
        #{rows,jdbcType=INTEGER} ]]>
    </sql>
</mapper>

2、在具体的mapper.xml可以通过namespace进行引用

<select id="queryPage" resultMap="clientPage"
    parameterType="java.util.Map">
        <include refid="Common.pagingStart"/>
        <include refid="commonSelect"/>
        <include refid="commonFrom"/>
        <include refid="commonWhere"/>
          <if test="clientId != null" >
            and CLIENT_ID = #{clientId,jdbcType=VARCHAR}
          </if>
          <if test="clientName != null" >
            and CLIENT_NAME like '%${clientName}'
          </if>
          <if test="telephone != null" >
            and TELEPHONE = #{telephone,jdbcType=VARCHAR}
          </if>
        order by client_id
        <include refid="Common.pagingEnd"/>    
    </select>

相关文章

网友评论

    本文标题:mybatis xml文件如何把几个sql公共部分提取

    本文链接:https://www.haomeiwen.com/subject/ituvrltx.html