详细原理以及讲解参考:2025/2/22上午《尚硅谷》——mybatis中处理多对一映射关系功能分析——三种处理方式:级联、association、分布查询——resultType和resultMap的区别以及对比-优快云博客
步骤一:Mapper 接口代码
DeptMapper.java
Dept getDeptAndEmpByStepOne(@Param("deptId") Integer deptId);
List<Emp> getDeptAndEmpByStepTwo(@Param("deptId") Integer deptId);
package com.atguigu.mybatis.mapper;
import com.atguigu.mybatis.pojo.Dept;
import com.atguigu.mybatis.pojo.Emp;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface DeptMapper {
/* *
*
*通过分步查询查询部门以及部门中的员工信息的第一步
* @param deptId
* @return com.atguigu.mybatis.pojo.Dept
* @author yzh
* @create 2025/2/22
**/
Dept getDeptAndEmpByStepOne(@Param("deptId") Integer deptId);
/* *
*
*通过分步查询查询部门以及部门中的员工信息的第二步
* @param deptId
* @return List<Emp>
* @author yzh
* @create 2025/2/22
**/
List<Emp> getDeptAndEmpByStepTwo(@Param("deptId") Integer deptId);
}
步骤二:SQL 映射文件代码
DeptMapper.xml
<!--Dept getDeptAndEmpByStepOne(@Param("deptId") Integer deptId);-->
<resultMap id="deptAndEmpResultMapByStep" type="Dept">
<id column="dept_id" property="deptId"></id>
<result column="dept_name" property="deptName"></result>
<collection property="emps"
select="com.atguigu.mybatis.mapper.DeptMapper.getDeptAndEmpByStepTwo"
column="dept_id"></collection>
</resultMap>
<select id="getDeptAndEmpByStepOne" resultMap="deptAndEmpResultMapByStep">
select * from t_dept where dept_id = #{deptId}
</select>
<!--List<Emp> getDeptAndEmpByStepTwo(@Param("deptId") Integer deptId);-->
<select id="getDeptAndEmpByStepTwo" resultType="Emp">
select * from t_emp where dept_id = #{deptId}
</select>
<?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="com.atguigu.mybatis.mapper.DeptMapper">
<!--Dept getDeptAndEmpByStepOne(@Param("deptId") Integer deptId);-->
<resultMap id="deptAndEmpResultMapByStep" type="Dept">
<id column="dept_id" property="deptId"></id>
<result column="dept_name" property="deptName"></result>
<collection property="emps"
select="com.atguigu.mybatis.mapper.DeptMapper.getDeptAndEmpByStepTwo"
column="dept_id"></collection>
</resultMap>
<select id="getDeptAndEmpByStepOne" resultMap="deptAndEmpResultMapByStep">
select * from t_dept where dept_id = #{deptId}
</select>
<!--List<Emp> getDeptAndEmpByStepTwo(@Param("deptId") Integer deptId);-->
<select id="getDeptAndEmpByStepTwo" resultType="Emp">
select * from t_emp where dept_id = #{deptId}
</select>
</mapper>
步骤三:测试代码
ResultMapperTest.java
@Test
public void testGetDeptAndEmpByStep() {
SqlSession sqlSession = SqlSessionUtil.getSqlSession();
DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);
Dept dept = mapper.getDeptAndEmpByStepOne(1);
System.out.println(dept.getDeptName());
System.out.println(dept);
}
import com.atguigu.mybatis.mapper.DeptMapper;
import com.atguigu.mybatis.mapper.EmpMapper;
import com.atguigu.mybatis.pojo.Dept;
import com.atguigu.mybatis.pojo.Emp;
import com.atguigu.mybatis.utils.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
public class ResultMapperTest {
@Test
public void testGetDeptAndEmpByStep() {
SqlSession sqlSession = SqlSessionUtil.getSqlSession();
DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);
Dept dept = mapper.getDeptAndEmpByStepOne(1);
System.out.println(dept.getDeptName());
System.out.println(dept);
}
}
步骤四:测试运行
测试输出——成功查询!
DEBUG 02-22 16:32:13,231 ==> Preparing: select * from t_dept where dept_id = ? (BaseJdbcLogger.java:137)
DEBUG 02-22 16:32:13,261 ==> Parameters: 1(Integer) (BaseJdbcLogger.java:137)
DEBUG 02-22 16:32:13,328 <== Total: 1 (BaseJdbcLogger.java:137)
A
DEBUG 02-22 16:32:13,329 ==> Preparing: select * from t_emp where dept_id = ? (BaseJdbcLogger.java:137)
DEBUG 02-22 16:32:13,329 ==> Parameters: 1(Integer) (BaseJdbcLogger.java:137)
DEBUG 02-22 16:32:13,340 <== Total: 2 (BaseJdbcLogger.java:137)
Dept{deptId=1, deptName='A', emps=[Emp{empId=1, empName='张三', age=20, gender='男', dept=null}, Emp{empId=4, empName='赵六', age=24, gender='男', dept=null}]}
进程已结束,退出代码为 0