<mappernamespace="www.xq.mybatis.dao.EmployeeMapper"><!--public Map<Integer, Employee> getEmpByLastNameLikeReturnMap(String lastName); --><selectid="getEmpByLastNameLikeReturnMap"resultType="www.xq.mybatis.bean.Employee">
select * from tbl_employee where last_name like #{lastName}
</select><!--public Map<String, Object> getEmpByIdReturnMap(Integer id); --><selectid="getEmpByIdReturnMap"resultType="map">
select * from tbl_employee where id=#{id}
</select><!-- public List<Employee> getEmpsByLastNameLike(String lastName); --><!--resultType:如果返回的是一个集合,要写集合中元素的类型 --><selectid="getEmpsByLastNameLike"resultType="www.xq.mybatis.bean.Employee">
select * from tbl_employee where last_name like #{lastName}
</select></mapper>
package www.xq.mybatis.dao;import www.xq.mybatis.bean.Department;publicinterfaceDepartmentMapper{public Department getDeptById(Integer id);}
3.3.2. 创建映射文件 DepartmentMapper.xml
<mappernamespace="www.xq.mybatis.dao.DepartmentMapper"><!--public Department getDeptById(Integer id); --><selectid="getDeptById"resultType="www.xq.mybatis.bean.Department">
select id,dept_name departmentName from tbl_dept where id=#{id}
</select></mapper>
3.3.3. EmployeeMapperPlus 接口中加入
public Employee getEmpByIdStep(Integer id);
3.3.4. EmployeeMapperPlus.xml 中加入
<!-- 使用association进行分步查询:
1、先按照员工id查询员工信息
2、根据查询员工信息中的d_id值去部门表查出部门信息
3、部门设置到员工中;
--><!-- id last_name email gender d_id --><resultMaptype="www.xq.mybatis.bean.Employee"id="MyEmpByStep"><idcolumn="id"property="id"/><resultcolumn="last_name"property="lastName"/><resultcolumn="email"property="email"/><resultcolumn="gender"property="gender"/><!-- association定义关联对象的封装规则
select:表明当前属性是调用select指定的方法查出的结果
column:指定将哪一列的值传给这个方法
流程:使用select指定的方法(传入column指定的这列参数的值)查出对象,并封装给property指定的属性
--><associationproperty="dept"select="www.xq.mybatis.dao.DepartmentMapper.getDeptById"column="d_id"></association></resultMap><!-- public Employee getEmpByIdStep(Integer id);--><selectid="getEmpByIdStep"resultMap="MyEmpByStep">
select * from tbl_employee where id=#{id}
</select>
public Department getDeptByIdPlus(Integer id);public Department getDeptByIdStep(Integer id);
3.5.2. 在 DepartmentMapper.xml 中加入
<!--嵌套结果集的方式,使用collection标签定义关联的集合类型的属性封装规则 --><resultMaptype="www.xq.mybatis.bean.Department"id="MyDept"><idcolumn="did"property="id"/><resultcolumn="dept_name"property="departmentName"/><!--
collection定义关联集合类型的属性的封装规则
ofType:指定集合里面元素的类型
--><collectionproperty="emps"ofType="www.xq.mybatis.bean.Employee"><!-- 定义这个集合中元素的封装规则 --><idcolumn="eid"property="id"/><resultcolumn="last_name"property="lastName"/><resultcolumn="email"property="email"/><resultcolumn="gender"property="gender"/></collection></resultMap><!-- public Department getDeptByIdPlus(Integer id); --><selectid="getDeptByIdPlus"resultMap="MyDept">
SELECT d.id did,d.dept_name dept_name,
e.id eid,e.last_name last_name,e.email email,e.gender gender
FROM tbl_dept d
LEFT JOIN tbl_employee e
ON d.id=e.d_id
WHERE d.id=#{id}
</select><!-- collection:分段查询 --><resultMaptype="www.xq.mybatis.bean.Department"id="MyDeptStep"><idcolumn="id"property="id"/><idcolumn="dept_name"property="departmentName"/><collectionproperty="emps"select="www.xq.mybatis.dao.EmployeeMapperPlus.getEmpsByDeptId"column="{deptId=id}"fetchType="lazy"></collection></resultMap><!-- public Department getDeptByIdStep(Integer id); --><selectid="getDeptByIdStep"resultMap="MyDeptStep">
select id,dept_name from tbl_dept where id=#{id}
</select>