十二、MyBatis实现一对多映射处理

十二、MyBatis实现一对多映射处理

准备

数据库表
员工表(t_employee)
在这里插入图片描述
部门表(t_department)
在这里插入图片描述
表所对应的类
员工 t_employee
在这里插入图片描述

部门 t_department
在这里插入图片描述

方式一(collection)

以通过部门id,查询部门信息,以及部门中所有员工信息为例。
接口

public interface DepartmentMapper {

    //通过部门的did查询部门所有信息
    Department selectDepartment(@Param("did")Integer did);

}

映射文件

<?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"><!--约束,约束不同xml中所写的标签也不同-->
<mapper namespace="com.xxx.mapper.DepartmentMapper"><!--接口-->



    <resultMap id="deptMap" type="com.xxx.pojo.Department">
        <id property="did" column="did"></id>
        <result property="dname" column="dname"></result>
        <collection property="emp" ofType="com.xxx.pojo.Employee">
            <id property="eid" column="eid"></id>
            <result property="ename" column="ename"></result>
            <result property="age" column="age"></result>
            <result property="sex" column="sex"></result>
        </collection>

    </resultMap>

<!--    //通过部门的did查询部门所有信息
    Department selectDepartment(@Param("did")Integer did);-->
    <select id="selectDepartment" resultMap="deptMap">
        select t_department.*,t_employee.* from t_department left join t_employee on t_department.did=t_employee.did where t_department.did=#{did}
    </select>



</mapper>

测试

		DepartmentMapper mapper = sqlSession.getMapper(DepartmentMapper.class);

        //查询部门did=2的所有信息
        Department department = mapper.selectDepartment(2);

        System.out.println(department);
Department{did=2, dname='部门2', emp=[Employee{eid=4, ename='翠花', age=19, sex='女', dept=null}, Employee{eid=3, ename='小红', age=20, sex='女', dept=null}]}

方式二(分步查询)

以通过部门id,查询部门信息,以及部门中所有员工信息为例。
第一步接口

public interface DepartmentMapper {

    //通过部门的did查询部门所有信息
    Department selectDepartment(@Param("did")Integer did);

}

第二步接口

public interface EmployeeMapper {

    //通过员工部门的did查询员工的所有信息
    Employee selectEmployee(@Param("did")Integer did);

}

第一步映射文件

<?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"><!--约束,约束不同xml中所写的标签也不同-->
<mapper namespace="com.xxx.mapper.DepartmentMapper"><!--接口-->


    <resultMap id="deptMap" type="com.xxx.pojo.Department">
        <id property="did" column="did"></id>
        <result property="dname" column="dname"></result>
        <!--此处的select中写的是第二步接口中的方法的全路径,colum中写的是第一步查出的那个数据作为第二步的参数-->
        <collection property="emp"
                    select="com.xxx.mapper.EmployeeMapper.selectEmployee"
                    column="did"></collection>
    </resultMap>

<!--    //通过部门的did查询部门所有信息
    Department selectDepartment(@Param("did")Integer did);-->
    <select id="selectDepartment" resultMap="deptMap">
        select * from t_department where did=#{did}
    </select>



</mapper>

第二步映射文件

<?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"><!--约束,约束不同xml中所写的标签也不同-->
<mapper namespace="com.xxx.mapper.EmployeeMapper"><!--接口-->

    <select id="selectEmployee" resultType="com.xxx.pojo.Employee">
        select * from t_employee where did=#{did}
    </select>
    
</mapper>

测试

		DepartmentMapper mapper = sqlSession.getMapper(DepartmentMapper.class);

        //查询部门did=2的所有信息
        Department department = mapper.selectDepartment(2);

        System.out.println(department);
Department{did=2, dname='部门2', emp=[Employee{eid=4, ename='翠花', age=19, sex='女', dept=null}, Employee{eid=3, ename='小红', age=20, sex='女', dept=null}]}
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

齊 天 大 聖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值