mybatis文件映射之利用collection定义关联集合

Posted 西西嘛呦

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis文件映射之利用collection定义关联集合相关的知识,希望对你有一定的参考价值。

Employee.java

public class Employee {
    private Integer id;
    private String lastName;
    private String gender;
    private String email;
    Department dept;
}

Department.java

public class Department {
    private Integer id;
    private String deptName;
    private List<Employee> employee;
}

DepartmentMapper.java

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

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.gong.mybatis.dao.DepartmentMapper">
    
    <resultMap type="com.gong.mybatis.bean.Department" id="MyDept">
        <id column="did" property="id"/>
        <result column="dept_name" property="deptName"/>
        <collection property="employee" ofType="com.gong.mybatis.bean.Employee">
            <id column="eid" property="id"/>
            <result column="last_name" property="lastName"/>
            <result column="gender" property="gender"/>
            <result column="email" property="email"/>
        </collection>
    </resultMap>
    <select id="getDeptByIdWithAllEmployees" resultMap="MyDept">
        SELECT d.id did,d.dept_name,e.id eid,e.last_name,e.gender,e.email 
        from tbl_department d 
        LEFT JOIN tbl_employee e 
        on d.id = e.d_id 
        WHERE d.id = #{id}
    </select>
</mapper>

说明:这实际上是多对一查询,即一个部门里面有多个员工。collection标签中property属性标识Department类中的成员属性,ofType标识其类型。

对该功能进行测试:

package com.gong.mybatis.test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import com.gong.mybatis.bean.Department;
import com.gong.mybatis.bean.Employee;
import com.gong.mybatis.dao.DepartmentMapper;
import com.gong.mybatis.dao.EmployeeMapperPlus;

public class TestMybatis2 {
    
    public SqlSessionFactory getSqlSessionFactory() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream is = Resources.getResourceAsStream(resource);
        return new SqlSessionFactoryBuilder().build(is);
    }
    
    @Test
    public void test2() throws IOException {
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
        SqlSession openSession = sqlSessionFactory.openSession();
        try {
            DepartmentMapper mapper = openSession.getMapper(DepartmentMapper.class);
            Department department = mapper.getDepthByIdWithAllEmployees(1);
            System.out.println(department);
            System.out.println(department.getEmployee());
            openSession.commit();
        } finally {
            openSession.close();
        }
        
    }

}

输出:

DEBUG 01-20 19:13:24,335 ==> Preparing: SELECT d.id did,d.dept_name,e.id eid,e.last_name,e.gender,e.email from tbl_department d LEFT JOIN tbl_employee e on d.id = e.d_id WHERE d.id = ? (BaseJdbcLogger.java:145)
DEBUG 01-20 19:13:24,389 ==> Parameters: 1(Integer) (BaseJdbcLogger.java:145)
DEBUG 01-20 19:13:24,426 <== Total: 2 (BaseJdbcLogger.java:145)
Department [id=1, deptName=开发部]
[Employee [id=1, lastName=xiximayou, gender=1, email=xiximayou@qq.com, dept=null], Employee [id=3, lastName=小红, gender=0, email=xiaohong@qq.com, dept=null]]

可以看到对于一个部门,取得了里面的雇员,表明该功能是成功的。

以上是关于mybatis文件映射之利用collection定义关联集合的主要内容,如果未能解决你的问题,请参考以下文章

mybatis文件映射之利分布查询时传递多列值

spring-mybatis配置文件中指定实体类映射文件

spring-mybatis配置文件中指定实体类映射文件

mybatis动态sql之利用sql标签抽取可重用的sql片段

MyBatis(映射文件中使用foreach标签时报错,属性collection的问题)

mybatis映射文件select_resultMap_关联查询_collection定义关联集合