MyBatis-關聯表增刪改查配置、resultMap、association、collection的使用

相關代碼參考:
MyBatis-搭建MyBatis開發環境一(MyEclipse版)

1、數據庫表信息

這裏寫圖片描述

實體類代碼

1、Dept

public class Dept {

    private Integer deptno;
    private String dname;
    private String loc;

    //get set方法略
}

2、Emp

public class Emp {

    private Integer empno;
    private String ename;
    private String job;
    private Integer mgr;
    private Date hiredate;
    private double sal;
    private double comm;
    private int deptno;

    //get set 方法略
}

2、子查詢

示例:在Emp表中查詢經理爲BLAKE的僱員信息

EmpMapper.xml配置

<!-- 查詢經理名稱爲BLAKE的僱員信息 -->
<select id="getEmpListByMgrIsBlake" resultType="Emp">
    select * from emp where mgr in (
        select empno from emp where ename = 'BLAKE')
</select>

測試代碼

    /**查詢經理名稱爲BLAKE的僱員信息 */
    public void getEmpListByMgrIsBlake() {
        SqlSession sqlSession = null;
        List<Emp> empList = new ArrayList<Emp>();
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            //在EmpMapper接口中添加對應方法
            empList = sqlSession.getMapper(EmpMapper.class).getEmpListByMgrIsBlake();
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
            sqlSession.rollback();
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        for (Emp emp : empList) {
            logger.debug(emp.getEname() + "==>" + emp.getDeptno());
        }
    }

3、內連接

示例一:查詢指定部門編號的僱員信息及部門信息

實體類代碼(新建一個實體類接收或者在已有實體類中添加屬性)

public class EmpAndDept {
    private Integer empno;
    private String ename;
    private String job;
    private Integer mgr;
    private Date hiredate;
    private double sal;
    private double comm;
    private int deptno;

    private String dname;
    private String loc;

    //get set 方法略
}

EmpMapper.xml配置

<select id="getEmpAndDeptByDeptno" resultType="EmpAndDept" parameterType="Dept">
    select * from emp e inner JOIN dept d on e.DEPTNO=d.DEPTNO 
        where e.deptno = #{deptno}
</select>

測試代碼

    /**查詢指定部門編號的僱員信息及部門信息*/
    public void getEmpAndDeptByDeptno() {
        SqlSession sqlSession = null;
        List<EmpAndDept> empAndDeptList = new ArrayList<>();
        Dept dept = new Dept();
        //查詢部門編號10
        dept.setDeptno(10);
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            //在EmpMapper接口中添加對應方法
            empAndDeptList = sqlSession.getMapper(EmpMapper.class).getEmpAndDeptByDeptno(dept);
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
            sqlSession.rollback();
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        for (EmpAndDept empAndDept : empAndDeptList) {
            logger.debug(empAndDept.getEname() + "==>" + empAndDept.getDname());
        }
    }

示例二:配置resultMap查詢指定部門名稱的僱員姓名及部門編號、部門地址

EmpMapper.xml配置

<resultMap type="EmpAndDept" id="EmpAndDeptByDname">
    <result property="ename" column="ename"/>
    <result property="deptno" column="e_dept"/>
    <result property="loc" column="loc"/>
</resultMap>

<select id="getEmpAndDeptByDname" resultMap="EmpAndDeptByDname" parameterType="Dept">
    select ename,e.deptno as e_dept,loc as loc from emp e inner join dept d on 
        e.DEPTNO=d.DEPTNO where dname = #{dname}
</select>

對於只想獲取指定的幾個字段信息,可以指定resultMap屬性,此屬性不能與resultType共存
這裏寫圖片描述

測試代碼

    /**查詢指定部門名稱的僱員姓名及部門編號、部門地址*/
    public void getEmpAndDeptByDname() {
        SqlSession sqlSession = null;
        List<EmpAndDept> empAndDeptList = new ArrayList<>();
        Dept dept = new Dept();
        //查詢部門名稱爲ACCOUNTING
        dept.setDname("ACCOUNTING");
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            //EmpMapper接口中添加此對應方法
            empAndDeptList = sqlSession.getMapper(EmpMapper.class).getEmpAndDeptByDname(dept);
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
            sqlSession.rollback();
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        for (EmpAndDept empAndDept : empAndDeptList) {
            logger.debug(empAndDept.getEname() + "==>" + empAndDept.getDeptno() + "==>" + empAndDept.getLoc());
        }
    }

示例三:配置association查詢指定僱員編號的僱員姓名、所在部門編號、部門名稱

在Emp實體類中添加Dept屬性

public class Emp {

    private Integer empno;
    private String ename;
    private String job;
    private Integer mgr;
    private Date hiredate;
    private double sal;
    private double comm;
    private int deptno;

    //配置association添加一方對象
    private Dept dept;

    //get set方法略
}

EmpMapper.xml配置

<!-- 查詢指定僱員編號的僱員姓名、所在部門編號、部門名稱 -->
<resultMap type="Emp" id="EmpAndDeptByEmpno">
    <!-- 指定主鍵,提高查詢效率 -->
    <id property="empno" column="empno"/>
    <result property="ename" column="ename"/>
    <result property="deptno" column="e_deptno"/>
    <!-- 配置association方式一 
    <association property="dept" javaType="Dept">
        <result property="dname" column="dname"/>
    </association>
    -->
    <!-- 配置association方式二 -->
    <association property="dept" javaType="Dept" resultMap="DeptInfo"/>
</resultMap>
<!-- association使用的resultMap -->
<resultMap type="Dept" id="DeptInfo">
    <result property="dname" column="dname"/>
</resultMap>

<select id="getEmpAndDeptByEmpno" resultMap="EmpAndDeptByEmpno" parameterType="Emp">
    select ename,e.deptno as e_deptno,dname from emp e inner join dept d on 
        e.deptno=d.DEPTNO where e.empno = #{empno}
</select>

可以看出,association用來配置本類中封裝的其他類型對象,可以用於多對一中多方實體類中,配置方式有倆種。
這裏寫圖片描述

測試代碼

    /**查詢指定僱員編號的僱員姓名、所在部門編號、部門名稱*/
    public void getEmpAndDeptByEmpno() {
        SqlSession sqlSession = null;
        List<Emp> empAndDeptList = new ArrayList<>();
        Emp emp = new Emp();
        //查詢僱員編號爲7788的僱員
        emp.setEmpno(7788);
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            //EmpMapper接口中添加對應方法
            empAndDeptList = sqlSession.getMapper(EmpMapper.class).getEmpAndDeptByEmpno(emp);
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
            sqlSession.rollback();
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        for (Emp emp2 : empAndDeptList) {
            logger.debug(emp2.getEname() + "==>" + emp2.getDeptno() + "==>" + emp2.getDept().getDname());
        }
    }

示例四:配置collection查詢指定部門編號下所有僱員的部門編號、部門名稱、僱員編號、僱員名稱

在Dept實體類中添加Emp集合

public class Dept {

    private Integer deptno;
    private String dname;
    private String loc;

    // collection 添加多方屬性,爲集合
    private List<Emp> empList;

    //get set 方法略
}

EmpMapper.xml配置

<!-- 查詢指定部門編號下所有僱員的部門編號、部門名稱、僱員編號、僱員名稱 -->
<resultMap type="Dept" id="DeptByDeptno">
    <id property="deptno" column="deptno"/>
    <result property="dname" column="dname"/>
    <!-- 配置collection方式一 
    <collection property="empList" ofType="Emp">
        <id property="empno" column="empno"/>
        <result property="ename" column="ename"/>
    </collection>
    -->
    <!-- 配置collection方式二 -->
    <collection property="empList" ofType="Emp" resultMap="EmpInfo"/>
</resultMap>
<!-- collection使用的resultMap -->
<resultMap type="Emp" id="EmpInfo">
    <id property="empno" column="empno"/>
    <result property="ename" column="ename"/>
</resultMap>

<select id="getDeptByDeptno" resultMap="DeptByDeptno" parameterType="Dept">
    select d.deptno,dname,empno,ename from dept d inner join emp e on 
        d.DEPTNO=e.DEPTNO where d.DEPTNO = #{deptno}
</select>

可以看出collection用於配置一對多中一方中包含的多方集合屬性

這裏寫圖片描述

測試代碼

    /**查詢指定部門編號下所有僱員的部門編號、部門名稱、僱員編號、僱員名稱*/
    public void getDeptByDeptno() {
        SqlSession sqlSession = null;
        Dept dept = new Dept();
        //查詢部門編號爲20的部門員工信息
        dept.setDeptno(20);
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            //EmpMapper接口中添加對應方法
            dept = sqlSession.getMapper(EmpMapper.class).getDeptByDeptno(dept);
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
            sqlSession.rollback();
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        System.out.println("部門信息:" + dept.getDeptno() + "==>" + dept.getDname());
        System.out.println("僱員信息如下:");
        for (Emp emp : dept.getEmpList()) {
            System.out.println(emp.getEmpno() + "-->" + emp.getEname());
        }
    }

關於select標籤一些屬性功能如下:
1、id:命名空間內唯一標識符,用來被引用這條語句。
2、parameterType:用來指定傳入參數的完全限定名或別名。
3、resultType:指定返回的期望的完全限定名或別名,如果是集合,應該爲集合包含的類型 ,不能和resultMap共存。
4、resultMap:命名引用外部的resultMap。
5、flushCache:默認false,設置爲true,則不論語句什麼時候被調用,都會清空緩存。
6、useCache:默認值true,會緩存本次語句結果。
7、timeout:這個設置驅動程序等待數據庫返回請求結果,並拋出異常時間的最大等待值。默認不設置(驅動自行處理)。
8、fetchSize:暗示程序每次返回的結果行數。
9、statementType:STATEMENT、PREPARED、CALLABLE中的一種,讓MyBatis選擇使用Statement、PreparedStatement、CallableStatement。默認是PREPARED。
10、resultSetType:FORWARD_ONLY|SCROOL_SENSITIVE|SCROLL_INSENSITIVE中的一種,默認是不設置(驅動自行處理)。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章