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>
- 方式二:使用association处理映射
<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方法等
}
- 方式一:使用collection
- collection:用来处理一对多的映射关系
- ofType:表示该属性对饮的集合中存储的数据的类型
<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>
延迟加载
- 分步查询的优点:可以实现延迟加载,但是必须在核心配置文件中设置全局配置信息:
lazyLoadingEnabled
:延迟加载的全局开关。当开启时,所有关联对象都会延迟加载aggressiveLazyLoading
:当开启时,任何方法的调用都会加载该对象的所有属性。 否则,每个属性会按需加载- 此时就可以实现按需加载,获取的数据是什么,就只会执行相应的sql。此时可通过association和collection中的fetchType属性设置当前的分步查询是否使用延迟加载,
fetchType="lazy
(延迟加载)|eager
(立即加载)"
<settings>
<!--开启延迟加载-->
<setting name="lazyLoadingEnabled" value="true"/>
</settings>
标签:dept,emp,mybatis,中多,where,id,select,加载 来源: https://www.cnblogs.com/Snowclod/p/16029783.html