1.实体类
package com.yw.test09;
public class Student
{
private int s_id;
private String s_name;
private int age;
private int c_id;
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
public int getC_id()
{
return c_id;
}
public void setC_id(int c_id)
{
this.c_id = c_id;
}
public int getS_id()
{
return s_id;
}
public void setS_id(int s_id)
{
this.s_id = s_id;
}
public String getS_name()
{
return s_name;
}
public void setS_name(String s_name)
{
this.s_name = s_name;
}
@Override
public String toString()
{
return "Student [s_id=" + s_id + ", s_name=" + s_name + ", age=" + age + ", c_id=" + c_id + "]";
}
}
Class班级的实体类不变
2.建立映射文件
<?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="com.yw.test09.ClassMapper">
<resultMap id="classResult" type="com.yw.test09.Class">
<id property="id" column="id" />
<result property="name" column="name" />
<collection property="students" ofType="com.yw.test09.Student" javaType="List" >
<id property="s_id" column="s_id" />
<result property="s_name" column="s_name" />
<result property="age" column="age" />
<result property="c_id" column="c_id" />
</collection>
</resultMap>
<select id="selectClass" resultMap="classResult">
select c.id,c.name,s.id as s_id,s.name as s_name,s.age,s.c_id from class c join student s on c.id=s.c_id where c.id=#{id}
</select>
</mapper>
3.测试类一样
4.效果如下