在多个mapper.xml中重用MyBatis ResultMap [英] Reusing MyBatis ResultMap in multiple mapper.xml

查看:1489
本文介绍了在多个mapper.xml中重用MyBatis ResultMap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想重用来自不同* Mapper.xml文件的特定文件,这些文件都以某种方式读取相同的对象.

我有一个名为Project的数据库表,我为此创建了以下resultMap:

<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>

它在ProjectMapper.xml中很好用,但是,现在我想创建一个ClientWithProjectsMapper.xml,在其中我要从* CLIENT中选择* PROJECT,其中PROJECT.CLIENT_ID = CLIENT.ID,并让一个Client对象返回一个列表对象.换句话说,我想使用一个SQL获得ClientWithProjects.

在映射中,我想重用我在ProjectMapper.xml中定义的ProjectMap(没有复制/粘贴),但是我不确定如何做到这一点.

我可以将ProjectMap分解为一个单独的文件,但是我没有在MyBatis中找到任何设施来#include其他文件.

关于如何做到这一点的任何想法? (我正在使用Maven,是否有任何插件可以过滤查找#include等文件,并将文件内容直接包含在正在处理的文件中?)

谢谢.

-AP_

解决方案

在mybatis-config.xml文件中导入所有映射器xml之后,您可以使用resultMap id和名称空间引用在任何映射器xml中配置的ResultMap. /p>

例如: 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>

在ClientWithProjectsMapper.xml中

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

此处您可以使用完全限定的名称作为"com.mybatisapp.mappers.ProjectMapper.ProjectMap"

在另一个Mapper xml文件中引用resultMap.

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_

解决方案

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"

这篇关于在多个mapper.xml中重用MyBatis ResultMap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆