一、创建部门表和员工表:

创建部门信息表`t_department`,其中包括`id`, `name`

CREATE TABLE t_department (
        id INT AUTO_INCREMENT,
        name VARCHAR(20) UNIQUE NOT NULL,
        PRIMARY KEY(id)
    ) DEFAULT CHARSET=UTF8;

往部门表中插入数据:

INSERT INTO t_department (name) VALUES 
        ('UI'), ('RD'), ('TEST');

创建员工信息表t_user

CREATE TABLE t_user(

  id INT PRIMARY KEY AUTO_INCREMENT,

  username VARCHAR(20) ,

  password VARCHAR(20) ,

  age int ,

  phone VARCHAR(20) ,

  email VARCHAR(20) ,

  is_Delete int

)DEFAULT CHARSET=UTF8;

往员工表中插入数据:

INSERT
INTO t_user
VALUES(null,'张三','123456',23,'110','11111@qq.com',1),(null,'历史','123456',23,'110','11111@qq.com',1),(null,'孙悟空','123456',23,'110','11111@qq.com',2),(null,'李白','123456',23,'110','11111@qq.com',2),(null,'八戒','123456',23,'110','11111@qq.com',2);

插入数据错误:

https://www.cnblogs.com/package-java/p/10494380.html

在查询时,`<select>`节点必须指定结果的类型,可以通过`resultType`属性来指定,也可以通过`resultMap`属性来指定。
 
当有直接对应的查询结果时,可以使用`resultType`,取值一般是实体类的类型,或VO类的类型。
 
某些查询可能需要将查询结果进行特殊的封装,例如查询时存在1对多、多对多、多对1等关系,则需要使用`resultMap`来配置封装的方式。

二、创建实体类:

1、Vo类的实体类,因为部门只有三个,三个部门里面可能有很多员工,所以把员工封装到List集合中,由于将多条信息明细存储到了list中,因此查询后将不再出现重复数据,达到了去重的效果

package cn.tedu.mybatis.vo;

import java.io.Serializable;
import java.util.List; import cn.tedu.mybatis.entity.User; public class DepartmentVO implements Serializable { private static final long serialVersionUID = -6442405812964981459L; private Integer did;
private String name;
private List<User> users; public Integer getDid() {
return did;
} public void setDid(Integer did) {
this.did = did;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public List<User> getUsers() {
return users;
} public void setUsers(List<User> users) {
this.users = users;
} @Override
public String toString() {
return "DepartmentVO [did=" + did + ", name=" + name + ", users=" + users + "]";
} }

2、员工类:

package cn.tedu.mybatis.entity;

import java.io.Serializable;

public class User implements Serializable {

    private static final long serialVersionUID = 7323921614984096421L;

    private Integer id;
private String username;
private String password;
private Integer age;
private String phone;
private String email;
private Integer isDelete;
private Integer did; public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
} public String getPhone() {
return phone;
} public void setPhone(String phone) {
this.phone = phone;
} public String getEmail() {
return email;
} public void setEmail(String email) {
this.email = email;
} public Integer getIsDelete() {
return isDelete;
} public void setIsDelete(Integer isDelete) {
this.isDelete = isDelete;
} public Integer getDid() {
return did;
} public void setDid(Integer did) {
this.did = did;
} @Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password=" + password + ", age=" + age + ", phone="
+ phone + ", email=" + email + ", isDelete=" + isDelete + ", did=" + did + "]";
}
}

三、创建接口和抽象方法:

package cn.tedu.mybatis.mapper;

import cn.tedu.mybatis.vo.DepartmentVO;

public interface DepartmentMapper {

    DepartmentVO findById(Integer id);

}

四、SQL语句的映射:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd"> <mapper namespace="cn.tedu.mybatis.mapper.DepartmentMapper">
<!-- namespace:抽象类绝对路径 -->
<resultMap id="Department_VO_Map"
type="cn.tedu.mybatis.vo.DepartmentVO">
     <!-- id节点:配置主键 -->
<!-- type:实体类绝对路径 -->
<!-- column:查询结果中的列名 -->
<!-- property:以上type属性对应的数据类型中的属性名(类里面的属性) -->
<id column="did" property="did"/>
<!-- result节点:配置普通字段 -->
<result column="name" property="name"/>
<!-- collection节点:配置List集合类型的属性,用于1对多的查询 -->
<!-- ofType:在List里放的是什么类型 -->
<collection property="users"
ofType="cn.tedu.mybatis.entity.User">
<id column="uid" property="id"/>
<result column="username" property="username"/>
<result column="password" property="password"/>
<result column="age" property="age"/>
<result column="phone" property="phone"/>
<result column="email" property="email"/>
<result column="isDelete" property="isDelete"/>
</collection>
</resultMap> <select id="findById"
resultMap="Department_VO_Map">
SELECT
t_department.id AS did,
name,
t_user.id AS uid,
username,
password,
age,
phone,
email,
is_delete
FROM
t_department
INNER JOIN
t_user
ON
t_user.did=t_department.id
WHERE
t_department.id=#{id}
</select> </mapper>

> 以上代码中,自定义别名是因为需要区分查询结果中的列的名称,并不是因为需要与数据类型中的属性对应,关于查询结果的列名与数据类型的属性名的对应,可以通过`<resultMap>`中的配置来完成!

五、测试:

package cn.tedu.mybatis.mapper;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.tedu.mybatis.vo.DepartmentVO; public class DepartmentMapperTestCase { AbstractApplicationContext ac;
DepartmentMapper mapper; @Before
public void doBefore() {
ac = new ClassPathXmlApplicationContext("spring-dao.xml");
mapper = ac.getBean("departmentMapper", DepartmentMapper.class);
} @After
public void doAfter() {
ac.close();
} @Test
public void findById() {
Integer id = 2;
DepartmentVO data
= mapper.findById(id);
System.out.println(data);
} }

Mybatis中<resultMap>用法(主要用于一对多去重)的更多相关文章

  1. mybatis中resultMap配置细则

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

  2. Mybatis中resultMap的基础配置

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

  3. mybatis中resultMap的使用

    在mybatis中,使用<select>标签,必须要设置resultType属性 或 resultMap属性 否则会报错! resultType一般是返回简单类型的查询结果,涉及一张表 可 ...

  4. 在mybatis中resultMap与resultType的区别

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

  5. Mybatis中resultMap与resultType区别

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

  6. mybatis中resultMap引发的吐血bug

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

  7. MyBatis 中 resultMap 详解

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

  8. mybatis中实现一对一,一对多查询

    在实际的开发中我们经常用到的是一对一查询和一对多查询.而多对多的实现是通过中间来实现,这里就没有给出来了 比如: 订单和用户是一对一的关系(一个订单只能对应一个用户) 订单和订单明细是一对多的关系(一 ...

  9. MyBatis中collection (一对一,一对多)

    MyBatis学习:http://www.mybatis.org/mybatis-3/zh/index.html 大对象InsuranceDetailsVO: com.quicksure.mobile ...

随机推荐

  1. mysql安装出现 conflicts with mysql*的解决办法

    rpm -ivh Percona-Server-client-56-5.6.16-rel64.0.el6.x86_64.rpm --nodeps --force error: Failed depen ...

  2. hdu 3416 Marriage Match IV 【 最短路 最大流 】

    求边不可重复的最短路条数 先从起点到终点用一次dijkstra,再从终点到起点用一次dijkstra,来判断一条边是否在最短路上 如果在,就将这条边的两个端点连起来,容量为1 再跑一下dinic(), ...

  3. ZBrush中Magnify膨胀笔刷介绍

    Magnify膨胀笔刷是ZBrush®笔刷中经常使用的,利用该笔刷可绘制中心向四周膨胀的效果.本文内容向大家介绍ZBrush®中膨胀笔刷以便大家熟悉它的用法和特性. Magnify膨胀笔刷 Magni ...

  4. Oracle查询优化之减少统计的数据量

    统计各部门人数很简单,通过部门分组即可,要统计部门以下下级部门的人数也简单,通过递归.要统计所有有下级部门的部门人数(包含下级)页比较简单, 先查询出有下级的部门,在对每个部门进行递归查询,如下: ) ...

  5. Codeforces Round #506 (Div. 3) D-F

    Codeforces Round #506 (Div. 3) (中等难度) 自己的做题速度大概只尝试了D题,不过TLE D. Concatenated Multiples 题意 数组a[],长度n,给 ...

  6. freeswitch 中文语音

    1.下载中文语音包 链接:https://pan.baidu.com/s/1UODvqj8NAQw7_CRatfl0kg 提取码:qwdn 创建目录 /usr/local/freeswitch/sou ...

  7. rsync实时同步mysql数据库

    1.主机slave 注:没有包的可以去下载 yum -y install gcc gcc-c++ 上传包 rsync-3.1.3.tar.gz 使用tar命令解压 使用gcc gcc-c++编译 ./ ...

  8. Python及相应软件安装

    Python安装 这是下载地址:Linux下载链接,windows下载链接 1.下载压缩包 wget https://www.python.org/ftp/python/3.7.1/Python-3. ...

  9. 新手学python-Day2-变量和循环判断

    第二天作业: 初探三级菜单,凭现有知识,注意变量可以不声明,但要提前赋值! 此处shuru = '' 可以不写,因为第7行被赋值了,如果只调用shuru不赋值就会报错 shuru = '' sheng ...

  10. [terry笔记]对人员列表文件进行数据库操作

    原文件(数据已经脱敏): staff_id,name,age,phone,dept,enroll_date1,姬建明,25,152015410,运维,2013-11-012,刘海龙,26,186184 ...