在 MyBatis 中的不同关联中映射相同 table 两次
Mapping same table twice in different associations in MyBatis
我有 6 个 table 结构如下(底部有一个更具描述性的示例):
一个
每一项都与 B 中的一项和 C 中的一项相关。
aid | bid | cid
B
每个项目都与 D 中的多个项目相关。
bid
C
每个项目都与 D 中的多个项目相关。
cid
D
每个项目都与 B 和 C 中的多个项目相关。
did
E
将 B 中的项目映射到 D 中的项目。
bid | did
F
将 C 中的项目映射到 D 中的项目。
cid | did
现在我正在尝试在 MyBatis 中编写一个映射器来映射 table A 中的项目。映射器与 table B 和 C 的映射器以及 table B 和 C 的映射器以及tables B 和 C 都有一个包含 table D.
项的集合
将 B 或 C 单独映射到 table D 的项目不是问题,因为使用 E 或 F 的简单连接操作就足够了。
然而,当试图从 table A 的角度映射所有内容时,问题就出现了。
由于 table A 与 table B 和 C 相关联,并且它们都与 table D 相关联,我不知道如何让 B 和 C 的映射器区分table D 中适合他们的物品。
基本上,我试图从 A 的映射器的角度将 D 连接到 B 和 C。
有没有办法在 MyBatis 中将 table D 连接两次,同时保持两次连接之间的区别,以便 B 和 C 的映射器可以区分两者?
感谢您的帮助!
编辑
这是一个小示例,其中包含更具描述性的名称。
房子
houseid | livingroomid | kitchenid
1 | 1 | 1
客厅
livingroomid
1
厨房
kitchenid
1
家具
furnitureid | furniturename
1 | couch
2 | lamp
3 | fridge
客厅家具
livingroomid | furnitureid
1 | 1
1 | 2
厨房家具
kitchenid | furnitureid
1 | 2
1 | 3
我目前的映射器:
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>
您实际上必须将 D 加入到 table 中,B 和 C 相应地为其别名。
假设 bd table d 加入 B 并且 cd.
现在,如果你想要 table d 的相同结果图,用于别名 table(bd 和 cd),你可以这样做:
select bd.did as bd_did, cd.did as cd_did
如果您还没有遇到 column_prefix
属性 结果图,这就是您的结果图的外观
<collection type="D" resultMap="d" column_prefix="bd_" />
我有 6 个 table 结构如下(底部有一个更具描述性的示例):
一个
每一项都与 B 中的一项和 C 中的一项相关。
aid | bid | cid
B
每个项目都与 D 中的多个项目相关。
bid
C
每个项目都与 D 中的多个项目相关。
cid
D
每个项目都与 B 和 C 中的多个项目相关。
did
E
将 B 中的项目映射到 D 中的项目。
bid | did
F
将 C 中的项目映射到 D 中的项目。
cid | did
现在我正在尝试在 MyBatis 中编写一个映射器来映射 table A 中的项目。映射器与 table B 和 C 的映射器以及 table B 和 C 的映射器以及tables B 和 C 都有一个包含 table D.
项的集合将 B 或 C 单独映射到 table D 的项目不是问题,因为使用 E 或 F 的简单连接操作就足够了。 然而,当试图从 table A 的角度映射所有内容时,问题就出现了。 由于 table A 与 table B 和 C 相关联,并且它们都与 table D 相关联,我不知道如何让 B 和 C 的映射器区分table D 中适合他们的物品。
基本上,我试图从 A 的映射器的角度将 D 连接到 B 和 C。
有没有办法在 MyBatis 中将 table D 连接两次,同时保持两次连接之间的区别,以便 B 和 C 的映射器可以区分两者?
感谢您的帮助!
编辑
这是一个小示例,其中包含更具描述性的名称。
房子
houseid | livingroomid | kitchenid
1 | 1 | 1
客厅
livingroomid
1
厨房
kitchenid
1
家具
furnitureid | furniturename
1 | couch
2 | lamp
3 | fridge
客厅家具
livingroomid | furnitureid
1 | 1
1 | 2
厨房家具
kitchenid | furnitureid
1 | 2
1 | 3
我目前的映射器:
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>
您实际上必须将 D 加入到 table 中,B 和 C 相应地为其别名。
假设 bd table d 加入 B 并且 cd.
现在,如果你想要 table d 的相同结果图,用于别名 table(bd 和 cd),你可以这样做:
select bd.did as bd_did, cd.did as cd_did
如果您还没有遇到 column_prefix
属性 结果图,这就是您的结果图的外观
<collection type="D" resultMap="d" column_prefix="bd_" />