您的位置:首页 > 其它

Mybatis学习笔记-一对多关联

2016-02-04 11:03 399 查看
需求:根据classId查询对应的班级信息,包括学生,老师
Student实体类
[code=java;toolbar:false">public class Student {
         private int id;
         private String name;
         //...
}public class Student {
private int id;
private String name;
//...
}<select id="getClass2" resultMap="getClass2Map">
select * from class where c_id=#{id}
</select>
<select id="getTeacher" resultType="com.mybatis.test04.Teacher">
select t_id id, t_name name from teacher where t_id=#{id}
</select>
<select id="getStudent" resultType="com.mybatis.test04.Student">
select s_id id, s_name name from student where class_id=#{id}
</select>
<resultMap type="com.mybatis.test04.Classes" id="getClass2Map">
<id property="id" column="c_id"/>
<result property="name" column="c_name"/>
<association property="teacher" column="teacher_id" select="getTeacher"></association>
<collection property="students" column="c_id" select="getStudent"></collection>
</resultMap>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: