MyBatis处理对象映射(一对多,多对一,多对多)
首先创建两张表:
CREATE TABLE `teacher` (
`id` varchar(20) NOT NULL DEFAULT '',
`name` varchar(20) NOT NULL DEFAULT '',
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `test`.`teacher`(`t_id`, `t_name`) VALUES ('01', '张三');
INSERT INTO `test`.`teacher`(`t_id`, `t_name`) VALUES ('02', '李四');
INSERT INTO `test`.`teacher`(`t_id`, `t_name`) VALUES ('03', '王五');
INSERT INTO `test`.`teacher`(`t_id`, `t_name`) VALUES ('04', '李强');
CREATE TABLE `student` (
`id` varchar(20) NOT NULL DEFAULT '',
`name` varchar(20) NOT NULL DEFAULT '',
`birth` varchar(20) NOT NULL DEFAULT '',
`sex` varchar(10) NOT NULL DEFAULT '',
`tid` varchar(20) NOT NULL,
PRIMARY KEY (`id`) USING BTREE,
KEY `t_id` (`tid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `test`.`student`(`s_id`, `s_name`, `s_birth`, `s_sex`, `t_id`) VALUES ('01', '赵雷', '1990-01-01', '男', '01');
INSERT INTO `test`.`student`(`s_id`, `s_name`, `s_birth`, `s_sex`, `t_id`) VALUES ('02', '钱电', '1990-12-21', '男', '02');
INSERT INTO `test`.`student`(`s_id`, `s_name`, `s_birth`, `s_sex`, `t_id`) VALUES ('03', '孙风', '1990-05-20', '男', '03');
INSERT INTO `test`.`student`(`s_id`, `s_name`, `s_birth`, `s_sex`, `t_id`) VALUES ('04', '李云', '1990-08-06', '男', '04');
INSERT INTO `test`.`student`(`s_id`, `s_name`, `s_birth`, `s_sex`, `t_id`) VALUES ('05', '周梅', '1991-12-01', '女', '02');
INSERT INTO `test`.`student`(`s_id`, `s_name`, `s_birth`, `s_sex`, `t_id`) VALUES ('06', '吴兰', '1992-03-01', '女', '04');
INSERT INTO `test`.`student`(`s_id`, `s_name`, `s_birth`, `s_sex`, `t_id`) VALUES ('07', '郑竹', '1989-07-01', '女', '03');
INSERT INTO `test`.`student`(`s_id`, `s_name`, `s_birth`, `s_sex`, `t_id`) VALUES ('08', '王菊', '1990-01-20', '女', '01');
配置文件:
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=123456
#mybatis
mybatis.mapper-locations=classpath*:com/example/spring_mybatis/mapper/*.xml
mybatis.type-aliases-package=com.example.spring_mybatis.pojo
多对一(多个学生拥有一个老师):
Student实体类:
package com.example.spring_mybatis.pojo;
import java.io.Serializable;
import lombok.Data;
@Data
public class Student implements Serializable {
private String id;
private String name;
private String birth;
private String sex;
private String tid;
private Teacher teacher;
private static final long serialVersionUID = 1L;
}
Teacher实体类:
package com.example.spring_mybatis.pojo;
import java.io.Serializable;
import lombok.Data;
@Data
public class Teacher implements Serializable {
private String id;
private String name;
private static final long serialVersionUID = 1L;
}
StudentMapper.xml
<?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.example.spring_mybatis.mapper.StudentMapper">
<!--子查询-->
<!-- <select id="getStudent" resultMap="StudentTeacher">
select id,name,tid from student
</select>
<resultMap id="StudentTeacher" type="com.example.spring_mybatis.pojo.Student">
<result property="id" column="id"/>
<result property="name" column="name"/>
<result property="tid" column="tid"/>
<!–复杂的属性单独处理
–>
<association property="teacher" column="tid" javaType="com.example.spring_mybatis.pojo.Teacher" select="getTeacher"/>
</resultMap>
<select id="getTeacher" resultType="com.example.spring_mybatis.pojo.Teacher">
select name from teacher where id = #{tid}
</select>-->
<!--按照结果嵌套处理,连表查询,推荐使用这种-->
<select id="getStudent" resultMap="StudentTeacher">
select s.id sid,s.name sname,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"/>
</association>
</resultMap>
</mapper>
测试代码:
package com.example.spring_mybatis;
import com.example.spring_mybatis.mapper.StudentMapper;
import com.example.spring_mybatis.mapper.TeacherMapper;
import com.example.spring_mybatis.pojo.Student;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
class SpringMybatisApplicationTests {
@Autowired
private StudentMapper studentMapper;
@Autowired
private TeacherMapper teacherMapper;
@Test
void contextLoads() {
List<Student> studentList = studentMapper.getStudent();
for (Student student : studentList) {
System.out.println(student);
}
}
}
查询结果:
一对多(一个老师拥有多个学生):
首先给老师的实体类修改:
package com.example.spring_mybatis.pojo;
import java.io.Serializable;
import java.util.List;
import lombok.Data;
@Data
public class Teacher implements Serializable {
private String id;
private String name;
private List<Student> students;
private static final long serialVersionUID = 1L;
}
TeacherMapper.xml
<?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.example.spring_mybatis.mapper.TeacherMapper">
<!--连表查询-->
<!-- <select id="getTeacher" resultMap="TeacherStudent" parameterType="String">
select s.id sid,s.name sname,t.id tid,t.name tname
from teacher t,student s
where t.id = s.tid
and t.id = #{id};
</select>
<!–按结果嵌套查询–>
<resultMap id="TeacherStudent" type="Teacher">
<result property="id" column="tid"/>
<result property="name" column="tname"/>
<!–此处要用集合
javaType,指定属性的类型
ofType,获取集合中的泛型
–>
<collection property="students" ofType="Student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
</collection>
</resultMap>-->
<!--子查询-->
<select id="getTeacher" resultMap="TeacherStudent">
select * from teacher where id = #{id};
</select>
<resultMap id="TeacherStudent" type="Teacher">
<collection property="students" javaType="ArrayList" ofType="Student" select="getStudent" column="id"/>
</resultMap>
<select id="getStudent" resultType="Student">
select * from student where tid = #{tid};
</select>
</mapper>
测试类:
package com.example.spring_mybatis;
import com.example.spring_mybatis.mapper.StudentMapper;
import com.example.spring_mybatis.mapper.TeacherMapper;
import com.example.spring_mybatis.pojo.Student;
import com.example.spring_mybatis.pojo.Teacher;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
class SpringMybatisApplicationTests {
@Autowired
private TeacherMapper teacherMapper;
@Test
void contextLoads() {
Teacher teacher = teacherMapper.getTeacher("01");
System.out.println(teacher);
}
}
输出结果:
注意点:
1.JavaType和ofType都是用来指定对象类型的,但是JavaType是用来指定pojo中属性的类型,而ofType指定的是映射到list集合属性中pojo的类型。