MyBatis3系列__05查询补充resultMap与resultType区别
1.查询补充
当你查询一条记录并且是简单查询时,情况相对简单,可以参考以下的例子:
public Employee getEmpById(Integer id);
对应的xml文件中:
<select id="getEmpById" resultType="com.mybatis.learn.bean.Employee">select id, last_name lastName, gender, email from tbl_employee where id = #{id}</select>
当查询多条记录时,可以参考以下方式:、
public List<Employee> getListByGender(String gender);public Map<String, Object> getMapById(Integer id);//该注解时指定封存记录的map的key@MapKey("lastName")public Map<String, Employee> getMap(String gender);
xml中:
<!-- 查询多条记录时,若是用list封装结果,resultType填写list中的每条记录的泛型的全类名 --><select id="getListByGender" resultType="com.mybatis.learn.bean.Employee">select * from tbl_employee where gender=#{gender}</select><!-- map存储查询结果时,单条记录可以直接在resultType中写map --><select id="getMapById" resultType="map">select * from tbl_employee where id=#{id}</select><!-- map存错多条查询结果时,reusltType指定的也是里面每条记录的泛型的全类名 --><select id="getMap" resultType="com.mybatis.learn.bean.Employee">select * from tbl_employee where gender=#{gender}</select>
2.resultType&resultMap
如果是简单查询,推荐使用resultType(resultMap也能使用,但是比较麻烦),使用方式在前面演示过了。
resultMap除了可以使用在简单查询情况下,也能使用在resultType不能胜任的地方,如:联合查询,关联查询等,举个例子:
查询员工信息时包含部门信息:
新建javaBean:
package com.mybatis.learn.bean;import lombok.*;@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Department {private Integer deptId;private String deptName;
}
在原来的Employee类添加新的属性department以及getter setter
private Department deparment;
建表sql:
CREATE TABLE `tbl_dept` (`dept_id` int(11) unsigned NOT NULL,`dept_name` varchar(255) DEFAULT NULL,PRIMARY KEY (`dept_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
修改表tbl_employee ,新增字段dept_id;
此时,由于全部字段存在于两个表中,需要关联查询,已经创建的JavaBean没有一个能满足要
求,这时可以结合resultMap创建一个:
还是在EmployeeMapper.xml文件中:
<!--
场景一:查询Employee的同时查询员工对应的部门Employee===Department一个员工有与之对应的部门信息;id last_name gender dept_id dept_name (private Department dept;)--><!--联合查询:级联属性封装结果集resultMap:id标签:指定主键;result标签:指定普通列column:指定数据库中的列名property:指定对应的JavaBean中的属性名其实,指定id后再指定和数据库不一样的字段即可,不过推荐全部指定--><!-- 方式一:关联查询--><resultMap id="myEmp" type="com.mybatis.learn.bean.Employee"><id column="id" property="id"/><result column="last_name" property="lastName"/><result column="gender" property="gender"/><result column="email" property="email"/><result column="dept_id" property="dept.deptId"/><result column="dept_name" property="dept.deptName"/></resultMap><!-- 方式二:使用association定义关联的单个对象的封装规则;--><resultMap id="myEmp2" type="com.mybatis.learn.bean.Employee"><id column="id" property="id"/><result column="last_name" property="lastName"/><result column="gender" property="gender"/><result column="email" property="email"/><!--association可以指定联合的javaBean对象property="dept":指定哪个属性是联合的对象javaType:指定这个属性对象的类型[不能省略]--><association property="dept" javaType="com.mybatis.learn.bean.Department"><id column="dept_id" property="deptId"/><result column="dept_name" property="deptName"/></association></resultMap><select id="getFullEmpById" resultMap="myEmp">SELECT e.id, e.last_name, e.gender, e.email, d.dept_id, d.dept_nameFROM tbl_employee e LEFT JOIN tbl_dept d ON e.dept_id = d.dept_idWHERE id = #{id}</select><select id="getFullEmp2ById" resultMap="myEmp2">SELECT e.id, e.last_name, e.gender, e.email, d.dept_id, d.dept_nameFROM tbl_employee e LEFT JOIN tbl_dept d ON e.dept_id = d.dept_idWHERE id = #{id}</select>
更改查询Mapper:
public Employee getFullEmp2ById(Integer id);public Employee getFullEmpById(Integer id);
这时可以分别测试一下了。
当然,resultMap的意义还不止于此,比如你想分布查询时:
首先开启延迟加载:更改mybatis-config.xml文件:
<!--设置延迟加载显示的指定每个我们需要更改的配置的值,即使他是默认的。防止版本更新带来的问题 --><setting name="lazyLoadingEnabled" value="true"/><setting name="aggressiveLazyLoading" value="false"/>
原理就是在一个查询中嵌套一个查询,并且在需要关联的字段时才去执行嵌套的查询:
//EmployeeMapper中新增的方法public Employee getEmpByStep(Integer id);//EmployeeMapper.xml中新增的内容:
<resultMap id="myEmpByStep" type="com.mybatis.learn.bean.Employee"><id column="id" property="id"/><result column="last_name" property="lastName"/><result column="gender" property="gender"/><result column="email" property="email"/><association column="dept_id" property="dept"select="com.mybatis.learn.dao.DepartmentMapper.getDeptById"/></resultMap><select id="getEmpByStep" resultMap="myEmpByStep">select * from tbl_employee where id=#{id}</select>//你也发现了问题对不对,没有对应的嵌套的sql,OK,现在补齐:
//新增DepartmentMapper:
import com.mybatis.learn.bean.Department;public interface DepartmentMapper {public Department getDeptById(Integer deptId);
}//以及对应的xml:<select id="getDeptById" resultType="com.mybatis.learn.bean.Department">select * from tbl_dept where dept_id=#{deptId}</select>
ok,如何证明分布查询呢:
下面的例子将会证明:
@Testpublic void testGetEmpByStep() {String resources = "mybatis-config.xml";InputStream inputStream = null;try {inputStream = Resources.getResourceAsStream(resources);} catch (IOException e) {e.printStackTrace();}SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);SqlSession session = sessionFactory.openSession(true);EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);Employee emp = mapper.getEmpByStep(1);System.out.println(emp.getEmail());System.out.println(emp.getDept());}
执行结果如下:
第一次查询,不需要查询dept
DEBUG 03-23 15:51:26,066 ==> Preparing: select * from tbl_employee where id=? (BaseJdbcLogger.java:159)
DEBUG 03-23 15:51:26,134 ==> Parameters: 1(Integer) (BaseJdbcLogger.java:159)
DEBUG 03-23 15:51:26,231 <== Total: 1 (BaseJdbcLogger.java:159)
第一次的查询结果,打印了email
eeee
需要dept时,执行对应的sql
DEBUG 03-23 15:51:26,232 ==> Preparing: select * from tbl_dept where dept_id=? (BaseJdbcLogger.java:159)
DEBUG 03-23 15:51:26,233 ==> Parameters: 1(Long) (BaseJdbcLogger.java:159)
DEBUG 03-23 15:51:26,236 <== Total: 1 (BaseJdbcLogger.java:159)
Department(deptId=1, deptName=组织部)
转载于:https://www.cnblogs.com/JackHou/p/10584433.html
MyBatis3系列__05查询补充resultMap与resultType区别相关推荐
- MyBatis学习总结(13)——Mybatis查询之resultMap和resultType区别
MyBatis的每一个查询映射的返回类型都是ResultMap,只是当我们提供的返回类型属性是resultType的时候,MyBatis对自动的给我们把对应的值赋给resultType所指定对象的属性 ...
- MyBatis学习总结_Mybatis查询之resultMap和resultType区别
MyBatis的每一个查询映射的返回类型都是ResultMap,只是当我们提供的返回类型属性是resultType的时候,MyBatis对自动的给我们把对应的值赋给resultType所指定对象的属性 ...
- MyBatis学习总结_13_Mybatis查询之resultMap和resultType区别
MyBatis的每一个查询映射的返回类型都是ResultMap,只是当我们提供的返回类型属性是resultType的时候,MyBatis对自动的给我们把对应的值赋给resultType所指定对象的属性 ...
- resultMap和resultType区别详解
resultMap和resultType区别详解 resultmap与resulttype的区别为:对象不同.描述不同.类型适用不同 一.对象不同 1.resultmap:resultMap如果查询出 ...
- mybatis中resultMap和resultType区别,三分钟读懂
先说结论: resultmap与resulttype的区别为:对象不同.描述不同.类型适用不同. 说人话就是,resultmap和resulttype功能差不多,但是resultmap功能更强大 re ...
- resultMap和resultType区别
导入: 后台日常开发中查询最多,故每次的返回结果就会碰到,有resultMap和resultType,日常有四种情形: //实体类Dept与表字段不匹配 Dept dept=deptService.s ...
- mybatis Resultmap 与 ResultType 区别
Resultmap 的写法====目的是为了做映射 <resultMap id="BaseResultMap"type="com.suning.jupiter.co ...
- mybatis中的resultMap与resultType、parameterMap与 parameterType的区别
Map:映射:Type:Java类型 resultMap 与 resultType.parameterMap 与 parameterType的区别在面试的时候被问到的几率非常高,项目中出现了一个小b ...
- MyBatis 实现多表查询、resultMap 标签、MyBatis 注解、mybatis运行原理
内容 Auto Mapping 单表实现(别名方式) 实现单表配置 单个对象关联查询(N+1,外连接) 集合对象关联查询 注解开发 MyBatis 运行原理 一.MyBatis 实现多表查询 Myba ...
最新文章
- Uva592 Island of Logic
- 不用代码,10分钟打造属于自己的第一款小程序
- 【RS码1】系统RS码编码原理及MATLAB实现(不使用MATLAB库函数)
- oracle 安装ora 27102,ORA-27102 解决办法
- 今晚直播丨全新MySQL OLAP实时分析解决方案HeatWave详解
- 事务Transaction
- HardLink SymbolLink Junctions
- 神经网络学习小记录63——Keras 图像处理中注意力机制的代码详解与应用
- 南邮NOJ2029节奏大师
- 3D中的OBJ文件格式详解
- tomcat安装以及部署jpress
- AI芯片:寒武纪NPU设计分析(DianNao)
- Unity3D-设置天空盒
- 基于spss的多元回归分析模型
- Failed to load response data:No data found for resource with given identifier
- 网络安全——无线局域网安全技术——802.11i
- 图标图片网址集合(更新中)
- 【音频处理】乐器音符播放时电流处理 ( 使用均衡器调节低频 )
- 通过snmp获取设备和系统信息
- java 当前时间戳_通过各种方法 获取当前系统时间、时间戳
热门文章
- java 上溯_java中Instrument的上溯造型
- 信奥中的“骗”分神技 ---“打表”
- 理性派:数学写真集系列书籍等
- 基于JavaFX实现的数据库学生管理系统
- uniapp 支付(支付宝,微信支付)
- IDEA中报错“cannot resolve symbol XXX”,但编译正确可以运行
- Spring Cloud文档阅读笔记-初识Spring Cloud(对Spring Cloud初步了解)
- Java笔记-使用RabbitMQ的Java接口生产数据并消费
- WEB安全基础-命令注入
- 程序功能:延时(定时)