我的项目都是关于使用spring-mybatis的crud操作。其中我正在对1:M relationship执行数据库操作,table.Select查询返回空列表。在Employee POJO类中,我有用于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>发布于 2019-08-20 03:41:40
以下方法可以解决您的问题:
请同时在collection-tag中设置属性javaType="List"
<collection
property="skills"
javaType="List"
ofType="skill"
resultMap="skillResult"
columnPrefix="skill_"/>ofType-property表示Class /Interface的泛型代码;例如List<?>,您将其实现为ArrayList<Skill>,因此javaType必须为List,ofType必须为<代码>D12
您在collection-tag中声明了属性columnPrefix,但是您的select语句中有任何以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>在collection-tag中声明的声明的columnPrefix是自动添加的,以解析resultMap
例如,select-语句声明名为/标记为skill_id的列
collection-tag告诉myBatis使用columnPrefix来解析声明的resultMap
mybatis结合了columnPrefix和column-property of id-tags,resulttags,(诸如此类)
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>https://stackoverflow.com/questions/57562074
复制相似问题