mybatis一对一和一对多实例
一对一:<association></association>
一对多:<collection></collection>
ClassMapper.xml:
<resultMap type="com.taobaoo.school.domain.edu.XjClass" id="classMap">
<id property="id" column="class_id"/>
<result property="className" column="class_name"/>
<association property="school" javaType="com.taobaoo.school.domain.edu.School">
<id property="id" column="school_id"/>
<result property="schoolName" column="school_name"/>
</association>
<association property="grade" javaType="com.taobaoo.school.domain.edu.Grade">
<id property="id" column="grade_id"/>
<result property="gradeName" column="grade_name"/>
</association>
<association property="classTea" javaType="com.taobaoo.school.domain.edu.Teacher">
<id property="id" column="class_teacher_id"/>
<result property="name" column="class_teacher_name"/>
</association>
<collection property="teaList" ofType="com.taobaoo.school.domain.edu.Teacher">
<id property="id" column="sub_teacher_id"/>
<result property="name" column="sub_teacher_name"/>
<result property="subjectName" column="sub_name"/>
</collection>
</resultMap>
<select id="queryClass" resultMap="classMap">
select
cls.id AS class_id,
cls.class_name AS class_name,
school.id AS school_id,
school.school_name AS school_name,
grade.id AS grade_id,
grade.grade_name AS grade_name,
teacher.id AS class_teacher_id,
teacher.name AS class_teacher_name
from class cls
join grade on cls.grade_id=grade.id
join school on cls.school_id=school.id
join teacher on cls.teacher_id=teacher.id
</select>
Class.java:
private Long id;
private Grade grade; //1对1
private Teacher teacher; //1对1
Private List<Teacher> teaList; //1对多
//get/set省略...
感谢您的阅读,您的支持是我写博客动力。