这里以学生与老师关系做‘多对一’和‘一对多’的案例
目录
多对一查询
实体类
//学生实体类
@Data
public class Student {
private int id;
private String name;
//学生需要关联一个老师!
private Teacher teacher;
}
//学生接口
public interface StudentMapper {
//查询所有学生的信息以及对应老师的信息
List<Student> getStudent();
List<Student> getStudent2();
}
//老师实体类
@Data
public class Teacher {
private int id;
private String name;
}
MyBatis的xml映射查询
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.mao.dao.StudentMapper">
<!--多对一 安装结果嵌套处理 联表查询-->
<select id="getStudent2" resultMap="StudentTeacher2">
select s.id sid,s.name sname,t.id tid,t.name tname from student s join teacher t on s.tid = t.id
</select>
<resultMap id="StudentTeacher2" type="Student">
<result property="id" column="sid" />
<result property="name" column="sname"/>
<!--复杂的属性。需要单独处理 对象:association 集合:collection
property 对实体类属性 ;
javaType 是property对应的属性数据类型。
这里javaType指定数据类型为老师类,相当于new一个老师对象,然后把查出来的数据给这个对象属性赋值,
这样就获得一个对象了,再把这个对象赋值给学生对象的老师属性
-->
<association property="teacher" javaType="Teacher">
<result property="name" column="tname"/>
<result property="id" column="tid"/>
</association>
</resultMap>
<!--=========================================-->
<!--多对一 多表查询方式一 子查询-->
<select id="getStudent" resultMap="StudentTeacher">
select * from student
</select>
<resultMap id="StudentTeacher" type="Student">
<result property="id" column="id" />
<result property="name" column="name" />
<!--复杂的属性。需要单独处理 对象:association 集合:collection
property 对实体类属性 ;
column 对数据库字段
javaType 是property对应的属性数据类型
select 子查询的id
-->
<association property="teacher" column="tid" javaType="Teacher" select="getTeacher" />
</resultMap>
<select id="getTeacher" resultType="Teacher">
select * from teacher where id = #{tid}
</select>
</mapper>
一对多查询
实体类
//老师实体类
@Data
public class Teacher {
private int id;
private String name;
//一个老师有多个学生 老师有一个学生集合
private List<Student> student;
}
//老师接口
public interface TeacherMapper {
//获取老师
// List<Teacher> getTeacher();
//获取指定老师下的所有学生及老师信息
Teacher getTeacher(@Param("tid") int id);
Teacher getTeacher2(@Param("tid") int id);
}
//学生实体类
@Data
public class Student {
private int id;
private String name;
private int tid;
}
MyBatis的xml映射查询
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.mao.dao.TeacherMapper">
<!-- 一对多 按结果嵌套查询 联表查询-->
<select id="getTeacher" resultMap="TeacherStudent">
select s.id sid,s.name sname,t.name tname,t.id tid
from student s,teacher t
where s.tid = t.id
and t.id = #{tid};
</select>
<resultMap id="TeacherStudent" type="Teacher">
<result property="id" column="tid"/>
<result property="name" column="tname"/>
<!-- 复杂属性 需要单独处理 对象:association 集合:collection
javaType="" 指定属性的类型
集合中的泛型信息,我们使用ofType获取
-->
<collection property="student" ofType="Student">
<result property="id" column="tid"/>
<result property="name" column="sname"/>
<result property="tid" column="tid"/>
</collection>
</resultMap>
<!--======================================-->
<!-- 一对多 按照查询嵌套处理 子查询-->
<select id="getTeacher2" resultMap="TeacherStuden2" >
select * from teacher where id = #{tid}
</select>
<resultMap id="TeacherStuden2" type="Teacher">
<!--
javaType指定数据类型 =>对应老师实体类的student属性数据类型
ofType 指定对象类型,对应约束集合的泛型类型
select 对应子查询的id值
column="id" 意思就是给子查询的参数,子查询需要的参数就是获取这个值
-->
<collection property="student" javaType="ArrayList" ofType="Student" select="getStudentById" column="id" />
</resultMap>
<select id="getStudentById" resultType="Student">
select * from student where tid = #{tid}
</select>
</mapper>