MyBatis映射文件6

时间:2022-05-22 20:28:18

之前说了由Employee找Department,这一节讲一讲由Department找Employee,显然前者是多对一的关系,而后者是一对多的关系。

Department的JavaBean:

private Integer id;
private String departmentName;
private List<Employee> employeeList;

接口中的方法:

Department getDepByIdPlus(Integer id);

查询的SQL语句:

<select id="getDepByIdPlus" resultMap="Dep">
SELECT d.`id` did,d.`department_name` dep_name,e.`id` eid,e.`last_name` last_name,e.`email` email,e.`gender` gender
FROM tb_department d LEFT JOIN tb_employee e
ON d.`id`=e.`d_id`
WHERE d.`id`=2;
</select>

接下来编写resultMap,

collection:定义关联集合类型的属性的封装规则

ofType:指定集合里面的元素类型

<resultMap id="Dep" type="com.figsprite.bean.Department">
<id property="id" column="did"/>
<result property="departmentName" column="dep_name"/>
<collection property="employeeList" ofType="com.figsprite.bean.Employee">
<id property="id" column="eid"/>
<result property="lastName" column="last_name"/>
<result property="gender" column="gender"/>
<result property="email" column="email"/>
</collection>
</resultMap>

其实就是一个resultMap套着另外一个resultMap格式的collection

@Test
public void test2() throws IOException {
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
SqlSession sqlOpenSession = sqlSessionFactory.openSession();
);
for (Employee e : department.getEmployeeList()) {
System.out.println(e);
}
} finally {
sqlOpenSession.close();
}
}

collection标签的分步查询

与之前的association基本一致

  1. <resultMap id="DepStep" type="com.figsprite.bean.Department">
  2. <id property="id" column="id"/>
  3. <result property="departmentName" column="department_name"/>
  4. <collection property="employeeList"
  5. select="com.figsprite.dao.EmployeeMapperPlus.getDepByEmp"
  6. column="id">
  7. </collection>
  8. </resultMap>

11. <select id="getDepByIdStep" resultMap="DepStep">

12.     select id,department_name from tb_department where id=#{id}

13. </select>

  1. @Test
  2. public void test1() throws IOException {
  3. SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
  4. SqlSession sqlOpenSession = sqlSessionFactory.openSession();
  5. try {
  6. DepartmentMapper departmentMapper = sqlOpenSession.getMapper(DepartmentMapper.class);
  7. Department department = departmentMapper.getDepByIdStep(1);
  8. System.out.println(department.getEmployeeList().get(0));
  9. } finally {
  10. sqlOpenSession.close();
  11. }

13. }

DEBUG [main] - ==>  Preparing: select id,department_name from tb_department where id=?

DEBUG [main] - ==> Parameters: 1(Integer)

DEBUG [main] - <==      Total: 1

DEBUG [main] - ==>  Preparing: select * from tb_employee where d_id=?

DEBUG [main] - ==> Parameters: 1(Integer)

DEBUG [main] - <==      Total: 2

日志打印出的是两条SQL语句

多值传递的分步查询

上面的例子中,无论是association还是collection在第一步SQL语句中传的都是单一值给第二条SQL语句当条件,接下来介绍第一步SQL语句传多值给SQL语句。

只要将这些多列值封装成map传递即可,

column={key1=column1,key2=column2}

  1. <resultMap id="DepStep" type="com.figsprite.bean.Department">
  2. <id property="id" column="id"/>
  3. <result property="departmentName" column="department_name"/>
  4. <collection property="employeeList"
  5. select="com.figsprite.dao.EmployeeMapperPlus.getDepByEmp"
  6. column="{did=id}">
  7. </collection>
  8. </resultMap>

注意这里column的写法。

在分步查询的时候还有一个属性fetchType,在默认情况下它的值是lazy,表示使用延迟,eager立即查询,这样即使全局设置了分步查询也不会有影响。

鉴别器discriminator

Mybatis可以使用鉴别器判断某列值,然后根据这个值做不同的封装行为。