-
环境准备
- 数据库创建:一个teacher表,字段有id(主键),name,一个student表,字段为id(主键),name,tid(外键)
- 建表语句:
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, '王老师'); 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 (1, '小明', '1'); INSERT INTO `student` (`id`, `name`, `tid`) VALUES (2, '小红', '1'); INSERT INTO `student` (`id`, `name`, `tid`) VALUES (3, '小张', '1'); INSERT INTO `student` (`id`, `name`, `tid`) VALUES (4, '小李', '1'); INSERT INTO `student` (`id`, `name`, `tid`) VALUES (5, '小王', '1');
- 与表对应的实体类准备:
//Teacher类 public class Teacher { private Integer id; private String name; //...构造器、get、set方法等 } //Student类 public class Student { private Integer id; private String name; private Teacher teacher; //...构造器、get、set方法等 }
- 创建对应的mapper接口和mapper.xml文件,并在核心文件中配置mapper映射
-
进行查询(学生相对于老师是多对一的关系)
- 按照查询嵌套处理:
//接口StudentMapper的处理 public interface StudentMapper { //查询所有的学生信息,以及对应的老师的信息 public List<Student> getStudent(); }
- StudentMapper.xml文件中写sql
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.test.dao.StudentMapper"> <!-- 思路:类似子查询 1.查询所有的学生信息 2.根据查询出来的学生的tid,寻找对应的老师 --> <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--> <association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/> </resultMap> <select id="getTeacher" resultType="Teacher"> select * from teacher where id = #{id}; </select> </mapper>
- 按照结果嵌套处理:连表查询的思路
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.test.dao.StudentMapper"> <!--按照结果嵌套处理--> <select id="getStudent" resultMap="StudentTeacher"> 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="StudentTeacher" type="Student"> <result property="id" column="sid"/> <result property="name" column="sname"/> <association property="teacher" javaType="Teacher"> <result property="name" column="tname"/> <result property="id" column="tid"/> </association> </resultMap> </mapper>
-
老师相对于学生是一对多的关系
- 实体类的编写
@Data public class Student { private int id; private String name; private int tid; } @Data public class Teacher { private int id; private String name; private List<Student> students; }
- 编写mapper.xml中的sql语句
<?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="com.test.dao.TeacherMapper"> <!--按结果嵌套查询--> <select id="getTeacher" resultMap="TeacherStudent"> select s.id sid, s.name sname, t.id tid, t.name tname 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"/> <!--ofType="" 用于获取集合中泛型的信息--> <collection property="students" ofType="Student"> <result property="id" column="sid"/> <result property="name" column="sname"/> <result property="tid" column="tid"/> </collection> </resultMap> <!--嵌套表查询(子查询)--> <select id="getTeacher1" resultMap="TeacherStudent1"> select * from teacher where id=#{tid} </select> <resultMap id="TeacherStudent1" type="Teacher"> <result property="id" column="id"/> <collection property="student" javaType="ArrayList" ofType="Student" select="getStudetByID" column="id"/> </resultMap> <select id="getStudetByID" resultType="Student"> select * from student where tid=#{tid} </select> </mapper>