0

We are using

mybatis-spring = 1.1.1 

mybatis = 3.1.1

spring = 3.2.0

MapperScannerConfigurer -  to scan mappers

How do we reuse resultMap in multiple Mapper xml files?

In this answered question "Reusing MyBatis ResultMap in multiple mapper.xml"

Solution is to use mybatis-config.xml files and add resultMap detail in that file (or import all mapper files in that file).

But we are not using that file and instead using mybatis-spring's MapperScannerConfigurer.

So how can we achieve the same feature with MapperScannerConfigurer?

For example we have a userMapper.xml.

<resultMap id="user" type="com.domain.ModelUser">
    <result>..</result>
    ...
    ...
</resultMap>

and we need to use this resultMap in for example managerMapper.xml and need to reuse the "user" resultMap.

For example

<select id="getManager" resultMap="com.domain.ModelUser.user">
    select .......
</select>

Right now it throws error java.lang.IllegalArgumentException: Result Maps collection does not contain value for com.domain.ModelUser.user

As of now it do not know how and where to find the resultMap in UserMapper.xml file

Any help and direction toward it will be appreciated.

Thank you for you time and help.

Community
  • 1
  • 1
Fossil
  • 179
  • 1
  • 3
  • 15

2 Answers2

0

You can still use mybatis-config.xml even with Spring and the MapperScanner. This xml file does not have to be a complete (or valid) configuration for base MyBatis. Just create a simple config like this:

<config>
   <mapper namespace="foo">
      <resultMap id="user" type="com.domain.ModelUser">
      <result>..</result>
      ...
      ...
      </resultMap>
   </mapper>
</config>

Reference this file with SqlSessionFactoryBean.setConfigLocation(). It will get loaded when the SqlSessionFactory is created and will be accessible using the namespace provided.

AngerClown
  • 6,149
  • 1
  • 25
  • 28
  • Sorry for late update. Thank you AngerClown for you help and time, when I tried to do that (adding mybatis-config.xml file) the app threw other errors about not finding other mapper files - may be now it has the config file and its loosing the config of the MapperScannerConfigurer option? But I did find this out to use property in class="org.mybatis.spring.SqlSessionFactoryBean" bean and using resultType="User" worked so i just did that and did not continue to debug the config file option – Fossil Jan 22 '15 at 16:24
0

I know it's late but I was also getting similar exception in one of my project. Use id of the resultMap in your getManager query as shown below:

<select id="getManager" resultMap="user">
    select .......
</select>
justAbit
  • 4,226
  • 2
  • 19
  • 34