首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我是mybatis的新手。我想知道一对多关系在mybatis中是如何映射的

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

Stack Overflow用户
提问于 2019-08-20 02:22:02
回答 1查看 80关注 0票数 0

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

Mapper.xml

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-08-20 03:41:40

以下方法可以解决您的问题:

请同时在collection-tag中设置属性javaType="List"

代码语言:javascript
复制
<collection
    property="skills"
    javaType="List"
    ofType="skill"
    resultMap="skillResult"
    columnPrefix="skill_"/>

ofType-property表示Class /Interface的泛型代码;例如List<?>,您将其实现为ArrayList<Skill>,因此javaType必须为ListofType必须为<代码>D12

您在collection-tag中声明了属性columnPrefix,但是您的select语句中有任何以skill_为前缀的列。因此,您必须更改/添加s.skillid as skill_id, s.skillname as skill_name, s.empId as skill_empid之类的内容

代码语言:javascript
复制
<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>

collection-tag中声明的声明的columnPrefix是自动添加的,以解析resultMap

例如,select-语句声明名为/标记为skill_id的列

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

mybatis结合了columnPrefixcolumn-property of id-tags,resulttags,(诸如此类)

columnPrefix="skill_"column="id"成为

运行时的skill_id

代码语言:javascript
复制
<resultMap type="skill" id="skillResult">
    <id
        property="skillId"
        column="id"/>
    <result
        property="skillname"
        column="name"/>
    <result
        property="empId"
        column="empId"/>
</resultMap>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57562074

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档