目录
问题、需求
多对一问题:查出每个学生的信息,并 查出他班主任的信息
一对多问题:查出每个老师的信息,并 查出他的所有学生的信息
一对多:一个班主任有很多的学生
多对一:很多学生有一个班主任
数据库设置方法
在多的那一方添加一个字段,这个字段取自于一的那一方表的id
为什么是一的那一方的id而不是name?
这里只能选一的那一方的id,如果你选择了一的那一方的name,那耦合度就很高了,如果班级改名字了,那你就得改两个表,而id就不会出现这种问题,这叫解耦
学生表,需要注意的是:t_id 表示学生对应的班主任

班主任表

解决多对一问题
多对一问题:查出每个学生的信息,并 查出他班主任的信息
实体类的设置方法
学生类:额外加上一个老师类的对象,用来查询这个学生对应的班主任
注意自己加上get、set、toString
private Integer id;
private String Sname;
private String sex;
private Integer age;
private Integer t_id;
//Teacher属性用于查询学生的老师时使用
private Teacher teacher;
班主任类:中规中矩
private Integer id;
private String Tname;
sql语句
下面两种都可以
select * from student left join teacher on student.t_id = teacher.id;
SELECT * FROM student s,teacher t where s.t_id = t.id
业务层
// 查询每个老师的信息以及其对应学生 1
public List<Teacher> getTeacher1();
// 查询每个老师的信息以及对应学生 2
public List<Teacher> getTeacher2();
xml的写法
第一种形式:按照查询嵌套处理
就是将两个查询语句嵌套起来
第二种形式:按照结果嵌套处理
就是对复杂字段进行单独处理
<!--多对一:第1种-->
<!--查询每个学生的对应的老师:方法1-->
<!--第一种: 按照查询嵌套处理-->
<!--
1.查询所有的学生信息
2.根据查询出来的t_id,寻找对应的老师
-->
<!-- 注意学生实体类当中要有老师类的对象才能实现关联查询-->
<!-- resultMap:返回关联的结果映射-->
<select id = "getStudent1" resultMap="StudentTeacher1">
select * from student;
</select>
<!--结果映射集-->
<resultMap id="StudentTeacher1" type="com.qcby.entity.Student">
<result property="id" column="id"/>
<result property="Sname" column="Sname"/>
<result property="sex" column="sex"/>
<result property="age" column="age"/>
<result property="t_id" column="t_id"/>
<!-- 复杂的属性我们需要单独去处理 对象:association 集合:collection -->
<!-- property="teacher" student类当中的关联字段 -->
<!-- column="t_id" 两个表的关联字段-->
<!-- javaType="com.javen.model.Teacher" 为复杂属性设置类类型-->
<!-- select="getTeacher" :调用下一个查询语句 -->
<association property="teacher" column="t_id" javaType="com.qcby.entity.Teacher" select="getTeacher"/>
</resultMap>
<select id="getTeacher" resultType="com.qcby.entity.Teacher">
select * from teacher where id = #{t_id}; <!-- #{id}; 可以写任何东西,因为会自动匹配 t_id -->
</select>
<!--多对一:第2种-->
<!--查询每个学生的对应的老师:方法2-->
<!--第二种: 按照结果嵌套处理-->
<!-- 按照结果嵌套处理-->
<select id="getStudent2" resultMap="StudentTeacher2">
SELECT
*
FROM
student
LEFT JOIN teacher ON student.t_id = teacher.id
</select>
<resultMap id="StudentTeacher2" type="com.qcby.entity.Student">
<result property="id" column="id"/>
<result property="Sname" column="Sname"/>
<result property="sex" column="sex"/>
<result property="age" column="age"/>
<result property="t_id" column="t_id"/>
<association property="teacher" javaType="com.qcby.entity.Teacher">
<result property="id" column="id"/>
<result property="Tname" column="Tname"/>
</association>
</resultMap>
查询结果

解决一对多问题
一对多问题:查出每个老师的信息,并 查出他的所有学生的信息
实体类设置
学生类:中规中矩
private Integer id;
private String Sname;
private String sex;
private Integer age;
private Integer t_id;
班主任类:需要加上一个学生集合
private Integer id;
private String Tname;
//Student集合 用于查询老师的学生时使用
private List<Student> students;
sql语句
下面两种都可以
SELECT * FROM teacher
LEFT JOIN student on student.t_id = teacher.id
SELECT * FROM student s,teacher t where s.t_id = t.id
业务层
//查询每个学生的信息以及其对应的老师:方法1
public List<Student> getStudent1();
//查询每个学生的的信息以及其对应的老师:方法2
public List<Student> getStudent2();
xml写法
第一种形式:按照查询嵌套处理
就是在xml里面嵌套两个查询语句
第二种形式:按照结果嵌套处理
在xml里面,对复杂的属性(这里就是学生的信息是复杂的)单独处理
<!--一对多:第1种-->
<!--查询每个老师有多少学生:第一种-->
<!--第一种:按照查询嵌套处理-->
<select id="getTeacher1" resultMap="TeacherStudent1">
select * from teacher
</select>
<resultMap id="TeacherStudent1" type="com.qcby.entity.Teacher">
<collection property="students" column="id" javaType="ArrayList"
ofType="com.qcby.entity.Student" select="getStudentByTeacherId" />
</resultMap>
<select id="getStudentByTeacherId" resultType="com.qcby.entity.Student">
select * from student where t_id = #{t_id}
</select>
<!--一对多:第2中-->
<!--查询每个老师有多少学生:第二种-->
<!--按照结果进行查询-->
<select id="getTeacher2" resultMap="TeacherStudent2">
SELECT
*
FROM
teacher
LEFT JOIN student ON student.t_id = teacher.id
</select>
<resultMap id="TeacherStudent2" type="com.qcby.entity.Teacher">
<result property="id" column="id"/>
<result property="Tname" column="Tname"/>
<!-- 复杂的属性我么需要单独去处理 对象:association 集合:collection
在集合中的泛型信息,我们使用ofType获取
-->
<collection property="students" ofType="com.qcby.entity.Student">
<result property="Sname" column="Sname"/>
<result property="sex" column="sex"/>
<result property="age" column="age"/>
<result property="t_id" column="t_id"/>
</collection>
</resultMap>
查询结果

8397



被折叠的 条评论
为什么被折叠?



