它是映射集的引用,将执行强大的映射功能,resultMap 可以给予我们自定义映射规则的机会
它是 MyBatis 最复杂的元素,可以配置映射规则,级联,typeHandler等
1、Java实体类
public class Group implements Serializable {
private String id;//主键id
private String gname;//小组名称
private Integer gnum;//成员数量
private List<Student> studentList;//小组成员
}
public class Student implements Serializable {
private String id;//主键id
private String name;//学生姓名
private String age;//学生年龄
private String gid;//组外键
}
2、mysql语句如下:
<resultMap id="astChecksPlanMap" type="org.modules.ast.entity.Group">
<result column="id" property="id" jdbcType="VARCHAR"/>
<result column="name" property="gname" jdbcType="VARCHAR"/>
<result column="num" property="gnum" jdbcType="Integer"/>
<collection property="studentList" column="id" select="findStudentList"></collection>
</resultMap>
<select id="findStudentList" resultType="org.modules.ast.entity.Student">
SELECT
id,
name,
age,
gid
FROM
student
where
gid = #{id}
</select>
<select id="selectMyPagg" resultMap="astChecksPlanMap">
SELECT
id,
name,
num
FROM
group
</select>
3、返回的结果如下
{
id:"121"
name:"飞虎队"
num:"3"
studentList:{
[id:"001,name:"张三",age:"12",gid:"121"]
[id:"002,name:"李四",age:"11",gid:"121"]
[id:"003,name:"王五",age:"10",gid:"121"]
}
}