[Mybatis] collection solves one-to-many association query

In the previous article, we used the cascade attribute method to find out the data and encapsulate it into Employee, and there is another way to realize the data encapsulation.
Next, the editor will introduce the relevant knowledge of the collection tag, including nested query and segmented query.

Now there are two tables of employees and departments. The first two articles introduced the investigation of departments based on employees. This article introduces the investigation of all employees by the department.

First, please refer to steps 1, 2, 3, 4 in the previous article, and start from step 5 here


1. Nested query

5. Added DepartmentMapper.java interface method

public interface DepartmentMapper {	
	public Department getDeptByIdPlus(Integer id);
}

6. Add the DepartmentMapper.xml file, the configuration information is as follows

<?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.atguigu.mybatis.dao.DepartmentMapper">

	<!-- The way the collection nests result sets, defines the encapsulation rules for the associated collection type elements-->
	
	<!-- The way of nesting result sets, use the collection tag to define the property encapsulation rules of the associated collection type -->
	<resultMap type="com.atguigu.mybatis.bean.Department" id="MyDept">
		<id column="did" property="id"/>
		<result column="dept_name" property="departmentName"/>
		<!--
		collection defines the encapsulation rules for properties of associated bound types
		ofType: specifies the type of elements in the collection
		-->
		<collection property="emps" ofType="com.atguigu.mybatis.bean.Employee">
			<!-- Define the encapsulation rules for elements in this collection-->
			<id column="eid" property="id"/>
			<result column="last_name" property="lastName"/>
			<result column="email" property="email"/>
			<result column="gender" property="gender"/>
		</collection>
	
	</resultMap>
	
	<!-- public Department getDeptByIdPlus(Integer id); -->
	<select id="getDeptByIdPlus" resultMap="MyDept" >
		SELECT
			d.id did,
			d.dept_name dept_name,
			e.id,
			e.last_name last_name,
			e.email email,
			e.gender gender
		FROM
			tbl_dept d
		LEFT JOIN tbl_employee e ON d.id = e.d_id
		WHERE
			d.id = #{id}
	</select>
	
</mapper>

7. New test method

	@Test
	public void test06() throws IOException{
		
		SqlSessionFactory sqlSesssionFactory = getSqlSesssionFactory();
		SqlSession openSession = sqlSesssionFactory.openSession();
		try {
			DepartmentMapper mapper = openSession.getMapper(DepartmentMapper.class);
			Department deptByIdPlus = mapper.getDeptByIdPlus(2);
			System.out.println(deptByIdPlus);
			System.out.println(deptByIdPlus.getEmps());
			
		} finally {
			openSession.close();
		}
	}

8. Console Information




Second, segment query

5. Add EmployeeMapperPlus.xml file, the configuration information is as follows

<?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.atguigu.mybatis.dao.EmployeeMapperPlus">
	<select id="getEmpsByDeptId" resultType="com.atguigu.mybatis.bean.Employee">
	 	select * from tbl_employee where d_id=#{deptId}
	 </select>
<mapper/>

6.DepartmentMapper.xml文件,配置信息

<?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.atguigu.mybatis.dao.EmployeeMapperPlus">
	
<resultMap type="com.atguigu.mybatis.bean.Department" id="MyDeptStep">
		<id column="id" property="id"/>
		<id column="dept_name" property="departmentName"/>
		<collection property="emps"
			select="com.atguigu.mybatis.dao.EmployeeMapperPlus.getEmpsByDeptId"
			column="{deptId=id}" fetchType="lazy"></collection>
</resultMap>
	<!-- public Department getDeptByIdStep(Integer id); -->
	<select id="getDeptByIdStep" resultMap="MyDeptStep">
		select id,dept_name departmentName from tbl_dept where id=#{id}
	</select>

<mapper/>


7.新增测试方法

@Test
	public void test06() throws IOException{
		
		SqlSessionFactory sqlSesssionFactory = getSqlSesssionFactory();
		SqlSession openSession = sqlSesssionFactory.openSession();
		try {
			DepartmentMapper mapper = openSession.getMapper(DepartmentMapper.class);
			Department deptByIdStep = mapper.getDeptByIdStep(1);
			System.out.println(deptByIdStep);		
		} finally {
			openSession.close();
		}
	}

8.控制台信息:分步查询效果


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325727780&siteId=291194637