繁体   English   中英

使用mybatis-spring和MapperScannerConfigurer在多个Mapper xml文件中重用resultMap

[英]With mybatis-spring and MapperScannerConfigurer reuse resultMap in multiple Mapper xml files

我们正在使用

mybatis-spring = 1.1.1 

mybatis = 3.1.1

spring = 3.2.0

MapperScannerConfigurer -  to scan mappers

如何在多个Mapper xml文件中重用resultMap?

在此已回答的问题“在多个mapper.xml中重用MyBatis ResultMap”

解决方案是使用mybatis-config.xml文件并在该文件中添加resultMap详细信息(或导入该文件中的所有映射器文件)。

但是我们没有使用该文件,而是使用了mybatis-spring的MapperScannerConfigurer。

那么,如何使用MapperScannerConfigurer实现相同的功能?

例如,我们有一个userMapper.xml。

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

并且我们需要在例如managerMapper.xml中使用此resultMap,并且需要重用“用户” resultMap。

例如

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

现在,它引发错误java.lang.IllegalArgumentException:结果Maps集合不包含com.domain.ModelUser.user的值

到目前为止,它还不知道如何以及在哪里找到UserMapper.xml文件中的resultMap。

任何帮助和指向它的方向将不胜感激。

谢谢您的时间和帮助。

即使使用Spring和MapperScanner,您仍然可以使用mybatis-config.xml 此xml文件不必是基本MyBatis的完整(或有效)配置。 只需创建一个简单的配置,如下所示:

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

使用SqlSessionFactoryBean.setConfigLocation()引用此文件。 创建SqlSessionFactory时将加载它,并且可以使用提供的名称空间对其进行访问。

我知道已经晚了,但是我的一个项目也遇到了类似的异常。 getManager查询中使用resultMap id ,如下所示:

<select id="getManager" resultMap="user">
    select .......
</select>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM