这里从学生的角度来说就是多对一的场景
那么在Java中是怎么样的呢?
CREATE TABLE `student`( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, `name` VARCHAR(20) DEFAULT NULL )ENGINE=INNODB DEFAULT CHARSET=utf8; INSERT INTO student VALUES (1,"李老师"); CREATE TABLE `student` ( `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY, `name` VARCHAR(20) DEFAULT NULL , `tid` INT(4) NOT NULL, KEY `fk_tid` (`tid`), CONSTRAINT fk_tid FOREIGN KEY(`tid`) REFERENCES `teacher`(`id`) INSERT INTO student VALUES (1,"小红",1), (2,"小绿",1), (3,"小灯",1), (4,"小勿",1), (5,"小闯",1);
Student
package pojo; import lombok.Data; @Data public class Student { private int id; private String name; private Teacher teacher;//这里因为我们有一个对应的关系,学生要对应到一个老师,所以要使用Teacher类型 }
Teacher
package pojo; import lombok.Data; @Data public class Teacher { private int id; private String name; }
(因为这里是多对一,所以我就没有用到TeacherMapper)
StudentMapper
package dao; import pojo.Student; import java.util.List; public interface StudentMapper { List<Student> getStudents(); List<Student> getStudents2(); }
TeacherMapper
package dao; public interface TeacherMapper { }
TeacherMapper.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="dao.TeacherMapper"> </mapper>
StudentMapper.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="dao.StudentMapper"> <!-- 第二种 --> <select id="getStudents2" 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="Student" > <result column="sid" property="id" /> <result column="sname" property="name" /> <association property="teacher" javaType="Teacher"> <result property="id" column="tid" /> <result property="name" column="tname" /> </association> </resultMap> <!-- 第一种 --> <select id="getStudents" resultMap="StudentTeacher"> select * from student; </select> <resultMap id="StudentTeacher" type="Student" > <result column="id" property="id" /> <result column="name" property="name" /> <association property="teacher" column="tid" javaType="Teacher" select="getTeachers" /> </resultMap> <select id="getTeachers" resultType="Teacher"> select * from teacher where id=#{id}; </select> </mapper>
这里说明一下
然后这里有两种方法,我们一种一种看
<?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="dao.StudentMapper"> <!-- 第一种 --> <select id="getStudents" resultMap="StudentTeacher"> select * from student; </select> <resultMap id="StudentTeacher" type="Student" > <result column="id" property="id" /> <result column="name" property="name" /> <association property="teacher" column="tid" javaType="Teacher" select="getTeachers" /> </resultMap> <select id="getTeachers" resultType="Teacher"> select * from teacher where id=#{id}; </mapper>
也就是这种方法,它其实是相当于我们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="dao.StudentMapper"> <!-- 第二种 --> <select id="getStudents2" 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="Student" > <result column="sid" property="id" /> <result column="sname" property="name" /> <association property="teacher" javaType="Teacher"> <result property="id" column="tid" /> <result property="name" column="tname" /> </association> </resultMap> </mapper>
这种方法就是我们一次性把结果查询出来然后对结果集做一下处理
这里特别说明一下column这个属性,这个可不是数据库中你需要的字段名,而是你查询出来的那张表的字段名,这里起了别名之后就不再是原来teacher数据库表中的字段名了