一.使用
1.有resultMap屬性的標籤均可以使用java
<select resultMap="命名空間.resultMap的id"></select>
<association resultMap="命名空間.resultMap的id"></association>
<collection resultMap="命名空間.resultMap的id"></collection>
2.某些標籤的extends熟悉應該也能使用(猜想的,待驗證)sql
https://blog.csdn.net/lxxxzzl/article/details/43833903mybatis
<resultMap extends="命名空間.resultMap的id"></resultMap>oracle
public class CocTreeNode extends CocBean implements TreeNode<CocTreeNode> { private String level1, level2; public void setLevel1(String level1){...} public void setLevel2(String level2){...} public String getLevel1(){...} public String getLevel1(){...} } public class CocBean { protected String name; protected Double volume; public void setName(String name){...} public void setVolume(Double volume){...} public String getName(){...} public Double getVolume(){...} }
2、映射xml文件app
利用resultMap的extends屬性。spa
<resultMap id="CocBeanResult" type="CocBean"> <result property="name" column="NAME"/> <result property="volume" column="VOLUME"/> </resultMap> <resultMap id="simpleRow" type="CocTreeNode" extends="CocBeanResult"> <result property="level1" column="LEVEL1"/> <result property="level2" column="LEVEL2"/> </resultMap>
二.格式
命名空間.resultMap的id.net
同任何的代碼庫同樣,在mapper中,一般也會有一些公共的sql代碼段會被不少業務mapper.xml引用到,好比最經常使用的多是分頁和數據權限過濾了,尤爲是在oracle中的分頁語法。爲了減小骨架性代碼,一般將它們抽象到sql中,可是確定又不能在每一個mapper中也包含,這樣就沒有意義了。此時,能夠將這部分移到專門的mapper.xml中,好比common.xml,其中包含以下: code
<?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>
而後在具體的mapper.xml能夠經過namespace進行引用,以下: xml
<select id="queryPage" resultMap="clientPage" parameterType="java.util.Map"> <include refid="Common.pagingStart"/> <include refid="commonSelect"/> <!-- 這裏有個額外的1是爲了不額外處理最後一個」,「 --> <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>
引用:blog