18

I would like to re-use a specific from different *Mapper.xml files which all somehow read same objects.

I have a Database table called Project, which I created the following resultMap for:

<resultMap id="ProjectMap" type="com.model.Project">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="name" jdbcType="VARCHAR" property="name" />
    <result column="client_prj_no" jdbcType="VARCHAR" property="clientPrjNo" />
    <result column="notes" jdbcType="VARCHAR" property="notes" />
    <result column="start_date" jdbcType="TIMESTAMP" property="startDate" />
    ...
<resultMap>

It works great in the ProjectMapper.xml, however, now I want to create a ClientWithProjectsMapper.xml where I want to SELECT * FROM CLIENT, PROJECT where PROJECT.CLIENT_ID = CLIENT.ID and have a Client object return with a List objects. In other words, I want to get a ClientWithProjects with a single SQL.

In my mapping, I want to reuse the ProjectMap (without copy/paste) which I defined in the ProjectMapper.xml, but I am not sure how to accomplish this.

I could factor out the ProjectMap into a separate file, but I have not found any facilities in MyBatis to #include other files.

Any ideas on how this can be done? (I am using Maven, are there any plugins that would filter the files looking for #include or such, and include the contents of the file right into file being processed?).

Thanks.

-AP_

bernie
  • 9,820
  • 5
  • 62
  • 92
Alex Paransky
  • 295
  • 1
  • 3
  • 12
  • 1
    MyBatis only allows importing of properties files. Mapper xml files can be specified in the main config xml, but there's no way to include whole XML elements. Maven or some other build step would be needed to create the final XML dynamically. – AngerClown Nov 21 '12 at 23:23
  • I believe this is a duplicate? http://stackoverflow.com/questions/6069071/mybatis-ibatis-reusable-sql-fragments-in-a-separate-sql-map-file – Andy Nov 27 '12 at 03:52
  • have a look http://stackoverflow.com/a/43356852/2762716 – Alexander Davliatov Apr 11 '17 at 22:52

1 Answers1

29

Once you import all the mapper xmls in mybatis-config.xml file you can refer ResultMaps configured in any of the mapper xmls using resultMap id along with namespace.

for Ex: ProjectMapper.xml

<mapper namespace="com.mybatisapp.mappers.ProjectMapper">
    <resultMap id="ProjectMap" type="com.model.Project">
        <id column="id" jdbcType="BIGINT" property="id" />
        <result column="name" jdbcType="VARCHAR" property="name" />     
    <resultMap>
</mapper>

In ClientWithProjectsMapper.xml

    <select id="selectProjectsByClient" parameterType="int" 
resultMap="com.mybatisapp.mappers.ProjectMapper.ProjectMap">
    select * from blahblah
    </select>

Here you can reference resultMap in another Mapper xml files using fully qualified name as "com.mybatisapp.mappers.ProjectMapper.ProjectMap"

K. Siva Prasad Reddy
  • 11,786
  • 12
  • 68
  • 95
  • Is there a way to define common result maps outside mappers? For example: we include "resultMaps.xml" first, then "projectMaper.xml" and "clientWithProjectsMapper.xml" will use the common result maps in "resultMaps.xml". – emeraldhieu Dec 03 '15 at 04:45