MyBatis implements one-to-many mapping processing
Prepare
Database table
employee table (t_employee)
department table (t_department)
table corresponding to the class
employee t_employee
Department t_department
Method 1 (collection)
Take the query of department information and the information of all employees in the department as an example.
interface
public interface DepartmentMapper {
//通过部门的did查询部门所有信息
Department selectDepartment(@Param("did")Integer did);
}
map file
<?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>
test
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}]}
Method 2 (step-by-step query)
Take the query of department information and the information of all employees in the department as an example.
The first interface
public interface DepartmentMapper {
//通过部门的did查询部门所有信息
Department selectDepartment(@Param("did")Integer did);
}
The second interface
public interface EmployeeMapper {
//通过员工部门的did查询员工的所有信息
Employee selectEmployee(@Param("did")Integer did);
}
First step mapping file
<?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>
The second step is to map the file
<?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>
test
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}]}