1.mapper.xml
<?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">
<mapper namespace="lz.cn.mapper.EmployeeMapper">
<resultMap id="baseResultMap" type="Employee">
<id property="empno" column="EMPNO" />
<result property="ename" column="ENAME"/>
<result property="job" column="JOB"/>
<result property="mgr" column="MGR"/>
<result property="hiredate" column="HIREDATE"/>
<result property="sal" column="SAL"/>
<result property="comm" column="COMM"/>
<result property="deptno" column="DEPTNO"/>
</resultMap>
<resultMap id="extendResultMap" type="Employee" extends="baseResultMap">
<association property="department" javaType="Department" columnPrefix="D_">
<id property="deptno" column="deptno"/>
<result property="dname" column="dname"></result>
<result property="location" column="location"></result>
</association>
</resultMap>
<select id="selectById" resultMap="extendResultMap" parameterType="int">
select e.EMPNO,
e.ENAME,
e.JOB,
e.MGR,
e.HIREDATE,
e.SAL,
e.COMM,
e.DEPTNO,
d.DEPTNO D_DEPTNO,
d.DNAME D_DNAME,
d.LOC D_LOC
from EMPLOYEE E,DEPARTMENT D
where E.DEPTNO = D.DEPTNO
and E.EMPNO=#{id}
</select>
<resultMap id="extentResultMap2" type="Employee" extends="baseResultMap">
<association property="department" column="deptno" select="selectDepartmentById"/>
</resultMap>
<select id="selectDepartmentById" resultType="Department" parameterType="int">
select * from DEPARTMENT where DEPTNO=#{deptno}
</select>
<select id="selectTwoById" parameterType="int" resultMap="extentResultMap2">
select * from EMPLOYEE where empno=#{empno}
</select>
</mapper>
- 第一种selectById运用sql语句管理查询简单粗暴
- 俩个sql语句,用association标签关联起来;
- 需要指定级联实体Bean在上级Bean中的属性名称,即association标签的property属性
- 要指定下一步查询需要使用的select语句,即association标签的select属性,该属性值为Mapper接口查询方法的全限定名;
- 需要使用column属性,用于指定第二步查询的输入参数,第二步查询只有一个输入参数时,使用第一步查询结果的column名称即可;
2.mapper接口
package lz.cn.mapper;
import lz.cn.vo.Employee;
public interface EmployeeMapper {
public Employee selectById(int empno);
public Employee selectTwoById(int empno);
}