Mybatis从另一个mapper.xml文件引用sql

Gin*_*woo 6 xml mapper mybatis

我在一个mapper.xml文件中为一个表/对象编写了一个标准的select和resultMap,我想知道是否有一种方法可以通过关联,集合等上的"select"参数在另一个mapper.xml文件中使用这个select.

Rom*_*val 13

其他映射器文件中定义的元素可以由包含映射器命名空间的完全限定标识符使用.

例如,您在mapper1.xml中选择了:

<mapper namespace="com.foo.bar.mapper.Mapper1">

  <select id="getEntity1" resultType="Entity1">
    select * form entity1
  </select>
</mapper>
Run Code Online (Sandbox Code Playgroud)

它可以在mapper2.xml中使用:

<mapper namespace="com.foo.bar.mapper.Mapper2">

  <resultMap id="entity2ResultMap" type="Entity2">
    <association property="entity1"
                 column="entity1_id" 
                 javaType="Entity1" 
                 select="com.foo.bar.mapper.Mapper1.getEntity1"/>
  </resultMap>

</mapper>
Run Code Online (Sandbox Code Playgroud)