首页 > 其他分享 >mybatis 表与表 关联查询 (一)

mybatis 表与表 关联查询 (一)

时间:2023-02-13 10:36:47浏览次数:58  
标签:Dept did 表与表 private 查询 dept mybatis Integer

 

@Mapper
public interface DeptMapper {

    /**
     * 分步查询员工
     * 及员工所对应的部门
     * 分步查询第二步:
     * 通过部门查询员工所对应的部门
     */
    Dept getEmpAndDeptByTwo(@Param("did") Integer did);

    /**
     * 查询部门以及部门下面所有的员工信息
     */
    Dept getDeptAndEmp(@Param("did") Integer did);

    /**
     * 分步查询查询部门及部门中对应所有的员工信息
     * 分步查询第一步:查询部门信息
     */
    Dept getDeptAndEmpByOne(@Param("did") Integer did)

 

 

 

mapper

  <!--Dept getEmpAndDeptByTwo(@Param("did") Integer did);-->
    <select id="getEmpAndDeptByTwo" resultType="com.example.bootdemo.pojo.Dept">
        select * from m_dept where did = #{did}
    </select>

    <resultMap id="deptAndEmpResultMap" type="com.example.bootdemo.pojo.Dept">
        <id property="did" column="did"></id>
        <result property="deptName" column="dept_name"></result>
        <!--
            collection:处理一对多的映射关系
            ofType:表示该属性所对应的集合中存储数据的类型
        -->
        <collection property="emps" ofType="com.example.bootdemo.pojo.Emp">
            <id property="eid" column="eid"></id>
            <result property="empName" column="emp_name"></result>
            <result property="age" column="age"></result>
            <result property="sex" column="sex"></result>
            <result property="email" column="email"></result>
        </collection>
    </resultMap>
    <select id="getDeptAndEmp" resultMap="deptAndEmpResultMap">
        select * from m_dept left join m_emp on m_dept.did = m_emp.did where m_dept.did = #{did}
    </select>

    <resultMap id="deptAndEmpByStepResultMap" type="com.example.bootdemo.pojo.Dept">
        <id property="did" column="did"></id>
        <result property="deptName" column="dept_name"></result>
        <collection property="emps"
                    select="com.example.bootdemo.mapper.EmpMapper.getDeptAndEmpByStepTwo"
                    column="did" fetchType="eager"></collection>
    </resultMap>
    <select id="getDeptAndEmpByOne" resultMap="deptAndEmpByStepResultMap">
        select * from m_dept where did = #{did}
    </select>

 

 

 

pojo


public class Emp {

    private Integer eid;

    private String empName;

    private Integer age;

    private String sex;

    private String email;

    private Dept dept;}

 



pojo

public class Dept {

    private Integer did;

    private String deptName;

    private List<Emp> emps;}

 



标签:Dept,did,表与表,private,查询,dept,mybatis,Integer
From: https://www.cnblogs.com/RedOrange/p/17091680.html

相关文章