简体   繁体   English

在MyBatis中以不同关联两次映射同一张表

[英]Mapping same table twice in different associations in MyBatis

I have 6 tables structured like this (there is a more descriptive example at the bottom): 我有6个这样的表结构(底部有一个更具描述性的示例):

A 一种

Every item is related to one item in B and one item in C. 每个项目都与B中的一项和C中的一项相关。

aid | bid | cid

B

Every item is related to multiple items in D. 每个项目都与D中的多个项目相关。

bid

C C

Every item is related to multiple items in D. 每个项目都与D中的多个项目相关。

cid

D d

Every item is related to multiple items in B and C. 每个项目都与B和C中的多个项目相关。

did

E Ë

Maps items in B to items in D. 将B中的项目映射到D中的项目。

bid | did

F F

Maps items in C to items in D. 将C中的项目映射到D中的项目。

cid | did

Now I'm trying to write a mapper in MyBatis that maps the items in table A. The mapper has an association to the mappers of table B and C, and the mappers of tables B and C both have a collection containing items of table D. 现在,我试图在MyBatis中编写一个映射器,该映射器映射表A中的项目。该映射器与表B和C的映射器有关联,并且表B和C的映射器都有一个包含表D项的集合。 。

Individually mapping either B or C to items of table D isn't a problem, since then a simple join operation using either E or F is enough. 单独将B或C映射到表D的项都没问题,因为使用E或F进行简单的联接操作就足够了。 However, the problem arises when trying to map everything from the perspective of table A. As table A is associated to both table B and C, and they are both associated with table D, I don't know how to let the mappers of B and C distinguish between the items in table D that are meant for them. 但是,当尝试从表A的角度映射所有内容时会出现问题。由于表A与表B和C都关联,并且它们都与表D关联,所以我不知道如何让B的映射器C和C区分表D中为它们指定的项目。

Basically, I'm trying to join D to B and C from the perspective of A's mapper. 基本上,我正尝试从A的映射器的角度将D加入B和C。

Is there a way to join table D twice in MyBatis, whilst keeping a distinction between the two joins so the mappers of B and C can distinguish between the two? 有没有一种方法可以在MyBatis中两次联接表D,同时保持两个联接之间的区别,以便B和C的映射器可以区分两个联接?

Thanks for the help! 谢谢您的帮助!

EDIT 编辑

Here's a small example with more descriptive names. 这是一个带有更多描述性名称的小示例。

houses 房屋

houseid | livingroomid | kitchenid
1       | 1            | 1

livingrooms 客厅

livingroomid
1

kitchens 厨房

kitchenid
1

furniture 家具

furnitureid | furniturename
1           | couch
2           | lamp
3           | fridge

livingroomfurniture livingroomfurniture

livingroomid | furnitureid
1            | 1
1            | 2

kitchenfurniture kitchenfurniture

kitchenid | furnitureid
1         | 2
1         | 3

My mappers currently: 我的地图绘制者当前:

Mapper.xml Mapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper>
    <resultMap type="com.project.models.house.House" id="HouseResult">
        <id             property="id"           column="houseid" />
        <association    property="livingroom"   resultMap="LivingRoomResult" />
        <association    property="kitchen"      resultMap="KitchenResult" />
    </resultMap>

    <resultMap type="com.project.models.livingroom.LivingRoom" id="LivingRoomResult">
        <id             property="id"           column="livingroomid" />
        <collection     property="furniture"    resultMap="FurnitureResult" />
    </resultMap>

    <resultMap type="com.project.models.kitchen.Kitchen" id="KitchenResult">
        <id             property="id"           column="kitchenid" />
        <collection     property="furniture"    resultMap="FurnitureResult" />
    </resultMap>

    <resultMap type="com.project.models.furniture.Furniture" id="FurnitureResult">
        <id             property="id"           column="furnitureid" />
        <result         property="name"         column="furniturename" />
    </resultMap>

    <select id="getAllHouses" resultMap="HouseResult">
        SELECT
          houses.houseid,
          houses.livingroomid,
          houses.kitchenid

        FROM houses

        -- Get the living room
        INNER JOIN livingrooms ON livingrooms.livingroomid = houses.livingroomid

        -- Get the kitchen
        INNER JOIN kitchens ON kitchens.kitchenid = kitchens.kitchenid

        -- Here somehow join the furniture for both the living room and the kitchen?
    </select>
</mapper>

You actually have to join D to both tables B and C alias them in accordingly. 实际上,您必须将D联接到表B和C上,从而为它们别名。

Let's say bd for table d joined to B and cd . 假设表d的bd连接到B和cd

now if you want same resultmap of table d for both aliased table (bd and cd), you can do this: 现在,如果要为两个别名表(bd和cd)使用表d的相同结果映射,则可以执行以下操作:

select bd.did as bd_did, cd.did as cd_did

And if you have not come across column_prefix property of result maps this is how your resultmap will look 而且,如果您还没有遇到结果图的column_prefix属性,那么结果图将是这样

<collection type="D" resultMap="d" column_prefix="bd_" />

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

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