首页 > 其他分享 >Mybatis 04 一对一,一对多,多对多关联映射

Mybatis 04 一对一,一对多,多对多关联映射

时间:2023-09-20 11:22:08浏览次数:51  
标签:return String 映射 void private Mybatis deptno public 04

一对一 一个员工对应一个部门

实体类 emp
package com.entity;

import java.io.Serializable;
import java.math.BigDecimal;

public class EmpVo implements Serializable {
    private Integer empno;
    private String ename;
    private String job;
    private Integer mgr;
    private String hiredate;
    private BigDecimal sal;
    private BigDecimal comm;

    private Dept dept;//一个员工对应一个部门

    public Dept getDept() {
        return dept;
    }

    public void setDept(Dept dept) {    this.dept = dept;    }

    public Integer getEmpno() {        return empno;    }

    public void setEmpno(Integer empno) {   this.empno = empno;    }

    public String getEname() {        return ename;    }

    public void setEname(String ename) {        this.ename = ename;    }

    public String getJob() {        return job;    }

    public void setJob(String job) {        this.job = job;    }

    public Integer getMgr() {        return mgr;    }

    public void setMgr(Integer mgr) {        this.mgr = mgr;    }

    public String getHiredate() {        return hiredate;    }

    public void setHiredate(String hiredate) {this.hiredate =hiredate; }

    public BigDecimal getSal() {        return sal;    }

    public void setSal(BigDecimal sal) {        this.sal = sal;    }

    public BigDecimal getComm() {        return comm;    }

    public void setComm(BigDecimal comm) {        this.comm = comm;    }


    @Override
    public String toString() {
        return "EmpVo{" +
                "empno=" + empno +
                ", ename='" + ename + '\'' +
                ", job='" + job + '\'' +
                ", mgr=" + mgr +
                ", hiredate='" + hiredate + '\'' +
                ", sal=" + sal +
                ", comm=" + comm +
                '}';
    }
}

接口 EmpMapper
public interface EmpMapper {
    //多表查询
    List<EmpVo> selectEmpDeptMapping();
}
配置 EmpMapper.xml
 <resultMap id="empDeptMapping" type="empVo">
        <id column="empno" property="empno"></id>
        <result column="ename" property="ename"></result>
        <result column="job" property="job"></result>
        <result column="mgr" property="mgr"></result>
        <result column="hiredate" property="hiredate"></result>
        <result column="sal" property="sal"></result>
        <result column="comm" property="comm"></result>
        <association property="dept" javaType="dept">
            <id column="deptno" property="deptno"></id>
            <result column="dname" property="dname"></result>
            <result column="loc" property="loc"></result>
        </association>
    </resultMap>
    <select id="selectEmpDeptMapping" resultMap="empDeptMapping">
        select e.*,d.* from emp e,dept d where e.deptno=d.deptno
    </select>
test
    @Test
    public void selectEmpDeptMapping() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        List<EmpVo> empList = empMapper.selectEmpDeptMapping();
        for (EmpVo empVo : empList) {
            System.out.println(empVo+"---"+empVo.getDept());
        }

        sqlSession.close();
    }

image

一对多 一个部门多个员工

实体类 Dept
package com.entity;

import java.util.List;

public class Dept {
    private Integer deptno;
    private String dname;
    private String loc;
    List<EmpVo> empVos; //一个部门多个员工

    public List<EmpVo> getEmpVos() {
        return empVos;
    }

    public void setEmpVos(List<EmpVo> empVos) {
        this.empVos = empVos;
    }

    public Integer getDeptno() {
        return deptno;
    }

    public void setDeptno(Integer deptno) {
        this.deptno = deptno;
    }

    public String getDname() {
        return dname;
    }

    public void setDname(String dname) {
        this.dname = dname;
    }

    public String getLoc() {
        return loc;
    }

    public void setLoc(String loc) {
        this.loc = loc;
    }

    public Dept(Integer deptno, String dname, String loc) {
        this.deptno = deptno;
        this.dname = dname;
        this.loc = loc;
    }

    public Dept() {
    }

    @Override
    public String toString() {
        return "Dept{" +
                "deptno=" + deptno +
                ", dname='" + dname + '\'' +
                ", loc='" + loc + '\'' +
                '}';
    }
}

接口类
public interface EmpMapper {
      List<Dept> selectDeptEmpMapping(); //查询各个部门的员工信息
}
映射
   <!--一对多 一个部门多个员工-->
    <resultMap id="deptEmpMapping"  type="dept">
        <id column="deptno" property="deptno"></id>
        <result column="dname" property="dname"></result>
        <result column="loc" property="loc"></result>
        <collection property="empVos" ofType="empVo">
            <id column="empno" property="empno"></id>
            <result column="ename" property="ename"></result>
            <result column="job" property="job"></result>
            <result column="mgr" property="mgr"></result>
            <result column="hiredate" property="hiredate"></result>
            <result column="sal" property="sal"></result>
            <result column="comm" property="comm"></result>
        </collection>
    </resultMap>
    <select id="selectDeptEmpMapping" resultMap="deptEmpMapping" >
        select e.*,d.* from emp e,dept d where e.deptno=d.deptno
    </select>
test
  /*配置 一对多关联映射 一个部门对应多个员工*/
    @Test
    public void selectDeptEmpMapping() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        List<Dept> deptList = empMapper.selectDeptEmpMapping();
        for (Dept dept : deptList) {
            System.out.println(dept+"---"+dept.getEmpVos());
        }
        sqlSession.close();
    }

image

标签:return,String,映射,void,private,Mybatis,deptno,public,04
From: https://www.cnblogs.com/oyww-2027/p/17716855.html

相关文章

  • Tangram Tutorial 1:映射小鼠皮层 MOp 的 snRNAseq 数据到 Slide-seq2 数据
    写在前面学习一个软件最好的方法就是啃它的官方文档。本着自己学习、分享他人的态度,分享官方文档的中文教程。软件可能随时更新,建议配合官方文档一起阅读。推荐先按顺序阅读往期内容:文献篇:1.文献阅读:使用Tangram进行空间解析单细胞转录组的深度学习和比对:::block-1目......
  • Ubuntu22.04运行Splash Docker镜像报错,cloud not load the Qt platform xcb in even t
    一、问题描述Ubuntu22.04运行SplashDocker镜像报错,cloudnotloadtheQtplatformxcbineventhoughitwasfound奇怪的地方在于,停止运行,重新执行同样的语句,即恢复正常。本文只做了问题记录,没有深入研究原因。sudodockerrun-it-p8050:8050scrapinghub/splash二......
  • MyBatis中#和$的区别
    MyBatis中#{}和${}的用法说一下为什么要写这篇文章,最近面试有被问到,一下子想不出来有啥区别,想记录一下加深自己的理解,同时自己也经常用MyBatis-Plus忽略了XML文件的编写和使用,所以需要加深一下这块的知识一、例子1、#{}将传入的数据当作一个字符串,会对传入的数据加上一个双......
  • 为什么我的 ubuntu22.04 每安装一个软件,就需要重启很多服务?
    在一般情况下,安装一个软件通常不需要重启系统或服务。但是,有些软件的安装可能需要重启相关的服务或重新加载配置。以下是可能导致需要重启服务的几种情况:1.安装依赖关系:某些软件可能依赖于其他服务或库。在安装这些软件时,系统可能需要重启相关的服务以确保依赖项的正确配置和加载......
  • MyBatis
    MyBatis本是apache的一个开源项目iBatis,2010年这个项目由apachesoftwarefoundation迁移到了googlecode,并且改名为MyBatis。2013年11月迁移到Github。iBATIS一词来源于“internet”和“abatis”的组合,是一个基于Java的持久层框架。iBATIS提供的持久层框架包括SQLMaps和DataAc......
  • 04身份认证:除了账号密码,我们还能怎么做身份认证?
    高效安全的对称加密算法,解决密钥分发难题的加密算法(防止对称密钥泄露),以及提供单向加密的三列算法问题现在公司有好几个应用,每一个应用都有独立的账号体系,管理起来失分复杂。而且,内部员工的账号系统也没有建设起来,应该怎么解决这些问题?身份认证分为两个部分:对外认证和对内认证......
  • case04数据结构字典
    编程提示·针对字典而言,输出字典的键运用keys函数,输出字典的值运用values函数,如需遍历字典的全部元素则可以通过items函数完成。·在字典中新增键和值,可以运用update函数;删除相应的键和值,则可以运用del命令。汇率变量日期中间价涨跌幅(%)美元兑人民币202......
  • mybatis
    MyBatis简介(了解)什么是MyBatisMyBatis本是apache的一个开源项目iBatis,2010年这个项目由apachesoftwarefoundation迁移到了googlecode,并且改名为MyBatis。2013年11月迁移到Github。MyBatis是一个优秀的持久层框架,它对jdbc的操作数据库的过程进行封装,使开发者只需......
  • Mybatis-Plus常见操作
    packagecom.ideas.system.service.impl;importcom.alibaba.fastjson.JSON;importcom.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;importcom.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;importcom.baomidou.mybatisplus......
  • 04_串口打印print函数
    串口打印print函数intfputc(intch,FILE*f){ HAL_UART_Transmit(&huart1,(uint8_t*)&ch,1,1000); returnch;}......