MyBatis-关联表增删改查配置、resultMap、association、collection的使用

来源:互联网 发布:重生之网络娱乐txt全集 编辑:程序博客网 时间:2024/04/29 05:55

相关代码参考:
MyBatis-搭建MyBatis开发环境一(MyEclipse版)

1、数据库表信息

这里写图片描述

实体类代码

1、Deptpublic class Dept {    private Integer deptno;    private String dname;    private String loc;    //get set方法略}2、Emppublic 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中的一种,默认是不设置(驱动自行处理)。

1 0
原创粉丝点击