mybatis resultMap 复用

 使用同一个命名空间里的resultMap,这里的 BaseResultMap 在另一个xml文件中,但是两个文件的命名空间是一样的

<?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="com.cetcht.dao.device.AllInfoMapper">
  
  <resultMap id="TypeListMap" type="com.cetcht.vo.scene.AllInfoTypeVo">
      <result column="class_name_cn" jdbcType="VARCHAR" property="classNameCn" />
    <collection property="infoList" ofType="com.cetcht.entity.device.AllInfo" resultMap="BaseResultMap">
    </collection>
  </resultMap>
  
  <select id="selectTypeList" parameterType="java.lang.String" resultMap="TypeListMap">
    SELECT * from s_all_info WHERE orgz_code LIKE #{orgzCode}
  </select>
  
</mapper>

使用不同命名空间里的resultMap,这里的 com.cetcht.dao.device.AllInfoMapper.BaseResultMap 是引用的另一个命名空间里的 BaseResultMap, 这里的 extends="BaseResultMap" 是同一个命名空间里定义的 BaseResultMap

<?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="com.cetcht.dao.scene.SceneBeanMapper">
  
  <resultMap id="BeanAllInfoMap" type="com.cetcht.entity.scene.SceneBean" extends="BaseResultMap">
    <association property="infoDetail" resultMap="com.cetcht.dao.device.AllInfoMapper.BaseResultMap" >
        <result column="entity_id" jdbcType="BIGINT" property="id" />
       </association>
  </resultMap>
  
  <select id="selectSceneBeanList" parameterType="java.lang.Long" resultMap="BeanAllInfoMap">
    SELECT * from b_scene_bean bean_ 
    RIGHT JOIN s_all_info info_ ON bean_.entity_id = info_.id AND bean_.entity_table_name = info_.class_name
    WHERE bean_.scene_id = #{sceneId}
  </select>
  
</mapper>
原文地址:https://www.cnblogs.com/LcxSummer/p/15034842.html