zoukankan      html  css  js  c++  java
  • mybatis中的一对多的查询

    一对多分为单条sq语句l和多条sql语句

    下面就以员工和就职部门为例:

    部门实体类

    private Integer deptno;
    private String deptname;
    //植入员工实体集合
    private List<Emp> emps=new ArrayList<Emp>();

    public String getDeptname() {
    return deptname;
    }
    public void setDeptname(String deptname) {
    this.deptname = deptname;
    }

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

    public List<Emp> getEmps() {return emps;}
    public void setEmps(List<Emp> emps) {
    this.emps = emps;
    }

    员工实体类
    private Integer empno;
    private String empname;
    private Integer deptno;
    public Integer getEmpno() {return empno;}
    public void setEmpno(Integer empno) {
    this.empno = empno;
    }

    public String getEmpname() {
    return empname;
    }
    public void setEmpname(String empname) {
    this.empname = empname;
    }

    public Integer getDeptno() {return deptno;}
    public void setDeptno(Integer deptno) {
    this.deptno = deptno;
    }
     单条sql语句

    接口

    /**
    * 根据部门对象编号,查询部门对象,对象里面包含的员工集合
    *一对多,一条sql语句
    * @param deptno
    * @return
    */
    public Dept findEmpByDept(int deptno);


    xml文件(小配置)
    <!--根据部门对象编号,查询部门对象,对象里面包含的员工集合
    一对多 一条sql语句
    -->
    <resultMap id="deptMapper" type="Dept">
    <id column="deptno" property="deptno"></id>
    <result column="deptname" property="deptname"></result>
    <collection property="emps" ofType="Emp">
    <id column="empno" property="empno"></id>
    <result column="empname" property="empname"></result>
    </collection><!--由于此处是集合,所以用cillection-->
    </resultMap>

    <select id="findEmpByDept" resultMap="deptMapper">
    SELECT dept.deptno,dept.deptname,empname,empno
    FROM Dept,Emp
    WHERE dept.deptno=emp.deptno AND emp.deptno=#{deptno}
    </select>


    测试类

    /**
    * 根据部门对象编号,查询部门对象,对象里面包含的员工集合
    * 一对多 一条sql语句
    */
    @Test
    public void findEmpByDept(){
    SqlSession session = MyBatisUtil.getSession();
    IDeptDAO mapper = session.getMapper(IDeptDAO.class);
    Dept dept = mapper.findEmpByDept(1);
    System.out.println(dept.getDeptname());
    for(Emp item:dept.getEmps()){
    System.out.println(item.getEmpname());
    }
    session.commit();
    session.close();
    }

    多条sql语句

    接口
    /**
    * 根据部门对象编号,查询部门对象,对象里面包含的员工集合
    *一对多,多条sql语句
    * @param deptno
    * @return
    */
    public Dept findEmpByDeptManySql(int deptno);



    xml文件(小配置)

    <!--根据部门对象编号,查询部门对象,对象里面包含的员工集合-->
    <!-- 一对多 多条sql语句-->
    <resultMap id="deptMapperManySql" type="Dept">
    <id column="deptno" property="deptno"></id>
    <result column="deptname" property="deptname"></result>
    <collection property="emps" ofType="Emp" select="getEmpByDeptNo" column="deptno"></collection>
    </resultMap>
    <select id="getEmpByDeptNo" resultType="Emp">
    SELECT *from Emp where deptno=#{deptno}
    </select>
    <select id="findEmpByDeptManySql" resultMap="deptMapperManySql">
    SELECT * FROM Dept WHERE deptno=#{deptno}
    </select>



    测试类

    /**
    * 根据部门对象编号,查询部门对象,对象里面包含的员工集合
    * 一对多 多条sql语句
    */
    @Test
    public void findEmpByDeptManySql(){
    SqlSession session = MyBatisUtil.getSession();
    IDeptDAO mapper = session.getMapper(IDeptDAO.class);
    Dept dept = mapper.findEmpByDeptManySql(3);
    System.out.println(dept.getDeptname());
    for(Emp item:dept.getEmps()){
    System.out.println(item.getEmpname());
    }
    session.commit();
    session.close();
    }




     
     
     


  • 相关阅读:
    C++的高效从何而来2
    初体验ajax跨域
    ACM在线测评系统评测程序设计与实现
    高效GTD云工具 Manage Your Time
    HTTP 长连接
    使用avalon MVVM框架打造整一套jquery ui
    GhostDoc(注释生成工具)使用方法
    NUnit快速入门 笔记
    ETags
    nodejs + edge + ejs + c#
  • 原文地址:https://www.cnblogs.com/sujulin/p/7588955.html
Copyright © 2011-2022 走看看