提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
mybatis学习记录
08. MyBatis 一对多映射关系
提示:以下是本篇文章正文内容,下面案例可供参考
处理一对多映射关系
1.collection
测试方法:
@Test
public void testGetDeptAndEmp(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);
Dept dept = mapper.getDeptAndEmp(2);
System.out.println(dept);
}
mapper接口:
public interface DeptMapper {
Dept getDeptAndEmp(@Param("did") Integer did);
}
映射文件:
collection:处理一对多的映射关系
ofType:设置collection标签所处理的集合属性中存储数据的类型
<?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.study.mapper.DeptMapper">
<resultMap id="deptAndEmpResultMap" type="Dept">
<id property="did" column="did"></id>
<result property="deptName" column="dept_name"></result>
<collection property="emps" ofType="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>
<!--Dept getDeptAndEmp(@Param("did") Integer did);-->
<select id="getDeptAndEmp" resultMap="deptAndEmpResultMap">
select * from t_dept left join t_emp on t_dept.did = t_emp.did where t_dept.did = #{did}
</select>
</mapper>
2.分步查询
测试方法:
@Test
public void testGetDeptAndEmpByStep() {
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);
Dept dept = mapper.getDeptAndEmpByStepOne(3);
// System.out.println(dept);
// System.out.println("***************************");
System.out.println(dept.getDeptName());
System.out.println("***************************");
System.out.println(dept.getEmps());
}
第一步查询出部门信息,在DeptMapper接口中
DepMapper接口:
public interface DeptMapper {
Dept getDeptAndEmpByStepOne(@Param("did") int 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">
<mapper namespace="com.study.mapper.DeptMapper">
<resultMap id="deptAndEmpByStepResultMap" type="Dept">
<id property="did" column="did"></id>
<result property="deptName" column="dept_name"></result>
<collection property="emps"
select="com.study.mapper.EmpMapper.getDeptAndEmpByStepTwo"
column="did">
</collection>
</resultMap>
<!--Dept getDeptAndEmpByStepOne(@Param("did") int did);多对一分步查询第一步-->
<select id="getDeptAndEmpByStepOne" resultMap="deptAndEmpByStepResultMap">
select * from t_dept where did = #{did}
</select>
</mapper>
第二步通过did查询出部门所对应的员工信息,在EmpMapper接口中
EmpMapper接口:
public interface EmpMapper {
List<Emp> getDeptAndEmpByStepTwo(@Param("did") int 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">
<mapper namespace="com.study.mapper.EmpMapper">
<!--List<Emp> getDeptAndEmpByStepTwo(@Param("did") int did);-->
<select id="getDeptAndEmpByStepTwo" resultType="Emp">
select * from t_emp where did = #{did}
</select>
</mapper>