zoukankan      html  css  js  c++  java
  • 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在测试类中书写如下代码进行测试

    代码同上

  • 相关阅读:
    使用频率比较高的PHP函数方法
    penetration test:渗透测试
    penetration test:渗透测试
    按照数组的形式排序+条件切换切换查询
    PHP 性能优化技巧
    再谈PHP单引号和双引号区别
    表单验证的3个函数ISSET()、empty()、is_numeric()的使用方法
    向中级程序员转变必备的10个秘诀
    彻底杜绝PHP的session cookie错误
    Analysis by Its History_theorem 1.2
  • 原文地址:https://www.cnblogs.com/hmy-1365/p/6197857.html
Copyright © 2011-2022 走看看