此示例仅是我的数据库的架构。我有下表叫做食谱:
id | author_name | recipe1_id | recipe2_id | recipe3_id
所有食谱都与recipes_ingredients表有关:
id | recipe_id | ingredient1 | ingredient2 | ingredient3
现在我在recipe1_id上对recipe3_id进行三次JOINS,如下所示:
SELECT recipe1.ingredient1 as recipe1ingredient1, recipe1.ingredient2 as recipe1ingredient2, recipe1.ingredient3 as recipe1ingredient3, [...]
FROM recipes
INNER JOIN recipe recipe1 ON (recipes.recipe1_id = recipes.id)
INNER JOIN recipe recipe2 ON (recipes.recipe2_id = recipes.id)
INNER JOIN recipe recipe3 ON (recipes.recipe3_id = recipes.id)
要将3个食谱映射到MyBatis中的一个bean,我有三个resultsMaps,它们将每个食谱映射到包含3种成分的食谱对象(recipe1,recipe2,recipe3)。
但是:我希望将食谱列入清单。我怎么能实现这一目标?
PS:可以肯定的是,我在表食谱和表recipes_ingredients中只有3个实体。这永远不会改变,所以我没有理由把它放在n:m表中。
答案 0 :(得分:0)
我认为这会奏效。
<resultMap id="recipe1" type="recipe1">
...
</resultMap>
<resultMap id="recipe2" type="recipe2">
...
</resultMap>
<resultMap id="recipe3" type="recipe3">
...
</resultMap>
<select id="getRecipes" parameterType="list" resultMap="recipe1, recipe2, recipe3">
SELECT recipe1.ingredient1 as recipe1ingredient1, recipe1.ingredient2 as recipe1ingredient2, recipe1.ingredient3 as recipe1ingredient3, [...]
FROM recipes
INNER JOIN recipe recipe1 ON (recipes.recipe1_id = recipes.id)
INNER JOIN recipe recipe2 ON (recipes.recipe2_id = recipes.id)
INNER JOIN recipe recipe3 ON (recipes.recipe3_id = recipes.id)
</select>