十、多对一处理
- 多个学生对应一个老师
- 对于学生这边:关联,多个学生关联一个老师【多对一】
- 对于老师这边:集合,一个老师又很多学生【一对多】
1. 基本环境搭建
1.1 创建数据库表
#数据库里面的`teacher`是键盘Tab上面的,而'王老师'这里的'是回车左边的 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);
1.2 导入Lombok
<dependencies> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.20</version> </dependency> </dependencies>
1.3 新建实体类Teacher,Student
@Data public class Teacher { private int id; private String name; }
@Data public class Student { private int id; private String name; //学生需要关联一个老师 private Teacher teacher; }
1.4 建立Mapper接口
import com.hxl.pojo.Teacher; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; public interface TeacherMapper { @Select("select * from teacher where id = #{tid}") Teacher getTeacher(@Param("tid")int id); }
public interface StudentMapper { }
1.5建立Mapper.xml文件
这个地方创建mapper.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="com.hxl.dao.TeacherMapper"> </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="com.hxl.dao.StudentMapper"> </mapper>
1.6 在核心配置文件中绑定注册我们的Mapper接口或者文件
这里如果使用class就必须让TeacherMapper和TeacherMapper.xml在同一个文件下。
<mappers> <mapper class="com.hxl.dao.TeacherMapper"/> <mapper class="com.hxl.dao.StudentMapper"/> </mappers>
1.7 测试查询是否能够成功
public class myTest { @Test public void test(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class); Teacher teacher = mapper.getTeacher(1); System.out.println(teacher); sqlSession.close(); } }
2. 按照查询嵌套处理
复杂的属性,我们需要单独处理. 对象:association 集合:collection
javaType=""指定属性的类型! 集合中的泛型信息,我们使用ofType获取
public interface StudentMapper { //查询所有的学生信息,以及对应的老师的信息 public List<Student> getStudent(); }
<mapper namespace="com.hxl.dao.StudentMapper"> <!--方式一:按照查询嵌套处理 思路: 1.查询所有的学生信息 2.根据查询出来的学生tid,寻找对应的老师 --> <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="getStudent" resultMap="studentTeacher"> select * from student; </select> <select id="getTeacher" resultType="Teacher"> select * from teacher where id = #{id}; </select> </mapper>
3. 按照查询嵌套处理
<!--方式二:按照结果嵌套处理--> <select id="getStudent2" resultMap="Student2"> select s.id sid, s.name sname, t.name tname from student s, teacher t where s.tid = t.id; </select> <resultMap id="StudentTeacher2" type="Student"> <result property="id" column="sid"/> <result property="name" column="sname"/> <association property="teacher" javaType="Teacher"> <result property="name" column="tname"/> </association> </resultMap>
回顾Mysql多对一查询方式
- 子查询
- 联表查询