ibatis支持返回一对多关系的实体类
例如:课程可以有多个讲师
课程类:
public class CourseDto {
/** cc_course.courseId 课程ID */
private Integer courseId;
/** cc_course.courseName 课程标题 */
private String courseName;
private String creUserName;
//讲师信息
private List<Lecturer> lecturer;
}
讲师类:
public class Lecturer{
/** 讲师ID */
public int lecturerId;
/** 讲师姓名 */
public String lecturerName;
public String lecturerType;
}
map写法:
<resultMap id="courseLecturerMap" type="com.pcitc.learn.cc.model.course.CourseDto">
<result column="courseId" property="courseId" jdbcType="INTEGER" />
<result column="courseName" property="courseName" jdbcType="VARCHAR" />
<result column="creUserName" property="creUserName" jdbcType="VARCHAR" />
<collection property="lecturer" column="courseId" ofType="com.pcitc.learn.cc.model.course.Lecturer">
<id property="id" column="lecturerId" javaType="int" jdbcType="INTEGER"/>
<result property="lecturerName" column="lecturerName" javaType="string" jdbcType="VARCHAR"/>
<result property="lecturerType" column="lecturerType" javaType="string" jdbcType="INTEGER"/>
</collection>
</resultMap>
<select id="queryCourseLecturerForPage" parameterType="java.util.Map" resultMap="courseLecturerMap">
SELECT
A.courseId,
A.courseName,
A.creUserName,
B.lecturerId,
B.lecturerName,
B.lecturerType
FROM
cc_course A
LEFT JOIN cc_lecturer B
ON A.courseId = B.courseId
</select>
说明:left join 关系时分页查询不可用。不用分页查询,这种写法是可以的。