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

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

创建部门信息表`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);
    }

}

原文地址:https://www.cnblogs.com/package-java/p/10495912.html

时间: 2024-08-29 04:09:14

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

在mybatis中resultMap与resultType的区别

MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMapresultType是直接表示返回类型的,而resultMap则是对外部ResultMap的引用但是resultType跟resultMap不能同时存在.在MyBatis进行查询映射的时候,其实查询出来的每一个属性都是放在一个对应的Map里面的,其中键是属性名,值则是其对应的值.当提供的返回类型属性是resultType的时候,MyBatis会将Map里面的键值对取出赋给resultT

MyBatis中resultMap的相关介绍

resultMap 元素是 MyBatis 中最重要最强大的元素.它就是让你远离从结果集中取出数据的JDBC 代码的那个东西,而且在一些情形下允许你做一些JDBC不支持的事情.ResultMap 的设计就是对于一些简单的语句我们不需要明确它们的结果映射,但是到于复杂的语句确实需要描述它们的关系. 简单结果映射 对于一个普通的JavaBean: package com.someapp.model; public class User {     private int id;     privat

mybatis中resultMap与resultType的使用说明

一.mapper文件内容如下: <mapper namespace="com.miapsoft.dao.UserDao"> <resultMap type="User" id="UserMapper"> <result property="id" column="ID" /> <result property="name" column=&quo

mybatis中@Param用法

用注解来简化xml配置的时候,@Param注解的作用是给参数命名,参数命名后就能根据名字得到参数值,正确的将参数传入sql语句中 我们先来看Mapper接口中的@Select方法 package Mapper; public interface Mapper { @Select("select s_id id,s_name name,class_id classid from student where s_name= #{aaaa} and class_id = #{bbbb}")

mybatis中useGeneratedKeys用法--插入数据库后获取主键值

前言:今天无意在mapper文件中看到useGeneratedKeys这个词,好奇就查了下,发现能解决我之前插入有外键表数据时,这个外键获取繁琐的问题,于是学习敲DEMO记录    在项目中经常需要获取到插入数据的主键来保障后续操作,数据库中主键一般我们使用自增或者uuid()的方式自动生成 问题:对于uuid使用Java代码生成的方式还比较容易控制,然而使用数据库生成的主键,这样我们就需要将插入的数据再查询出来得到主键,某些情况下还可能查询到多条情况,这样就比较尴尬了. 那有什么办法来插入数据

[已解决] MyBatis 中bind用法

JAVA: TC_ENTR_FLOW selectFlowForUpdate(String ENTR_ID); XML: <select id="selectFlowForUpdate" resultMap="BaseResultMap" parameterType="java.lang.String"> <bind name="ENTR_ID" value="'%' + _parameter&qu

Mybatis中的resultType和resultMap

一.概述 MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap,resultType是直接表示返回类型的,而resultMap则是对外部ResultMap的引用,但是resultType跟resultMap不能同时存在. 在MyBatis进行查询映射时,其实查询出来的每一个属性都是放在一个对应的Map里面的,其中键是属性名,值则是其对应的值. ①当提供的返回类型属性是resultType时,MyBatis会将Map里面的键值对取出赋给r

mybatis中的resultMap

MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap,resultType是直接表示返回类型的,而resultMap则是对外部ResultMap的引用,但是resultType跟resultMap不能同时存在. 1.resultType 在MyBatis进行查询映射的时候,其实查询出来的每一个属性都是放在一个对应的Map里面的,其中键是属性名,值则是其对应的值.当提供的返回类型属性是resultType的时候,MyBatis会将Map里

Mybatis中collection实现一对多的问题

今天在使用Mybatis中的Collection获取集合信息时,数据库中对应了多条数据,但在做单元测试时只能获取到一条数据. 纠结了很久,突然想到是不是主键的问题,结果一试,还真是这么回事. Mybatis中id和result的唯一不同是id表示的结果将是当比较对象实例时用到的标识属性.这帮助来改进整体表现,特别是缓存和嵌入结果映射.因此在不同的数据中要对ID进行唯一处理,不然就会出现类似我只能查看一条数据. <resultMap type="OrderBaseInfo" id=