Mybatis reference sql from another mapper.xml file
Asked Answered
U

1

7

I have written a standard select and resultMap for a table/object in one mapper.xml file and am wondering if there is a method to use this select in another mapper.xml file via the "select" parameter on Associations, Collections, etc.

Underthrust answered 28/10, 2013 at 16:14 Comment(1)
can you give an example of what you want to achieve?Crenelate
A
15

Elements defined in other mapper files can be used by fully qualified identifier which includes mapper namespace.

For example you have select in mapper1.xml:

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

  <select id="getEntity1" resultType="Entity1">
    select * form entity1
  </select>
</mapper>

It can be used in 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>
Astilbe answered 2/11, 2013 at 17:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.