Mybatis public code is extracted to a separate mapper.xml file

Like any code base, in mapper, there are usually some common sql code segments that are referenced by many business mapper.xml. For example, the most common ones may be paging and data permission filtering, especially in oracle. Paging syntax. In order to reduce the skeleton code, they are usually abstracted into sql, but they must not be included in each mapper, so it is meaningless. At this point, you can move this part to a special mapper.xml, such as common.xml, which contains the following:
<?xml version="1.0" encoding="UTF-8"?>
 
<!DOCTYPE mapper  
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 
<mapper namespace="Common">
    <sql id="Common.pagingStart">
    </sql>
    <sql id="Common.pagingEnd">
        <![CDATA[ limit #{startWith,jdbcType=INTEGER},#{rows,jdbcType=INTEGER} ]]>
    </sql>
</mapper>


Then the specific mapper.xml can be referenced by namespace, as follows:
<select id="queryPage" resultMap="clientPage" parameterType="java.util.Map">
        <include refid="Common.pagingStart"/>
        <include refid="commonSelect"/>
            <!-- There is an extra 1 here to avoid extra processing of the last "," -->
        <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>



Reprinted from http://www.cnblogs.com/zhjh256/p/6193278.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325346289&siteId=291194637