MyBatis的collection和association的使用解读
作者:秃了也弱了。
这篇文章主要介绍了MyBatis的collection和association的使用解读
写在前面
MyBatis涉及到多表关联查询的时候,有一个非常实用的工具,可以无缝封装object、array,将结果返回指定格式的json数据。
在这里提供手把手教学,一起看collection和association如何使用。
表结构及数据准备
CREATE TABLE `test_school` ( `id` varchar(20) NOT NULL, `name` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE `test_class` ( `id` varchar(20) NOT NULL, `name` varchar(255) DEFAULT NULL, `school_id` varchar(20) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE `test_teacher` ( `id` varchar(20) NOT NULL, `name` varchar(255) DEFAULT NULL, `class_id` varchar(20) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
数据与关系
分别在三张表中简单的创建了几条数据,来简单的表示数据之间的关系。

我们可以看出,school与class的数据之间是一对多的关系;class与teacher之间的关系是一对一的关系。
如果只是单纯的使用sql关联查询时,会查出来三条数据,其中school的数据是重复的,因为left join嘛。
关键:MyBatis的xml
SchoolMapper.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.boot.security.server.dao.SchoolDao">
<resultMap id="SchoolMap" type="com.boot.security.server.model.TestSchool">
<id column="id" property="id" />
<result column="name" property="name" />
<collection property="testClasses" columnPrefix="tc_"
resultMap="com.boot.security.server.dao.ClassDao.ClassMap"/>
</resultMap>
<select id="list" resultMap="SchoolMap">
select ts.id,ts.name,
tc.id AS tc_id,
tc.name AS tc_name,
tc.school_id AS tc_school_id,
tt.id AS tc_tt_id,
tt.name AS tc_tt_name,
tt.class_id AS tc_tt_class_id
from test_school ts
left join test_class tc on ts.id=tc.school_id
left join test_teacher tt on tc.id=tt.class_id
</select>
</mapper>ClassMapper.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.boot.security.server.dao.ClassDao">
<resultMap id="ClassMap" type="com.boot.security.server.model.TestClass">
<id column="id" property="id" />
<result column="name" property="name" />
<result column="school_id" property="schoolId" />
<association property="testTeacher" columnPrefix="tt_"
resultMap="com.boot.security.server.dao.TeacherDao.TeacherMap"/>
</resultMap>
</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.boot.security.server.dao.TeacherDao">
<resultMap id="TeacherMap" type="com.boot.security.server.model.TestTeacher">
<id column="id" property="id" />
<result column="name" property="name" />
<result column="class_id" property="classId" />
</resultMap>
</mapper>注意!SchoolMapper.xml中的collection与ClassMapper.xml中的association!
注意!注意查询的sql起的别名,与collection和association中的columnPrefix属性需要对应
其中service、controller这里省略了,我们直接调用SchoolMapper.xml中的list方法,查看一下执行结果!
执行结果

我们可以看到,class被封装成了一个数组(因为我们在SchoolMapper.xml中用collection封装);
teacher被封装成一个对象(因为我们在ClassMapper.xml中用association封装)。
总结
MyBatis提供了非常强大的对象、数组封装方式,直接将查询出来的结果封装成对应的格式。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
- Mybatis中collection和association的使用区别详解
- mybatis利用association或collection传递多参数子查询
- Mybatis之association和collection用法
- 在Mybatis中association标签多层嵌套的问题
- mybatis中一对一关系association标签的使用
- MyBatis中association的基本使用方法
- mybatis的association传递参数问题示例
- Mybatis中一对多(collection)和一对一(association)的组合查询使用
- mybatis中association标签的使用解读
- MyBatis使用嵌套查询collection和association的实现
- Mybatis的association使用子查询结果错误的问题解决
