其他分享
首页 > 其他分享> > mybatis中多对一和一对多映射、延迟加载

mybatis中多对一和一对多映射、延迟加载

作者:互联网

处理多对一映射关系

    <resultMap id="empAneDeptMap" type="employee">
        <id property="empId" column="emp_id"></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>
        <!--  方式一:使用级联方式 -->
        <result property="dept.deptId" column="dept_id"></result>
        <result property="dept.deptName" column="dept_name"></result>
    </resultMap>

    <select id="getEmpAndDept" resultMap="empAneDeptMap">
        select * from t_emp left join t_dept on t_emp.dept_id = t_dept.dept_id where t_emp.emp_id = #{id}
    </select>
    <resultMap id="empAneDeptMap" type="employee">
        <id property="empId" column="emp_id"></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>
        <!--  方式二:使用association -->
        <association property="dept" javaType="department">
            <id property="deptId" column="dept_id"></id>
            <result property="deptName" column="dept_name"></result>
        </association>
    </resultMap>

    <select id="getEmpAndDept" resultMap="empAneDeptMap">
        select * from t_emp left join t_dept on t_emp.dept_id = t_dept.dept_id where t_emp.emp_id = #{id}
    </select>

employeeMapper.xml

    <!-- 方式三:分步查询-->
    <resultMap id="byStepMap" type="employee">
        <id property="empId" column="emp_id"></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>
        <association property="dept"
                     select="com.snow.mapper.DepartmentMapper.getDeptById"
                     column="dept_id"></association>
    </resultMap>
    <select id="getEmpAndDeptByStep" resultMap="byStepMap">
        select * from t_emp where emp_id = #{empId}
    </select>

departmentMapper.xml

<mapper namespace="com.snow.mapper.DepartmentMapper">
    <!-- Department getDeptById(@Param("deptId")Integer id);-->
    <select id="getDeptById" resultType="department">
        select dept_id deptId,dept_name deptName from t_dept where dept_id = #{deptId}
    </select>
</mapper>

处理一对多映射关系

public class Dept {
    private Integer did;
    private String deptName;
    private List<Emp> emps;
    //...构造器、get、set方法等
}
    <resultMap id="deptResultMap" type="department">
        <id property="deptId" column="dept_id"></id>
        <result property="deptName" column="dept_name"></result>
        <collection property="employees" ofType="employee">
            <id property="empId" column="emp_id"></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>

    <!-- Department getDeptAndEmp(@Param("deptId") Integer id); -->
    <select id="getDeptAndEmp" resultMap="deptResultMap">
        select * from t_dept left join t_emp on t_dept.dept_id = t_emp.dept_id where t_dept.dept_id = #{deptId}
    </select>

departmentMapper.xml

    <resultMap id="DeptByStepMap" type="department">
        <id property="deptId" column="dept_id"></id>
        <result property="deptName" column="dept_name"></result>
        <collection property="employees"
                    select="com.snow.mapper.EmployeeMapper.getEmpByDeptId"
                    column="dept_id"></collection>
    </resultMap>

    <select id="getDeptAndEmpByStep" resultMap="DeptByStepMap">
        select * from t_dept where dept_id = #{deptId}
    </select>

EmployeeMapper.xml

    <select id="getEmpByDeptId" resultType="com.snow.pojo.Employee">
        select * from t_emp where dept_id = #{dept_id}
    </select>

延迟加载

<settings>
	<!--开启延迟加载-->
	<setting name="lazyLoadingEnabled" value="true"/>
</settings>

标签:dept,emp,mybatis,中多,where,id,select,加载
来源: https://www.cnblogs.com/Snowclod/p/16029783.html