Mybatis对象映射列表

Mybatis mapping list of objects

我有以下两个类

public class User {
    private String id;
    private String name;
    private List<Account> accounts
}

public class Account {
    private String id;
}

我有如下mybatis resultMap

<resultMap id="User" type="User">
    <result property="id" column="id"/>
    <result property="name" column="name"/>
    <collection property="accounts" ofType="Account">
        <result property="id" column="accont_id"/>
    </collection>
</resultMap>

我有两个表 useruser_account

如何使用 myBatis 获得一个包含所有字段的用户

您需要使用 id 标签而不是 result 标签来标记父对象中的 id 属性。

<resultMap id="User" type="User">
    <id property="id" column="id"/>
    <result property="name" column="name"/>
    <collection property="accounts" ofType="Account">
        <result property="id" column="accont_id"/>
    </collection>
</resultMap>