Issue
My project is all about crud operation using spring-mybatis. In which i am performing database operation on 1:M relationship table.Select query returns empty list. In Employee POJO class i have setters and getters for List skills = new ArrayList();
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>
Solution
the following should solve your problem(s):
please also set property javaType="List"
in collection
-tag
<collection
property="skills"
javaType="List"
ofType="skill"
resultMap="skillResult"
columnPrefix="skill_"/>
the ofType
-property represents the generic Type
of a Class
/ Interface
; for example List<?>
, you implement it as an ArrayList<Skill>
, so javaType
has to be List
and ofType
has to be skill
you declared the property columnPrefix
in the collection
-tag, but your select-statement has any columns prefixed with skill_
. so you have to change/add something like 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>
the declared columnPrefix
, declared in collection
-tag is 'added' automatically, to resolve the resultMap
for example, the select-statement declares a column named/labled skill_id
collection
-tag tells myBatis to use a columnPrefix
to resolve the declared resultMap
mybatis combines columnPrefix
and the column
-property of id
-tags, result
tags, (whatever)
columnPrefix="skill_"
and column="id"
become
skill_id
at runtime
<resultMap type="skill" id="skillResult">
<id
property="skillId"
column="id"/>
<result
property="skillname"
column="name"/>
<result
property="empId"
column="empId"/>
</resultMap>
Answered By - user4602302