如何在Mybatis(Java)中的实例映射中制作两个相同的内部类

问题描述 投票:0回答:0

我正在使用

Hand
类来保存数据。它包括两个相同的内部类
Card
,这意味着两个
Card
组成一个
Hand
。如下:

public class Card {
    private Integer id;
    private Integer number;
    private Integer color;

    public Card() {}
    public Card(int number, int color) {
        this.number = number;
        this.color = color;
    }
public class Hand {

    private Integer id;

    private Card card1,card2;

    public Hand() {}
    public Hand(Card a, Card b) {
        card1 = a;
        card2 = b;
    }
}

然后我像这样制作映射xml:

    <resultMap id="handMap" type="com.company.handsmap.Hand">
        <id column="id" property="id"/>
        <association property="card1" resultMap="cardMap"/>
        <association property="card2" resultMap="cardMap"/>
    </resultMap>

    <resultMap id="cardMap" type="com.company.handsmap.Card">
        <id column="id" property="id"/>
        <result column="number" property="number"/>
        <result column="color" property="color"/>
    </resultMap>

    <select id="getAllHand" resultMap="handMap">
        SELECT
        *
        FROM Hand H
        LEFT JOIN Card c1 ON H.card1_id = c1.id
        LEFT JOIN Card c2 ON H.card2_id = c2.id
    </select>

然后我使用映射如下,它似乎出错了: Debugging the bug

因为它表明

Hand
得到了正确的
Card
1但是在
null
2
中得到了
Card

如果我跑

SELECT
        *
        FROM Hand H
        LEFT JOIN Card c1 ON H.card1_id = c1.id
        LEFT JOIN Card c2 ON H.card2_id = c2.id

在数据库中,我可以得到如下正确的结果: SQL results

有人能告诉我问题出在哪里,我该如何解决?非常感谢!

java mysql mybatis
© www.soinside.com 2019 - 2024. All rights reserved.