mybatis学习(四)(级联查询)

mybatis的级联是需要在resultMap中配置,可以是获得数据更加的方便和简洁,但是会增加系统的负担,一般到了三层及以上就不建议使用级联了,

级联有两种情况:

  1.一对一

  2.一对多

一对一的情况下使用association,

如一对一的配置如下:

<select id = "select02" resultMap="info02">
            select empno, ename, e.deptno, loc, dname from emp e, dept d where e.deptno = d.deptno
    </select>
            
    <resultMap type="Emp" id="info02" autoMapping="true"><!-- 一些表中的字段名和实体类中的属性名相同可以自匹配 -->
            <id column="empno" property="empno"></id> <!-- 主键必须要注值 -->
            <!-- 一个员工中只能有属于一个部门,从这个角度讲是属于一对一,使用association -->
             <!-- 属性是emp实体类中要注值的属性 -->
             <!-- javaType为dept的数据类型 -->
            <association property="dept" autoMapping="true" javaType="Dept" >
                <id column="deptno" property="deptno"/><!-- 主键列的对应 -->
            </association>
    </resultMap>

由于一对一在上一篇resultMap中的博客中使用过,这里就不详讲,

接下来详讲一下一对多,一对多使用collection

案例:查询不同部们中的员工信息,一个部门有多个员工,属于一对多的关系。两个表:dept、 emp。

属先要有两个实体类,如下:

package com.yc.mybatis;

import java.util.List;

public class Dept1 {
    private int deptno;
    private String dname;
    private String loc;
    
    private List<Emp1> emps ;

    public Dept1() {
        super();
    }

    public Dept1(int deptno, String dname, String loc, List<Emp1> emps) {
        super();
        this.deptno = deptno;
        this.dname = dname;
        this.loc = loc;
        this.emps = emps;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + deptno;
        result = prime * result + ((dname == null) ? 0 : dname.hashCode());
        result = prime * result + ((emps == null) ? 0 : emps.hashCode());
        result = prime * result + ((loc == null) ? 0 : loc.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Dept1 other = (Dept1) obj;
        if (deptno != other.deptno)
            return false;
        if (dname == null) {
            if (other.dname != null)
                return false;
        } else if (!dname.equals(other.dname))
            return false;
        if (emps == null) {
            if (other.emps != null)
                return false;
        } else if (!emps.equals(other.emps))
            return false;
        if (loc == null) {
            if (other.loc != null)
                return false;
        } else if (!loc.equals(other.loc))
            return false;
        return true;
    }

    @Override
    public String toString() {
        return "Dept1 [deptno=" + deptno + ", dname=" + dname + ", loc=" + loc + ", emps=" + emps + "]";
    }

    public int getDeptno() {
        return deptno;
    }

    public void setDeptno(int 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 List<Emp1> getEmps() {
        return emps;
    }

    public void setEmps(List<Emp1> emps) {
        this.emps = emps;
    }
    
}
Dept1
package com.yc.mybatis;

public class Emp1 {
    private int empno;
    private int deptno;
    private String ename;
    @Override
    public String toString() {
        return "Emp1 [empno=" + empno + ", deptno=" + deptno + ", ename=" + ename + "]";
    }
    public int getEmpno() {
        return empno;
    }
    public void setEmpno(int empno) {
        this.empno = empno;
    }
    public int getDeptno() {
        return deptno;
    }
    public void setDeptno(int deptno) {
        this.deptno = deptno;
    }
    public String getEname() {
        return ename;
    }
    public void setEname(String ename) {
        this.ename = ename;
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + deptno;
        result = prime * result + empno;
        result = prime * result + ((ename == null) ? 0 : ename.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Emp1 other = (Emp1) obj;
        if (deptno != other.deptno)
            return false;
        if (empno != other.empno)
            return false;
        if (ename == null) {
            if (other.ename != null)
                return false;
        } else if (!ename.equals(other.ename))
            return false;
        return true;
    }
    public Emp1(int empno, int deptno, String ename) {
        super();
        this.empno = empno;
        this.deptno = deptno;
        this.ename = ename;
    }
    public Emp1() {
        super();
    }
}
Emp1

再Dept1中有个属性:emps,这个属性的类型是:List<Emp1>,用于保存Emp员工信息

创建好实体类后,接下来就是mapper的文件配置

如下:

<select id="select03" resultMap="info03">
        select d.dname, d.deptno, d.loc, e.ename, e.empno from dept d, emp e where e.deptno = d.deptno
    </select>
    <resultMap id="info03" type="Dept1" autoMapping="true">
        <id column="deptno" property="deptno"/>     <!--  column为表中的字段名,property为Dept1中的属性名 --> 
        <collection property="emps" autoMapping="true" ofType="Emp1">
            <id column="empno" property="empno"/>
        </collection>
    </resultMap>
    

测试类:

package com.yc.mybatis;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;



public class TestTest01 {
    InputStream is = null;
    SqlSessionFactory factory = null;
    SqlSession session = null;
    
    {
        try {
            is = Resources.getResourceAsStream("mybatis-config.xml");
            factory = new SqlSessionFactoryBuilder().build(is);
            session = factory.openSession();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    @Test
    public void TTest03(){
        List<Dept1> list = session.selectList("TTest.select03");//mapper文件的命名空间和要操作的id
        for(Dept1 d : list){
            System.out.println(d);
        }
    }
    
}

结果:

级联目前到此。

原文地址:https://www.cnblogs.com/1998xujinren/p/11208303.html