mybatis中resultMap的使用
在mybatis中,使用<select>标签,必须要设置resultType属性 或 resultMap属性
否则会报错!
resultType一般是返回简单类型的查询结果,涉及一张表
可是当我们使用关联查询时,会涉及到多张表,这时的返回结果只用一个pojo类来描述,显然是不合适的
所以我们引入了resultMap属性,可以实现返回 自定义类型
在Mapper文件中,写入resultMap
<?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"> <!--其中namespace是命名空间,通过命名空间去寻找对应的sql标签
必须要唯一,定位到dao层的接口上-->
<mapper namespace="top.bigking.dao.EmpMapper">
<resultMap id="RM_Emp" type="top.bigking.pojo.Emp">
<!-- property属性表示 pojo 中的属性 ,column 表示数据库中的列名-->
<id property="empNo" column="empNo"/>
<result property="ename" column="ename" />
<result property="job" column="job" />
<result property="mgr" column="mgr" />
<result property="hireDate" column="hireDate" />
<result property="sal" column="sal" />
<result property="comm" column="comm" />
<result property="deptNo" column="deptNo" />
<association property="dept" column="deptNo" javaType="top.bigking.pojo.Dept" >
<result property="deptNo" column="deptNo" />
<result property="dname" column="dname" />
<result property="loc" column="loc" />
</association>
</resultMap>
<select id="query" resultMap="RM_Emp">
SELECT t1.EMPNO, t1.ENAME, t1.MGR, t1.HIREDATE, t1.JOB,t1.SAL, t1.COMM,t1.DEPTNO, t2.DNAME
FROM emp t1 LEFT JOIN dept t2
on t1.DEPTNO = t2.DEPTNO
</select>
</mapper>
注意:<resultMap>中的id属性的值,必须与下面<select>标签中的resultMap的值相等,这样才能对应到返回值
在Emp.java中,增加属性:private Dept dept;
这样就能完成一对一查询
什么意思呢?
一个员工只属于一个部门,所以这是一对一查询
但是!反过来,一个部门可以有多个员工,这就是一对多查询!
在Dept.java中增加新的属性:private List<Emp> empList;
即可表示:一个部门中有多个员工
在DeptMapper.xml中,加入以下代码
<resultMap id="RM_Dept" type="top.bigking.pojo.Dept">
<id property="deptNo" column="deptNo" />
<result property="dname" column="dname" />
<result property="loc" column="loc" />
<collection property="empList" ofType="top.bigking.pojo.Emp" column="deptNo">
<result property="empNo" column="empNo"/>
<result property="ename" column="ename" />
<result property="job" column="job" />
<result property="mgr" column="mgr" />
<result property="hireDate" column="hireDate" />
<result property="sal" column="sal" />
<result property="comm" column="comm" />
<result property="deptNo" column="deptNo" />
</collection>
</resultMap>
完整代码如下:
链接:https://pan.baidu.com/s/1Gnp5VdJwYghJe8lFml1gTw
提取码:tm6z
mybatis中resultMap的使用的更多相关文章
- mybatis中resultMap配置细则
resultMap算是mybatis映射器中最复杂的一个节点了,能够配置的属性较多,我们在mybatis映射器配置细则这篇博客中已经简单介绍过resultMap的配置了,当时我们介绍了resultMa ...
- 在mybatis中resultMap与resultType的区别
MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMapresultType是直接表示返回类型的,而resultMap则是对外部ResultMa ...
- Mybatis中resultMap与resultType区别
MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap,resultType是直接表示返回类型的,而resultMap则是对外部ResultM ...
- Mybatis中resultMap的基础配置
一.概述 resultMap 元素是 MyBatis 中最重要最强大的元素.它就是让你远离 90%的需要从结果集中取出数据的 JDBC 代码的那个东西,而且在一些情形下允许你做一些 JDBC 不支持的 ...
- mybatis中resultMap引发的吐血bug
简单的讲: 问题背景:如果在写mybatis中的resultMap时,不下心将resultMapde id写成映射接口的名字,会发生什么? 结论:单元测试进度条卡住但不报错, Tomcat运行不报错, ...
- MyBatis 中 resultMap 详解
resultMap 是 Mybatis 最强大的元素之一,它可以将查询到的复杂数据(比如查询到几个表中数据)映射到一个结果集当中.如在实际应用中,有一个表为(用户角色表),通过查询用户表信息展示页面, ...
- Mybatis中<resultMap>用法(主要用于一对多去重)
一.创建部门表和员工表: 创建部门信息表`t_department`,其中包括`id`, `name` CREATE TABLE t_department ( id INT AUTO_ ...
- Mybatis中resultMap的作用-解决实体类属性名和数据库字段不一致
解决实体类属性名和数据库字段不一致
- MyBatis中resultType和resultMap的区别
resultType和resultMap功能类似 ,都是返回对象信息 ,但是resultMap要更强大一些 ,可自定义.因为resultMap要配置一下,表和类的一一对应关系,所以说就算你的字段名 ...
随机推荐
- web性能优化--算法优化(四)
避免for-in 把数组长度保存在局部变量中 较少迭代次数(Duffs Device) 基于函数的循环比基于循环的迭代消耗性能更多 优化if-else,一般switch比if-else速度快(hash ...
- python基础31[python IDE之Eclipse+PyDev]
一 入门IDE作为python的初学者,在语法和类库学习阶段,我们可以使用以下简单使用的IDE:1) Python SDK 自带的IDEL(Python GUI)2) Komodo-Edit3) No ...
- 一种循环C字符数组的骚操作
#include <stdio.h> #include <stdlib.h> int main() { char wenwa[] = "程劲小盆友在做什么" ...
- thinkphp一般数据库操作
引入命名空间 插入 更新 查询 删除 一些支持命令行的操作 清空操作 分库操作 分库相关配置---在config.php中进行 使用: 参数绑定 占位符绑定 第一句后半拉
- spring boot2.0.2,<-1.4.8
DataSourceBuilder cannot be resolved DataSourceBuilder cannot be resolved to a type RelaxedPropertyR ...
- html aside标签 语法
html aside标签 语法 aside是什么意思? aside为语义化标签,通常用来描述与文档主体内容不相关的内容,其aside标签的内容应该与附近的内容相关. 作用:定义其所处内容之外的内容.直 ...
- python Tkinter 组件
Tkinter的提供各种控件,如按钮,标签和文本框,一个GUI应用程序中使用.这些控件通常被称为控件或者部件. 目前有15种Tkinter的部件.我们提出这些部件以及一个简短的介绍,在下面的表: 控件 ...
- 常见对象-Object类
Object类概述 是类层次结构的根类,每个类都直接或者间接继承该类. eg: 1.class Student extends Object{} //直接继承 2.class Student ext ...
- CDOJ 1060 秋实大哥与快餐店 字典树 水题
题目链接 B - 秋实大哥与快餐店 Time Limit:1000MS Memory Limit:65535KB 64bit IO Format:%lld & %llu Sub ...
- Shell中的特殊字符(三)
一 通配符 [root@192 test]# touch abc [root@192 test]# touch abcd [root@192 test]# touch 012 [root@192 te ...