我是mybatis的新手。我想知道一对多关系在mybatis中是如何映射的

I am new to mybatis. I want to know how one to many relationship is mapped in mybatis

我的项目都是关于使用spring-mybatis的crud操作。其中我正在对 1:M 关系 table.Select 查询 returns 空列表执行数据库操作。在 Employee POJO class 我有 List skills = new ArrayList();

的 setter 和 getter

Mapper.xml

<resultMap type="employee" id="result">
    <id property="employeeId" column="empId" />
    <result property="firstName" column="firstName" />
    <result property="lastName" column="lastName" />
    <result property="age" column="age" />
    <result property="gender" column="gender" />
    <result property="salary" column="salary" />
    <result property="department" column="department" />
    <result property="state" column="state" />
    <result property="city" column="city" />
    <result property="skillSet" column="skillSet" />
    <result property="address" column="address" />
    <result property="email" column="email" />
    <collection property="skills" ofType="skill" resultMap="skillResult" columnPrefix="skill_"></collection>
</resultMap>

<resultMap type="skill" id="skillResult">
    <id property="skillId" column="skillId"/>
    <result property="skillname" column="skillname"/>
    <result property="empId" column="empId"/>
</resultMap>

<select id="getAllEmployees" resultType="employee" resultMap="result">
    Select e.empid,e.firstname,e.lastname,e.age,e.salary,e.department,e.state,e.city,e.address,e.gender,e.email,s.skillname,s.empId
    from Employee40 e right outer join Skill s on e.empid = s.empid
</select>

以下应该可以解决您的问题:

请同时在 collection-tag

中设置 属性 javaType="List"
<collection
    property="skills"
    javaType="List"
    ofType="skill"
    resultMap="skillResult"
    columnPrefix="skill_"/>

ofType-属性表示Class/Interface的泛型Type;例如 List<?>,您将其实现为 ArrayList<Skill>,因此 javaType 必须是 ListofType 必须是 skill

您在 collection- 标签中声明了 属性 columnPrefix,但是您的 select- 语句有任何列以 skill_ 为前缀。所以你必须 change/add 类似 s.skillid as skill_id, s.skillname as skill_name, s.empId as skill_empid

<select id="getAllEmployees" resultType="employee" resultMap="result">
    Select
        e.empid,
        e.firstname,
        e.lastname,
        e.age,
        e.salary,
        e.department,
        e.state,
        e.city,
        e.address,
        e.gender,
        e.email,
        s.skillid   as skill_id,
        s.skillname as skill_name,
        s.empId     as skill_empid
    from
        Employee40 e
    right outer join
        Skill s
    on e.empid = s.empid
</select>

声明的columnPrefix,在collection-tag中声明的是自动'added',解析resultMap

例如,select-语句声明了一个列 named/labled skill_id

collection-tag 告诉 myBatis 使用一个 columnPrefix 来解析声明的 resultMap

mybatis结合了columnPrefixcolumn-属性的id-tags,resulttags,(whatever)

columnPrefix="skill_"column="id" 变成

skill_id 运行时

<resultMap type="skill" id="skillResult">
    <id
        property="skillId"
        column="id"/>
    <result
        property="skillname"
        column="name"/>
    <result
        property="empId"
        column="empId"/>
</resultMap>