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关联查询多个参数方式
- MyBatis使用嵌套查询collection和association的实现
- mybatis resultMap之collection聚集两种实现方式
- mybatis中association和collection的使用与区别
- Mybatis中一对多(collection)和一对一(association)的组合查询使用
- Mybatis使用Collection属性的示例代码
- Mybatis的collection三层嵌套查询方式(验证通过)
- mybatis collection和association的区别解析
- MyBatis中<collection>标签的多种用法
相关文章
深入理解Java并发编程之LinkedBlockingQueue队列
本文主要介绍了Java并发编程之LinkedBlockingQueue队列,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2022-04-04
最新评论