关于MyBatis中ResultMap的详细使用
- ResultMap简介
- 使用ResultMap
- 创建sql 模仿一对多或多对一关系
- 创建数据库
- 创建SpringBoot项目 导入mybatis依赖
- pojo---------------------------------------------------------------------------------
- dao----------------------------------------------------------------------------------
- Mapper.xml------------------------------------------------------------------------
- Controller---------------------------------------------------------------------------
ResultMap简介
在使用mybatis时,很多时候使用ResultType接收查询对象太过于局限性
例如:
- 查询出的字段与接收的实体类不符合
- 碰到关系联系的时候(一对多,多对一)
所以就引出了ResultMap。
其实官网已经能足够说明怎么使用
(https://mybatis.org/mybatis-3/zh/sqlmap-xml.html#Result_Maps)
但是讲真,光看的话真的很难熟悉它,直接开敲
使用ResultMap
创建sql 模仿一对多或多对一关系
创建数据库
use resultmaptest;
create table teacher(
tid int primary key AUTO_INCREMENT,
tname varchar(32) not null
);
create table student(
sid int PRIMARY key AUTO_INCREMENT,
sname varchar(32) not null,
tid int not null
);
alter table student add constraint FK foreign key(tid) references teacher(tid)
insert into teacher values(1,"lukew");
insert into student values(1,"张三",1);
insert into student values(2,"李四",1);
insert into student values(3,"王五",1);
insert into student values(4,"赵六",1);
insert into student values(5,"win7",1);
创建SpringBoot项目 导入mybatis依赖
这些不用多说了。。
我是用的web的方式测试
直接上代码
pojo---------------------------------------------------------------------------------
Student
@Data
public class Student {
private int sid;
private String sname;
//这里不能和数据库的表结构一样使用tid来创建Student的属性,在关系型数据库里可以,但是java里这样做是没有意义的
//所以这里使用的是Teacher对象,因为多个学生对应的是同一个老师
private Teacher teacher;
}
Teacher
@Data
public class Teacher {
private int tid;
private String tname;
//因为一个老师同时教了多个学生,所以可以这样,虽然数据库中没有定义,但是没有关系
private List<Student> lists;
}
dao----------------------------------------------------------------------------------
StudentDao
@Mapper
public interface StudentDao {
/**
* 通过sid查询学生
*/
Student queryById(@Param("sid") Integer sid);
Teacher teacherQueryById(@Param("tid") Integer tid);
}
TeacherDao
@Mapper
public interface TeacherDao {
/**
* 通过tid查询老师
*/
Teacher queryById(@Param("tid") Integer tid);
Student studentQueryById(@Param("tid") Integer sid);
}
Mapper.xml------------------------------------------------------------------------
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.jh.resultmap.dao.StudentDao">
<resultMap id="StudentMap" type="student">
<result property="sid" column="sid" />
<result property="sname" column="sname"/>
<association property="teacher" javaType="teacher">
<id column="tid" property="tid"/>
<result column="tname" property="tname"/>
</association>
</resultMap>
<select id="queryById" parameterType="int" resultMap="StudentMap">
<!--
多对一第一种方式:结果集映射
因为我们要把老师的名字给查出来,然后再赋到teacher属性中,所以不能直接select * ...
应该这样
-->
select sid,sname,t.tid,t.tname
from student as s,teacher as t
where s.tid=t.tid and sid=#{sid}
</select>
<!--*******************************************************************************************-->
<!--
第二种方式:子查询映射
先把所有的student 结果查询出来
再将tid对应上,效果等同以上
-->
<resultMap id="StudentMap2" type="student">
<id property="sid" column="sid"/>
<result property="sname" column="sname"/>
<association property="teacher" javaType="teacher" column="tid" select="teacherQueryById"/>
</resultMap>
<select id="queryById" parameterType="int" resultMap="StudentMap2">
select * from student where sid=#{sid}
</select>
<select id="teacherQueryById" parameterType="int" resultType="teacher">
select * from teacher where tid=#{tid}
</select>
</mapper>
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.jh.resultmap.dao.TeacherDao">
<resultMap id="queryMap" type="teacher">
<id column="tid" property="tid"/>
<result column="tname" property="tname"/>
<!--
在一对多的关系中就得使用collection了
因为一对多,一个的里面有多个,也可以想成一个里面多一个集合
JavaType:指定属性的类型
ofType:集合指定的泛型
-->
<collection property="lists" ofType="student" javaType="list">
<result property="sid" column="sid"/>
<result property="sname" column="sname"/>
<association property="teacher" column="tid">
<id column="tid" property="tid"/>
</association>
</collection>
</resultMap>
<select id="queryById" resultMap="queryMap" parameterType="int">
select s.sid,t.tname,s.sname,t.tid from teacher t,student s where s.tid=t.tid and t.tid=#{tid}
</select>
<!--*******************************************************************************************-->
<!--
集合中的这个同Student中的ResultMap一个道理
-->
<select id="queryById" parameterType="int" resultMap="tMap">
select * from teacher where tid=#{tid}
</select>
<resultMap id="tMap" type="teacher">
<id property="tid" column="tid"/>
<result property="tname" column="tname"/>
<collection property="lists" ofType="student" javaType="ArrayList" select="studentQueryById" column="tid">
<id column="sid" property="sid"/>
<result column="sname" property="sname"/>
</collection>
</resultMap>
<select id="studentQueryById" parameterType="int" resultType="student">
select * from student where tid=#{tid}
</select>
</mapper>
Controller---------------------------------------------------------------------------
StudentController
@RestController
public class StudentController {
@Autowired
private StudentDao studentDao;
@GetMapping("/stu/{sid}")
public Student getStudentById(@PathVariable("sid") Integer sid){
Student student = studentDao.queryById(sid);
return student;
}
}
TeacherController
@RestController
public class TeacherController {
@Autowired
private TeacherDao teacherDao;
@GetMapping("/t/{tid}")
public Teacher getTeacher(@PathVariable("tid") Integer tid){
return teacherDao.queryById(tid);
}
}
过程大部分写在了注释里,因为这样方便思考
两种方式,不能同时存在,需要注释掉其中一个才能测试
以上是参考 狂神
视频结合自己总结出来
一句话,还是得多用、多敲、多想!