MyBatis association的两种形式——MyBatis学习笔记之四
一、嵌套的resultMap
这 种方法本质上就是上篇博文介绍的方法,只是把教师实体映射从association元素中提取出来,用一个resultMap元素表示。然后 association元素再引用这个resultMap元素。修改上篇博文示例的StudentMapper.xml如下:
<?xml version="1.0" encoding="utf8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.abc.mapper.StudentMapper"> <select id="getById" parameterType="int" resultMap="studentResultMap"> select s.id s_id, s.name s_name, s.gender s_gender, s.major s_major, s.grade s_grade, t.id t_id, t.name t_name, t.gender t_gender, t.title t_title, t.research_area t_research_area from student s left join teacher t on s.supervisor_id = t.id where s.id=#{id} </select> <resultMap id="studentResultMap" type="Student"> <id property="id" column="s_id"/> <result property="name" column="s_name"/> <result property="gender" column="s_gender"/> <result property="major" column="s_major"/> <result property="grade" column="s_grade"/> <!--使用resultMap属性引用下面的教师实体映射--> <association property="supervisor" javaType="Teacher" resultMap="supervisorResultMap"/> </resultMap> <!--教师实体映射--> <resultMap id="supervisorResultMap" type="Teacher"> <id property="id" column="t_id"/> <result property="name" column="t_name"/> <result property="gender" column="t_gender"/> <result property="researchArea" column="t_research_area"/> <result property="title" column="t_title"/> </resultMap> </mapper>
在 这里,为了让select语句更加清晰可读,每个字段都使用了别名。association元素的属性javaType="Teacher"可以不要,不 像上篇博文示例那样,没有此属性即报错。这大概是因为引用的resultMap元素已经指明了映射的类型为“Teacher”。另外,今天偶然发现,把 StudentMapper.xml的encoding属性由“UTF-8”改为“utf8”即可添加中文注释。
运行结果如下:
这种方式的好处是此resultMap元素可在其它地方被重用(可在博文下方附件下载处下载示例源码)。
二、嵌套的select语句
这种方式是使用一条单独的select语句来加载关联的实体(在本例中就是教师实体),然后在association元素中引用此select语句(注:此方法会产生N+1问题,关于这个问题可参考本系列博客中的“MyBatis中的N+1问题”)。修改StudentMapper.xml如下:
<?xml version="1.0" encoding="utf8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.abc.mapper.StudentMapper"> <select id="getById" parameterType="int" resultMap="studentResultMap"> select id,name,gender,major,grade,supervisor_id from student where id=#{id} </select> <select id="selectSupervisor" parameterType="int" resultMap="supervisorResultMap"> select id,name,gender,title,research_area from teacher where id=#{id} </select> <resultMap id="studentResultMap" type="Student"> <id property="id" column="id"/> <result property="name" column="name"/> <result property="gender" column="gender"/> <result property="major" column="major"/> <result property="grade" column="grade"/> <!--column="supervisor_id"不能少。此列的值作为参数 传递给要引用的select语句,用来查询相应学生的指导教师 的信息。select属性指定要引用的select语句--> <association property="supervisor" javaType="Teacher" column="supervisor_id" select="selectSupervisor"/> </resultMap> <!--教师实体映射--> <resultMap id="supervisorResultMap" type="Teacher"> <id property="id" column="id"/> <result property="name" column="name"/> <result property="gender" column="gender"/> <result property="researchArea" column="research_area"/> <result property="title" column="title"/> </resultMap> </mapper>
association元素的属性javaType="Teacher"也可以不要,这大概也是因为引用的select语句中已经指明了要用到的resultMap。
执行结果如下(可在博文下方附件下载处下载示例源码):
转载于:https://www.cnblogs.com/duanxz/p/3830509.html
MyBatis association的两种形式——MyBatis学习笔记之四相关推荐
- MyBatis collection的两种形式——MyBatis学习笔记之九
与association一样,collection元素也有两种形式,现介绍如下: 一.嵌套的resultMap 实际上以前的示例使用的就是这种方法,今天介绍它的另一种写法.还是以教师映射为例,修改映射 ...
- mysql association_MyBatis association的两种形式——MyBatis学习笔记之四
一.嵌套的resultMap 这 种方法本质上就是上篇博文介绍的方法,只是把教师实体映射从association元素中提取出来,用一个resultMap元素表示.然后 association元素再引用 ...
- myBatis association的两种形式
1.嵌套的resultMap 这种方法的本质就是把教师实体映射从association元素中提取出来,用一个resultMap元素表示.然后association元素再引用这个resultMap元素. ...
- SQL 关于apply的两种形式cross apply 和 outer apply
SQL 关于apply的两种形式cross apply 和 outer apply 阅读目录 SQL 关于apply的两种形式cross apply 和 outer apply Sql学习第四天--S ...
- 【算法笔记】莫比乌斯反演(包含定理,两种形式的证明及入门经典模板)
整理的算法模板合集: ACM模板 目录 一.莫比乌斯反演 二.几个概念和定理 三.两种形式的莫比乌斯反演证明 四.POJ 3904 Sky Code(入门例题) 一.莫比乌斯反演 学习笔记,我是看这个 ...
- c语言中的普通字符包括什么,【判断题】C语言中的字符常量通常有两种形式:普通字符和转义字符。...
[判断题]C语言中的字符常量通常有两种形式:普通字符和转义字符. 更多相关问题 ---Can you speak French?---Yes, but only____.A.a littleB.lit ...
- java语言的多态性及特点_Java中的方法的多态性有两种形式:( )和( )。_学小易找答案...
[填空题]已知函数 ,则微分 . [填空题]. [单选题]71.用转动小滑板法车圆锥时产生( )误差的原因是小滑板转动角度计算错误. [单选题] [填空题]Java中的方法的多态性有两种形式:( )和 ...
- Go 两种形式的“类型转换”
Go 的类型转换常常让人有点迷,有两种形式的"类型转换": Type(obj) :这种形式的类型转换要求 obj 对象的类型和 Type 是等价类型,即实现了相同的方法 obj.( ...
- include的两种形式、CPP的搜索路径
文章目录 1 include的两种形式.CPP的搜索路径 1 include的两种形式.CPP的搜索路径 #include "stdio.h" //1.源文件所在路径//2.-I选 ...
最新文章
- 可以控制到多低(功率)?
- 思考:开发者如何挑选最合适的机器学习框架?
- Delphi解析类似\u97e9这样的Unicode字符串
- 记录结果再利用的动态规划
- 安全的交互通道 及 栈回塑检查与伪造
- 一个想法不一定对 系列
- Exchange端口列表
- SAP Cloud Platform certificate trust下载和business role创建
- ios 筛选_万千网友让quot;低调使用quot;的软件!居然还支持iOS
- apache配置多个虚拟主机
- 软件概要设计_软件测试模型之 V模型
- 10.机器学习sklearn-------手写数字识别实例
- linux dm9000网卡驱动,ARM-Linux驱动--DM9000网卡驱动分析(三)
- vue中变量名前加三个点代表什么意思
- 手机开热点但是电脑一直连接不上_电脑连不上wifi,手机可以连上。手机开热点,电脑可以连上。这是怎么回事,电脑就一直循环连接那个w...
- VMware安装windows7x64时提示找不到CD/DVD驱动器
- HTML--video中的自动播放autoplay以及循环播放loop+更多多媒体标签
- 学习数织物密度/经纬密
- CUG中国地质大学(武汉)现代软件工程澳洲代购(网购)用例图活动图状态图
- 爬虫 爬取不到数据原因总结
热门文章
- python 函数特殊属性
- javascript window location
- 图解TCPIP-MIME
- catv系统主要有哪三部分组成_答案光接入试题(答案)3.12
- Vcenter6.5 Administrator账户密码忘记或者过期无法登陆解决办法
- Docker学习总结(61)——Docker、Docker-Compose、Docker swarm 、 K8s 之间的区别
- MyBatis学习总结(22)——Mybatis自增长ID获取
- 凯斯西储计算机科学,凯斯西储大学电气工程与计算机科学系基本信息详解
- 小括号教学设计导入_如何在教学中凸显出学生的主体地位呢?原来只需要做到这几点即可...
- # 2017-2018-1 20155224 《信息安全系系统设计基础》第四周MyOD