MyBatis集合的映射不起作用

时间:2018-10-21 19:11:40

标签: mybatis

我对MyBatis设置String类型的集合有疑问。

我有以下2个Pojo课程:

public class Person {

    private long personId;
    private String lastName;
    private String firstName;
    private Set<String> belongings;

    public Person(String lastName, String firstName) {
        this.lastName = lastName;
        this.firstName = firstName;
    }

    public Person(Long personId, String lastName, String firstName) {
        this(lastName, firstName);
        this.personId = personId;
    }

    // Getters & Setters
}

public class Mariage {

    private final Person husband;
    private final Person wife;

    public Mariage(Person husband, Person wife) {
        this.husband = husband;
        this.wife = wife;
    }

    // Getters & Setters
}

以及以下MyBatis xml代码:

<resultMap type="Mariage" id="mariageResultMap">
    <constructor>
        <idArg javaType="Person" resultMap="husbandResultMap"/>
        <idArg javaType="Person" resultMap="wifeResultMap"/>
    </constructor>
</resultMap>

<resultMap type="Person" id="husbandResultMap">
    <constructor>
        <idArg column="h_person_id" javaType="long"/>
        <arg column="h_last_name" javaType="string"/>
        <arg column="h_first_name" javaType="string"/>
    </constructor>
    <collection property="belongings" column="h_belonging" ofType="string"/>
</resultMap>

<resultMap type="Person" id="wifeResultMap">
    <constructor>
        <idArg column="w_person_id" javaType="Long"/>
        <arg column="w_last_name" javaType="string"/>
        <arg column="w_first_name" javaType="string"/>
    </constructor>
    <collection property="belongings" column="w_belonging" ofType="string"/>
</resultMap>

<insert id="savePerson">
    <selectKey keyProperty="personId" resultType="long" order="BEFORE">
        SELECT nextval('persons_person_id_seq')
    </selectKey>
    INSERT INTO persons(person_id, last_name, first_name)
    VALUES(#{personId}, #{lastName}, #{firstName} );

    INSERT INTO belongings(person_id, name) values
    <foreach collection="belongings" item="name" separator=", " close=";"> 
        (#{personId}, #{name})
    </foreach>
</insert>

<insert id="saveMariage" parameterType="Mariage"> 
    INSERT INTO mariages(husband_id
        , wife_id)
    VALUES(#{husband.personId}
        , #{wife.personId});
</insert>

<select id="getMariages" resultMap="mariageResultMap">
    SELECT h.person_id h_person_id
        , h.last_name h_last_name
        , h.first_name h_first_name
        , bh.name h_belonging
        , w.person_id w_person_id
        , w.last_name w_last_name
        , w.first_name w_first_name
        , bw.name w_belonging
    FROM mariages m 
    JOIN persons h ON m.husband_id = h.person_id 
    JOIN persons w ON m.wife_id = w.person_id 
    JOIN belongings bh ON bh.person_id = h.person_id 
    JOIN belongings bw ON w.person_id = bw.person_id
    ORDER BY h_last_name
        , h_first_name
        , w_last_name
        , w_first_name
        , h_belonging
        , w_belonging;
</select>

挽救人(丈夫和妻子)以及婚姻没有问题。它们很好地保存在数据库中。 但是,从数据库中检索杂物是个问题。 我确实与丈夫和妻子的婚姻有了正确的名字,但是他们的财产(字符串类型的集合)没有初始化。 在日志中,我可以看到查询getMariages正在执行。 执行此查询将给出(与先前插入的人员,财产和航海物一起)给出:

h_person_id | h_last_name | h_first_name | h_belonging | w_person_id | w_last_name | w_first_name | w_belonging 
------------+-------------+--------------+-------------+-------------+-------------+--------------+-------------
         13 | Doe         | John         | Chicken      |          14 | Smith       | Jane        | Cat
         13 | Doe         | John         | Chicken      |          14 | Smith       | Jane        | Parrot
         13 | Doe         | John         | Dog          |          14 | Smith       | Jane        | Cat
         13 | Doe         | John         | Dog          |          14 | Smith       | Jane        | Parrot
         13 | Doe         | John         | Goldfish     |          14 | Smith       | Jane        | Cat
         13 | Doe         | John         | Goldfish     |          14 | Smith       | Jane        | Parrot
         13 | Doe         | John         | Lizzard      |          14 | Smith       | Jane        | Cat
         13 | Doe         | John         | Lizzard      |          14 | Smith       | Jane        | Parrot
         13 | Doe         | John         | Turtle       |          14 | Smith       | Jane        | Cat
         13 | Doe         | John         | Turtle       |          14 | Smith       | Jane        | Parrot
         11 | Johnson     | Charles      | Bed          |          12 | Peterson    | Jill        | Car
         11 | Johnson     | Charles      | Bed          |          12 | Peterson    | Jill        | House
         11 | Johnson     | Charles      | Chair        |          12 | Peterson    | Jill        | Car
         11 | Johnson     | Charles      | Chair        |          12 | Peterson    | Jill        | House
         11 | Johnson     | Charles      | Table        |          12 | Peterson    | Jill        | Car
         11 | Johnson     | Charles      | Table        |          12 | Peterson    | Jill        | House

出了什么问题? h_belonging列在丈夫结果映射中映射到集合所有物:

    <collection property="belongings" column="h_belonging" ofType="string"/>

0 个答案:

没有答案
相关问题