javatype指定的是对象的属性的类型,而oftype指定的是映射到t集合属性中pojo的类型
package com.test.dao;
public class Student {
private Integer id;
private String name;
public Integer getId() {
return id;
}
//省略settergetter
}
package com.test.dao;
public class School{
private Integer id;
private String name;
//省略settergetter
}
package com.test.dao;
public class Teacher{
private Integer id;
private String name;
private Integer schoolId;
private Integer studentId;
private School school;
private List<Student> studentList;
//省略settergetter
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="tbl_teacher" >
<resultMap id="BaseResultMap" type="com.test.dao.Teacher" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="name" property="name" jdbcType="VARCHAR" />
<result column="school_id" property="schoolId" jdbcType="INTEGER" />
<result column="student_id" property="studentId" jdbcType="INTEGER" />
<!--javatype指定的是Teacher对象的属性的类型(例如id,name,school,studentList),而oftype指定的是映射到list集合属性中pojo的类型(本例List<Student>指的是Student类型)-->
<association property="school" column="school_id" javaType="com.test.dao.school" select="tbl_school.selectByPrimaryKey"></association>
<collection property="studentList" ofType="com.test.dao.Student" column="student_id" select="tbl_studnt.selectByPrimaryKey"></collection>
</resultMap>