在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的使用的更多相关文章

  1. mybatis中resultMap配置细则

    resultMap算是mybatis映射器中最复杂的一个节点了,能够配置的属性较多,我们在mybatis映射器配置细则这篇博客中已经简单介绍过resultMap的配置了,当时我们介绍了resultMa ...

  2. 在mybatis中resultMap与resultType的区别

    MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMapresultType是直接表示返回类型的,而resultMap则是对外部ResultMa ...

  3. Mybatis中resultMap与resultType区别

    MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap,resultType是直接表示返回类型的,而resultMap则是对外部ResultM ...

  4. Mybatis中resultMap的基础配置

    一.概述 resultMap 元素是 MyBatis 中最重要最强大的元素.它就是让你远离 90%的需要从结果集中取出数据的 JDBC 代码的那个东西,而且在一些情形下允许你做一些 JDBC 不支持的 ...

  5. mybatis中resultMap引发的吐血bug

    简单的讲: 问题背景:如果在写mybatis中的resultMap时,不下心将resultMapde id写成映射接口的名字,会发生什么? 结论:单元测试进度条卡住但不报错, Tomcat运行不报错, ...

  6. MyBatis 中 resultMap 详解

    resultMap 是 Mybatis 最强大的元素之一,它可以将查询到的复杂数据(比如查询到几个表中数据)映射到一个结果集当中.如在实际应用中,有一个表为(用户角色表),通过查询用户表信息展示页面, ...

  7. Mybatis中<resultMap>用法(主要用于一对多去重)

    一.创建部门表和员工表: 创建部门信息表`t_department`,其中包括`id`, `name` CREATE TABLE t_department (         id INT AUTO_ ...

  8. Mybatis中resultMap的作用-解决实体类属性名和数据库字段不一致

    解决实体类属性名和数据库字段不一致

  9. MyBatis中resultType和resultMap的区别

    resultType和resultMap功能类似  ,都是返回对象信息  ,但是resultMap要更强大一些 ,可自定义.因为resultMap要配置一下,表和类的一一对应关系,所以说就算你的字段名 ...

随机推荐

  1. [每日一讲] Python系列:字典

    #! /usr/bin/python # coding:utf-8 """ DATA STRUCTURE Container: Mapping (Another cont ...

  2. 【leetcode】1220. Count Vowels Permutation

    题目如下: Given an integer n, your task is to count how many strings of length n can be formed under the ...

  3. 11.关于django的content_type表

    ****** Django的contenttype表中存放发的是app名称和模型的对应关系 contentType使用方式 - 导入模块 from django.contrib.contenttype ...

  4. 一图解决五大I/O模型比较

  5. (66)Nginx+lua+Redis开发

    一. 概述 Nginx是一个高性能,支持高并发的,轻量级的web服务器.目前,Apache依然web服务器中的老大,但是在全球前1000大的web服务器中,Nginx的份额为22.4%.Nginx采用 ...

  6. ElasticSearch的介绍

    1.ELK 1.1 集中式日志系统 日志,对于任何系统来说都是及其重要的组成部分.在计算机系统里面,更是如此.但是由于现在的计算机系统大多比较复杂,很多系统都不是在一个地方,甚至都是跨国界的:即使是在 ...

  7. 大哥带的mssql注入拿shell

    任务二 注入点拿shell 路径的爆破 https://blog.csdn.net/edu_aqniu/article/details/78409451 0X01判断 是否为判断当前数据库用户名是否为 ...

  8. [CSP-S模拟测试]:椎(线段树维护区间最值和单调栈)

    题目描述 虽不能至,心向往之. $Treap=Tree+Heap$ 椎$=$树$+$堆 小$\pi$学习了计算机科学中的数据结构$Treap$. 小$\pi$知道$Treap$指的是一种树. 小$\p ...

  9. mysql中对比 JSON_VALUE 与 JSON_QUERY

    1. JSON概述 MySQL里的json分为json array和json object. $表示整个json对象,在索引数据时用下标(对于json array,从0开始)或键值(对于json ob ...

  10. gdb break 断点设置

    http://sourceware.org/gdb/current/onlinedocs/gdb/ 断点设置 gdb断点分类: 以设置断点的命令分类: breakpoint 可以根据行号.函数.条件生 ...