spring-mvc - 我是mybatis的新手。我想知道mybatis中是如何映射一对多关系的
问题描述
我的项目都是关于使用 spring-mybatis 的 crud 操作。我在 1:M 关系表上执行数据库操作。选择查询返回空列表。在 Employee POJO 类中,我有 List 技能的 setter 和 getter = new ArrayList();
映射器.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>
解决方案
以下应该可以解决您的问题:
javaType="List"
还请在collection
-tag中设置属性
<collection
property="skills"
javaType="List"
ofType="skill"
resultMap="skillResult"
columnPrefix="skill_"/>
ofType
-property 表示Type
a Class
/的泛型Interface
;例如List<?>
,您将其实现为ArrayList<Skill>
, so javaType
has to be List
and ofType
must beskill
columnPrefix
您在collection
-tag中声明了该属性,但您的 select-statement 有任何以 . 为前缀的列skill_
。所以你必须改变/添加类似的东西 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
在-tag 中声明的声明collection
是自动“添加”的,以解决resultMap
例如,select-statement 声明了一个名为/labeled 的列skill_id
collection
-tag 告诉 myBatis 使用 acolumnPrefix
来解析声明的resultMap
mybatis 结合了-tags, tags, (whatever)columnPrefix
的column
-propertyid
result
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>
推荐阅读
- python - Python 编程路障类型错误:'pygame.Surface' 对象不可调用 如何修复
- node.js - 在发送 POST 之前与远程服务器协商最大请求正文大小
- vba - 如果 Outlook 关闭,则从 C# OOM 发送的电子邮件将保留在发件箱中,直到下一次 Outlook 启动
- android - 如何在 Android 上应用淡出“EditText”文本的遮罩?
- sql - 将数据移动到正确的记录
- ruby-on-rails - Arel - 如何在 Arel 查询中合并字段和字符串文字?
- jquery - 如何将表信息保存到 localStorage?
- sitefinity - 以编程方式创建 sitefinity(自定义模块)内容副本的正确方法是什么?
- wpf - 在 CodeBehind 中更新 FlowDocument 的文本
- asp.net-core - 是否有可能找到有关调用您的 api 的计算机/用户的任何信息?如果是这样,怎么做?