• MyBatis 系列五 之 关联映射


                                                                     MyBatis 系列五 之 关联映射

    一对多的关联映射

    一对多关联查询多表数据

    1.1在MyBatis映射文件中做如下配置

    <!--一对多单向的连接两表的查询-->
    <resultMap type="Dept" id="deptMapper">
        <id property="deptNo" column="deptNo"/>
        <result property="deptName" column="deptName"/>
        <collection property="emps" ofType="Emp">
        <id property="empNo" column="empNo"/>
        <result property="empName" column="empName"/>
        </collection>
    </resultMap>
    <sql id="columns">
      dept.deptNo,deptName,empNo,empName 
    </sql>
    
    <select id="findDeptInfoById" resultMap="deptMapper">
       select <include refid="columns"/> from dept,emp 
       where dept.deptNo=emp.deptNo and dept.deptNo=#{deptNo}
    
    </select> 

    1.2在接口类中定义与映射文件查询语句的id值相同的方法名

    package cn.hmy.dao;
    
    
    import cn.hmy.entity.Dept;
    
    public interface DeptDao {
        //根据编号查询部门信息
        public Dept findDeptInfoById(Dept dept);
    
    }

    1.3在测试类中书写一下代码进行测试

    package cn.hmy.test;
    
    import java.io.IOException;
    import java.util.List;
    
    import org.apache.ibatis.session.SqlSession;
    import org.junit.Before;
    import org.junit.Test;
    
    import cn.hmy.dao.DeptDao;
    import cn.hmy.entity.Dept;
    import cn.hmy.util.MyBatisUtil;
    
    public class MyTest {
        DeptDao deptDao;
        @Before
        public void myBefore(){
            SqlSession session = MyBatisUtil.getSession();
            deptDao = session.getMapper(DeptDao.class);
        }
        
        
        
        /**
         * 根据编号查找部门信息
         * @throws IOException
         */
        @Test
        public void findDeptInfoById() throws IOException{
            Dept dept=new Dept();
            dept.setDeptNo(3);
            Dept dept2 = deptDao.findDeptInfoById(dept);
            System.out.println(dept2);
            
        }
        
    }

    一对多的单张表的查询

    1.1在MyBatis的映射文件中配置如下文件

    <!--一对多单向的单张表的查询  -->
    <select id="selectEmpByDeptNo" resultType="Emp">
      select empNo,empName from Emp where deptNo=#{deptNo}
    </select>
    <sql id="columns">
    deptNo,deptName
    </sql>
    <resultMap type="Dept" id="deptMapper">
       <id property="deptNo" column="deptNo"/>
        <result property="deptName" column="deptName"/>
        <collection property="emps" ofType="Emp"
         select="selectEmpByDeptNo"
         column="deptNo"
        />
    </resultMap>
    
    <select id="findDeptInfoById" resultMap="deptMapper">
    select <include refid="columns"/> from Dept
    where deptNo=#{deptNo}
    </select>

    1.2在接口类中定义与映射文件查询语句的id值相同的方法名

    1.3在测试类中书写一下代码进行测试

    均如上

    多对一的关联映射

    多对一多表查询的配置

    1.1在MyBatis的映射文件中,配置如下信息

    <!-- 一对多单向的连接两表的查询 -->
    <resultMap type="Emp" id="empMapper">
        <id property="empNo" column="empNo"/>
        <result property="empName" column="empName"/>
        <association property="dept" javaType="Dept">
          <id property="deptNo" column="deptNo"/>
          <result property="deptName" column="deptName"/>
        </association>
    </resultMap>
    <sql id="columns">
      dept.deptNo,deptName,empNo,empName 
    </sql>
    
    <select id="findEmpById" resultMap="empMapper">
       select <include refid="columns"/> from dept,emp 
       where dept.deptNo=emp.deptNo and empNo=#{empNo}
    
    </select>

    1.2在接口类中定义一个和映射文件的查询语句的id值相同的方法名

    package cn.hmy.dao;
    
    
    import cn.hmy.entity.Emp;
    
    public interface EmpDao {
        //根据员工编号查找员工信息
        public Emp findEmpById(Emp emp);
    
    }

    1.3在测试类中书写如下代码进行测试

    package cn.hmy.test;
    
    import java.io.IOException;
    import java.util.List;
    
    import org.apache.ibatis.session.SqlSession;
    import org.junit.Before;
    import org.junit.Test;
    
    import cn.hmy.dao.EmpDao;
    import cn.hmy.entity.Dept;
    import cn.hmy.entity.Emp;
    import cn.hmy.util.MyBatisUtil;
    
    public class MyTest {
        EmpDao empDao;
        @Before
        public void myBefore(){
            SqlSession session = MyBatisUtil.getSession();
            empDao = session.getMapper(EmpDao.class);
        }
        
        
        
        /**
         * 根据员工编号查找员工信息
         * @throws IOException
         */
        @Test
        public void findEmpById() throws IOException{
            Emp emp=new Emp();
            emp.setEmpNo(2);
            Emp emp2 = empDao.findEmpById(emp);
            System.out.println(emp2);
            
        }
        
    }

    多对一的单表的查询配置

    1.1在MyBatis的映射文件中配置如下信息

    <!-- 多对一单向的连接单表的查询 -->
    <select id="selectDeptInfoBydeptNo" resultType="Dept">
      select deptNo,deptName from dept where deptNo=#{deptNo}
    </select>
    
    <resultMap type="Emp" id="empMapper">
        <id property="empNo" column="empNo"/>
        <result property="empName" column="empName"/>
        <association property="dept" javaType="Dept"
           select="selectDeptInfoBydeptNo"
           column="deptNo"
        />
    </resultMap>
    <sql id="columns">
      empNo,empName,deptNo
    </sql>
    
    <select id="findEmpById" resultMap="empMapper">
       select <include refid="columns"/> from emp 
       where empNo=#{empNo}
    
    </select>

    1.2在接口类中定义一个和映射文件的查询语句的id值相同的方法名

    1.3在测试类中书写如下代码进行测试

    代码同上

  • 相关阅读:
    lr 增强窗格中,如何生成调试信息?
    lr 自带的例子,如何进行关联,通过代码的函数进行实现
    lr11 录制脚本时候,无法自动启动ie,查了网上很多方法都未解决?
    loadrunner11 录制脚步不成功,在录制概要出现“No Events were detected”,浮动窗口总是显示“0 Events”,解决办法
    loadrunner11 安装及破解教程来自百度文库
    安装loadrunner11 ,出现如下错误如何解决?
    回收站数据删除了,如何进行恢复?
    网管工作方面——————打印机删除了然后开机重启他依然存在,如何解决
    Windows 不能在 本地计算机 启动 SQL Server 服务 错误代码126
    Sorry, the page you are looking for is currently unavailable. Please try again later. Nginx
  • 原文地址:https://www.cnblogs.com/hmy-1365/p/6197857.html
Copyright © 2020-2023  润新知