MyBatis 快速学习04:ResultMap映射及分页插件的使用
目录
创建数据库
CREATE TABLE `teacher` (
`id` INT(10) NOT NULL,
`name` VARCHAR(30) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8
INSERT INTO teacher(`id`, `name`) VALUES (1, '阿大');
INSERT INTO teacher(`id`, `name`) VALUES (2, '二郎神');
INSERT INTO teacher(`id`, `name`) VALUES (3, '三圣母');
INSERT INTO teacher(`id`, `name`) VALUES (4, '四公主');
CREATE TABLE `student` (
`id` INT(10) NOT NULL,
`name` VARCHAR(30) DEFAULT NULL,
`tid` INT(10) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `fktid` (`tid`),
CONSTRAINT `fktid` FOREIGN KEY (`tid`) REFERENCES `teacher` (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8
INSERT INTO `student` (`id`, `name`, `tid`) VALUES ('1001', '张龙', '1');
INSERT INTO `student` (`id`, `name`, `tid`) VALUES ('1002', '韦琪', '2');
INSERT INTO `student` (`id`, `name`, `tid`) VALUES ('1003', '占城', '3');
INSERT INTO `student` (`id`, `name`, `tid`) VALUES ('1004', '梁瑞', '4');
INSERT INTO `student` (`id`, `name`, `tid`) VALUES ('1005', '黄磊', '2');
多对一关联
多个学生对应一个老师
如果对于学生这边,就是一个多对一的现象,即从学生这边关联一个老师!
实体类:
@Data
public class Student {
private int id;
private String name;
//多个学生可以是同一个老师,即:多对一
private Teacher teacher;
}
子查询--按查询嵌套处理
接口方法:
public interface StudentDAO {
//获取所有学生及对应老师的信息
public List<Student> queryAllStudent();
}
编写mapper文件:
<!--子查询
思路:
1. 获取所有学生的信息
2. 根据获取的学生信息的老师ID->获取该老师的信息-->
<select id="queryAllStudent" resultMap="StudentTeacher">
select * from student
</select>
<resultMap id="StudentTeacher" type="com.qgc.pojo.Student">
<result property="id" column="id"></result>
<result property="name" column="name"></result>
<!-- 复杂属性需要单独处理 对象:association 集合:collection-->
<association property="teacher" column="tid" javaType="com.qgc.pojo.Teacher" select="queryAllTeacher">
</association>
</resultMap>
<select id="queryAllTeacher" resultType="com.qgc.pojo.Teacher">
select * from teacher where id=#{tid}
</select>
注册mapper文件:
<!-- 注册mapper文件 -->
<mappers>
<mapper resource="mappers/UserMapper.xml"></mapper>
<mapper resource="mappers/StudentMapper.xml"></mapper>
</mappers>
测试:
@Test
public void queryAllStudent() {
SqlSession sqlSession = MyBatisUtil.getSqlSession();
StudentDAO studentDAO = sqlSession.getMapper(StudentDAO.class);
List<Student> students = studentDAO.queryAllStudent();
for (Student student : students) {
System.out.println("学生:"+student.getName()+"\t老师:"+student.getTeacher().getName());
}
sqlSession.close();
}
联表查询--按结果嵌套处理
接口方法:
public interface StudentDAO {
//获取所有学生及对应老师的信息
public List<Student> queryAllStudent();
public List<Student> queryAllStudent2();
}
编写mapper文件:
<!-- 方法二 -->
<select id="queryAllStudent2" resultMap="StudentTeacher2">
select s.id sid,s.name sname,t.id tid,t.name tname
from student s,teacher t
where s.tid=t.id
</select>
<resultMap id="StudentTeacher2" type="com.qgc.pojo.Student">
<result property="id" column="sid"></result>
<result property="name" column="sname"></result>
<association property="teacher" javaType="com.qgc.pojo.Teacher">
<result property="id" column="tid"></result>
<result property="name" column="tname"></result>
</association>
</resultMap>
注册mapper文件:
<!-- 注册mapper文件 -->
<mappers>
<mapper resource="mappers/UserMapper.xml"></mapper>
<mapper resource="mappers/StudentMapper.xml"></mapper>
</mappers>
测试:
@Test
public void queryAllStudent2() {
SqlSession sqlSession = MyBatisUtil.getSqlSession();
StudentDAO studentDAO = sqlSession.getMapper(StudentDAO.class);
List<Student> students = studentDAO.queryAllStudent2();
for (Student student : students) {
System.out.println("学生:"+student.getName()+"\t老师:"+student.getTeacher().getName());
}
sqlSession.close();
}
一对多关联
多个学生对应一个老师
如果对于学生这边,就是一个多对一的现象,即从学生这边关联一个老师!
实体类:
@Data
public class Student {
private int id;
private String name;
//多个学生可以是同一个老师,即:多对一
private Teacher teacher;
}
子查询--按查询嵌套处理
接口方法:
public interface TeacherDAO {
//获取指定老师及老师下的学生
public Teacher getTeacher(int id);
}
编写mapper文件:
<!-- 思路:
1、从学生表和老师表中查询学出学生id 学生姓名,老师姓名
2、对查询出来的操作做结果集映射
集合 用collection JavaType 和 ofType 都是依赖指定对象类型的
JavaType指定pojo中的属性类型
ofType指定映射到List集合属性中的pojo类型 -->
<!-- 子查询:按查询嵌套 -->
<select id="getTeacher2" resultMap="TeacherStudent2">
select * from teacher where id=#{id}
</select>
<resultMap id="TeacherStudent2" type="com.qgc.pojo.Teacher">
<collection property="students" javaType="ArrayList" ofType="com.qgc.pojo.Student" column="id" select="getStudentByTeacherId"></collection>
</resultMap>
<select id="getStudentByTeacherId" resultType="com.qgc.pojo.Student">
select * from student where tid=#{id}
</select>
注册mapper文件:
<!-- 注册mapper文件 -->
<mappers>
<mapper resource="mappers/UserMapper.xml"></mapper>
<mapper resource="mappers/StudentMapper.xml"></mapper>
<mapper resource="mappers/TeacherMapper.xml"></mapper>
</mappers>
测试:
@Test
public void queryAllTeacher() {
SqlSession sqlSession = MyBatisUtil.getSqlSession();
TeacherDAO teacherDAO = sqlSession.getMapper(TeacherDAO.class);
Teacher teacher = teacherDAO.getTeacher(1);
System.out.println(teacher.getName());
System.out.println(teacher.getStudents());
sqlSession.close();
}
联表查询--按结果嵌套处理
接口方法:
public interface TeacherDAO {
//获取指定老师及老师下的学生
public Teacher getTeacher(int id);
public Teacher getTeacher2(int id);
}
编写mapper文件:
<!-- 联表查询:按结果嵌套 -->
<select id="getTeacher" resultMap="TeacherStudent">
select s.id sid,s.name sname,t.id tid,t.name tname
from teacher t,student s
where s.tid=t.id and t.id=#{id}
</select>
<resultMap id="TeacherStudent" type="com.qgc.pojo.Teacher">
<id property="id" column="id"></id>
<result property="name" column="tname"></result>
<collection property="students" ofType="com.qgc.pojo.Student">
<result property="id" column="sid"></result>
<result property="name" column="sname"></result>
<result property="tid" column="tid"></result>
</collection>
</resultMap>
注册mapper文件:
<!-- 注册mapper文件 -->
<mappers>
<mapper resource="mappers/UserMapper.xml"></mapper>
<mapper resource="mappers/StudentMapper.xml"></mapper>
<mapper resource="mappers/TeacherMapper.xml"></mapper>
</mappers>
测试:
@Test
public void queryAllTeacher2() {
SqlSession sqlSession = MyBatisUtil.getSqlSession();
TeacherDAO teacherDAO = sqlSession.getMapper(TeacherDAO.class);
Teacher teacher = teacherDAO.getTeacher2(1);
System.out.println(teacher.getName());
System.out.println(teacher.getStudents());
sqlSession.close();
}
总结
按查询嵌套--子查询
按结果嵌套--联表查询
association--关联属性,用于 一对一,多对一
collection--集合用于 一对多
JavaType指定pojo中的属性类型
ofType指定映射到List集合属性中的pojo类型