MyBatis处理对象映射(一对多,多对一,多对多)

首先创建两张表:

  1. CREATE TABLE `teacher` (
  2. `id` varchar(20) NOT NULL DEFAULT '',
  3. `name` varchar(20) NOT NULL DEFAULT '',
  4. PRIMARY KEY (`id`) USING BTREE
  5. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  6. INSERT INTO `test`.`teacher`(`t_id`, `t_name`) VALUES ('01', '张三');
  7. INSERT INTO `test`.`teacher`(`t_id`, `t_name`) VALUES ('02', '李四');
  8. INSERT INTO `test`.`teacher`(`t_id`, `t_name`) VALUES ('03', '王五');
  9. INSERT INTO `test`.`teacher`(`t_id`, `t_name`) VALUES ('04', '李强');
  10. CREATE TABLE `student` (
  11. `id` varchar(20) NOT NULL DEFAULT '',
  12. `name` varchar(20) NOT NULL DEFAULT '',
  13. `birth` varchar(20) NOT NULL DEFAULT '',
  14. `sex` varchar(10) NOT NULL DEFAULT '',
  15. `tid` varchar(20) NOT NULL,
  16. PRIMARY KEY (`id`) USING BTREE,
  17. KEY `t_id` (`tid`)
  18. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  19. INSERT INTO `test`.`student`(`s_id`, `s_name`, `s_birth`, `s_sex`, `t_id`) VALUES ('01', '赵雷', '1990-01-01', '男', '01');
  20. INSERT INTO `test`.`student`(`s_id`, `s_name`, `s_birth`, `s_sex`, `t_id`) VALUES ('02', '钱电', '1990-12-21', '男', '02');
  21. INSERT INTO `test`.`student`(`s_id`, `s_name`, `s_birth`, `s_sex`, `t_id`) VALUES ('03', '孙风', '1990-05-20', '男', '03');
  22. INSERT INTO `test`.`student`(`s_id`, `s_name`, `s_birth`, `s_sex`, `t_id`) VALUES ('04', '李云', '1990-08-06', '男', '04');
  23. INSERT INTO `test`.`student`(`s_id`, `s_name`, `s_birth`, `s_sex`, `t_id`) VALUES ('05', '周梅', '1991-12-01', '女', '02');
  24. INSERT INTO `test`.`student`(`s_id`, `s_name`, `s_birth`, `s_sex`, `t_id`) VALUES ('06', '吴兰', '1992-03-01', '女', '04');
  25. INSERT INTO `test`.`student`(`s_id`, `s_name`, `s_birth`, `s_sex`, `t_id`) VALUES ('07', '郑竹', '1989-07-01', '女', '03');
  26. INSERT INTO `test`.`student`(`s_id`, `s_name`, `s_birth`, `s_sex`, `t_id`) VALUES ('08', '王菊', '1990-01-20', '女', '01');

配置文件:

  1. spring.datasource.driver-class-name=com.mysql.jdbc.Driver
  2. spring.datasource.url=jdbc:mysql://localhost:3306/test
  3. spring.datasource.username=root
  4. spring.datasource.password=123456
  5. #mybatis
  6. mybatis.mapper-locations=classpath*:com/example/spring_mybatis/mapper/*.xml
  7. mybatis.type-aliases-package=com.example.spring_mybatis.pojo

多对一(多个学生拥有一个老师):

Student实体类:

  1. package com.example.spring_mybatis.pojo;
  2. import java.io.Serializable;
  3. import lombok.Data;
  4. @Data
  5. public class Student implements Serializable {
  6. private String id;
  7. private String name;
  8. private String birth;
  9. private String sex;
  10. private String tid;
  11. private Teacher teacher;
  12. private static final long serialVersionUID = 1L;
  13. }

Teacher实体类:

  1. package com.example.spring_mybatis.pojo;
  2. import java.io.Serializable;
  3. import lombok.Data;
  4. @Data
  5. public class Teacher implements Serializable {
  6. private String id;
  7. private String name;
  8. private static final long serialVersionUID = 1L;
  9. }

StudentMapper.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="com.example.spring_mybatis.mapper.StudentMapper">
  4. <!--子查询-->
  5. <!-- <select id="getStudent" resultMap="StudentTeacher">
  6. select id,name,tid from student
  7. </select>
  8. <resultMap id="StudentTeacher" type="com.example.spring_mybatis.pojo.Student">
  9. <result property="id" column="id"/>
  10. <result property="name" column="name"/>
  11. <result property="tid" column="tid"/>
  12. &lt;!&ndash;复杂的属性单独处理
  13. &ndash;&gt;
  14. <association property="teacher" column="tid" javaType="com.example.spring_mybatis.pojo.Teacher" select="getTeacher"/>
  15. </resultMap>
  16. <select id="getTeacher" resultType="com.example.spring_mybatis.pojo.Teacher">
  17. select name from teacher where id = #{tid}
  18. </select>-->
  19. <!--按照结果嵌套处理,连表查询,推荐使用这种-->
  20. <select id="getStudent" resultMap="StudentTeacher">
  21. select s.id sid,s.name sname,t.name tname
  22. from student s,teacher t
  23. where s.tid = t.id;
  24. </select>
  25. <resultMap id="StudentTeacher" type="Student">
  26. <result property="id" column="sid"/>
  27. <result property="name" column="sname"/>
  28. <association property="teacher" javaType="Teacher">
  29. <result property="name" column="tname"/>
  30. </association>
  31. </resultMap>
  32. </mapper>

测试代码:

  1. package com.example.spring_mybatis;
  2. import com.example.spring_mybatis.mapper.StudentMapper;
  3. import com.example.spring_mybatis.mapper.TeacherMapper;
  4. import com.example.spring_mybatis.pojo.Student;
  5. import org.junit.jupiter.api.Test;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.boot.test.context.SpringBootTest;
  8. import java.util.List;
  9. @SpringBootTest
  10. class SpringMybatisApplicationTests {
  11. @Autowired
  12. private StudentMapper studentMapper;
  13. @Autowired
  14. private TeacherMapper teacherMapper;
  15. @Test
  16. void contextLoads() {
  17. List<Student> studentList = studentMapper.getStudent();
  18. for (Student student : studentList) {
  19. System.out.println(student);
  20. }
  21. }
  22. }

查询结果:
1.png

一对多(一个老师拥有多个学生):

首先给老师的实体类修改:

  1. package com.example.spring_mybatis.pojo;
  2. import java.io.Serializable;
  3. import java.util.List;
  4. import lombok.Data;
  5. @Data
  6. public class Teacher implements Serializable {
  7. private String id;
  8. private String name;
  9. private List<Student> students;
  10. private static final long serialVersionUID = 1L;
  11. }

TeacherMapper.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="com.example.spring_mybatis.mapper.TeacherMapper">
  4. <!--连表查询-->
  5. <!-- <select id="getTeacher" resultMap="TeacherStudent" parameterType="String">
  6. select s.id sid,s.name sname,t.id tid,t.name tname
  7. from teacher t,student s
  8. where t.id = s.tid
  9. and t.id = #{id};
  10. </select>
  11. &lt;!&ndash;按结果嵌套查询&ndash;&gt;
  12. <resultMap id="TeacherStudent" type="Teacher">
  13. <result property="id" column="tid"/>
  14. <result property="name" column="tname"/>
  15. &lt;!&ndash;此处要用集合
  16. javaType,指定属性的类型
  17. ofType,获取集合中的泛型
  18. &ndash;&gt;
  19. <collection property="students" ofType="Student">
  20. <result property="id" column="sid"/>
  21. <result property="name" column="sname"/>
  22. </collection>
  23. </resultMap>-->
  24. <!--子查询-->
  25. <select id="getTeacher" resultMap="TeacherStudent">
  26. select * from teacher where id = #{id};
  27. </select>
  28. <resultMap id="TeacherStudent" type="Teacher">
  29. <collection property="students" javaType="ArrayList" ofType="Student" select="getStudent" column="id"/>
  30. </resultMap>
  31. <select id="getStudent" resultType="Student">
  32. select * from student where tid = #{tid};
  33. </select>
  34. </mapper>

测试类:

  1. package com.example.spring_mybatis;
  2. import com.example.spring_mybatis.mapper.StudentMapper;
  3. import com.example.spring_mybatis.mapper.TeacherMapper;
  4. import com.example.spring_mybatis.pojo.Student;
  5. import com.example.spring_mybatis.pojo.Teacher;
  6. import org.junit.jupiter.api.Test;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.boot.test.context.SpringBootTest;
  9. import java.util.List;
  10. @SpringBootTest
  11. class SpringMybatisApplicationTests {
  12. @Autowired
  13. private TeacherMapper teacherMapper;
  14. @Test
  15. void contextLoads() {
  16. Teacher teacher = teacherMapper.getTeacher("01");
  17. System.out.println(teacher);
  18. }
  19. }

输出结果:
2.png

注意点:

1.JavaType和ofType都是用来指定对象类型的,但是JavaType是用来指定pojo中属性的类型,而ofType指定的是映射到list集合属性中pojo的类型。