2025/2/22 下午《尚硅谷》—— MyBatis 中通过分步查询处理一对多的映射关系——模板套用步骤

详细原理以及讲解参考: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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值