Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mybatis reference sql from another mapper.xml file

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.

like image 557
Ginto Hewoo Avatar asked Oct 28 '13 16:10

Ginto Hewoo


People also ask

What is resultMap in XML?

resultMap – The most complicated and powerful element that describes how to load your objects from the database result sets.

How do I connect my database to MyBatis?

Within the environments element, we configure the environment of the database that we use in our application. In MyBatis, you can connect to multiple databases by configuring multiple environment elements. To configure the environment, we are provided with two sub tags namely transactionManager and dataSource.


1 Answers

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>
like image 114
Roman Konoval Avatar answered Nov 15 '22 09:11

Roman Konoval