DepartmentMapper.xml sql配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace不能随便自定义了,应该是接口类的全限定名 -->
<mapper namespace="com.atChina.dao.DepartmentMapper">
<select id="getDepartByDeptno" resultType="com.atChina.bean.Department">
select * from depttest a where a.deptno = #{deptno}
</select>
</mapper>
EmployeePlusMapper.xml sql配置文件
<resultMap type="com.atChina.bean.Employee" id="defineEmpStep">
<id column="empno" property="empno" />
<result column="ename" property="ename" />
<result column="job" property="job" />
<result column="mgr" property="mgr" />
<result column="hiredate" property="hiredate" />
<result column="sal" property="sal" />
<!-- association可以指定联合的javaBean对象
select:表明当前属性是调用select指定的方法查出的结果
column:指定将哪一列的值传给这个方法
流程: 使用select指定的方法(传入colomn指定的这列作为参数的值)查出对象,
并封装给proterty指定的属性.
-->
<association property="dt"
select="com.atChina.dao.DepartmentMapper.getDepartByDeptno"
column="deptno">
</association>
</resultMap>
<select id="getEmpAndDeptByStep" resultMap="defineEmpStep">
select *
from emptest a where a.empno = #{empno}
</select>
测试方法:
@Test
public void test25() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession openSession = sqlSessionFactory.openSession();
try{
// 命名空间.id,这样别的配置文件里有同名的id,程序也不报错
EmployeePlusMapper em = openSession.getMapper(EmployeePlusMapper.class);
System.out.println(em.getClass()); // 动态代理类
// Employee ee = em.getEmpAndDeptByEmpno(7369);
Employee ee = em.getEmpAndDeptByStep(7369);
System.out.println(ee);
}finally{
// 关闭
openSession.close();
}
}
测试结果: 分步执行了两条sql语句
DEBUG com.atChina.dao.EmployeePlusMapper.getEmpAndDeptByStep - ==> Preparing: select * from emptest a where a.empno = ?
DEBUG com.atChina.dao.EmployeePlusMapper.getEmpAndDeptByStep - ==> Parameters: 7369(Integer)
DEBUG com.atChina.dao.EmployeePlusMapper.getEmpAndDeptByStep - <== Columns: EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO
DEBUG com.atChina.dao.EmployeePlusMapper.getEmpAndDeptByStep - <== Row: 7369, SMITH, CLERK, 7902, 1980-12-17 00:00:00, 800, null, 20
DEBUG com.atChina.dao.DepartmentMapper.getDepartByDeptno - ooo Using Connection [oracle.jdbc.driver.T4CConnection@786c1a82]
DEBUG com.atChina.dao.DepartmentMapper.getDepartByDeptno - ==> Preparing: select * from depttest a where a.deptno = ?
DEBUG com.atChina.dao.DepartmentMapper.getDepartByDeptno - ==> Parameters: 20(BigDecimal)
DEBUG com.atChina.dao.DepartmentMapper.getDepartByDeptno - <== Columns: DEPTNO, DNAME, LOC, ID
DEBUG com.atChina.dao.DepartmentMapper.getDepartByDeptno - <== Row: 20, RESEARCH, DALLAS, null
延迟加载:
在全局配置文件中,配置延迟加载功能
<settings>
<!--开启延迟加载-->
<setting name="lazyLoadingEnabled" value="true"/>
<!--关闭积极加载-->
<setting name="aggressiveLazyLoading" value="false"/>
</settings>