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 protected], dept=null], Employee [id=3, lastName=小红, gender=0, [email protected], dept=null]]

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

原文地址:https://www.cnblogs.com/xiximayou/p/12219162.html

时间: 2024-11-05 21:34:21

mybatis文件映射之利用collection定义关联集合(五)的相关文章

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<Emp

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

知识点:查询一个实体类,并查出这个类下面的集合 Employee.java实体类 package com.hand.mybatis.bean;public class Employee {    private Integer eId;    private String eName;    private Integer gender;    private String email;    private Department dept;        public Employee() {

mybatis文件映射之select操作返回Map

1.返回的Map键为列所对应的名称,值就是具体的值 EmployeeMapper.java public Map<String,Object> getEmpByIdReturnMap(Integer id); EmployeeMapper.xml <select id="getEmpByIdReturnMap" resultType="map"> select id,last_name lastName,gender,email from t

【MyBatis】映射文件示例

本文以实例说明Mapper文件的标签的使用. 配置文件在[MyBatis]配置文件示例 DAO文件 DepartmentMapper.java package com.atguigu.mybatis.dao; import com.atguigu.mybatis.bean.Department; public interface DepartmentMapper { public Department getDeptById(Integer id); public Department getD

13、mybatis一对多resultMap中用collection指定集合的封装规则

一的一方College.java: 多的一方Student.java College的sqlmapper文件配置 <resultMap type="com.pxxy.bean.College" id="collegeMap"> <id column="id" property="id"/> <result column="collegeName" property="

Mybatis 高级结果映射 ResultMap Association Collection

作者:ilovejava_2010 MyBatis的创建基于这样一个思想:数据库并不是您想怎样就怎样的.虽然我们希望所有的数据库遵守第三范式或BCNF(修正的第三范式),但它们不是.如果有一个数据库能够完美映射到所有应用程序,也将是非常棒的,但也没有.结果集映射就是MyBatis为解决这些问题而提供的解决方案.例如,我们如何映射下面这条语句? <!-- Very Complex Statement --> <select id="selectBlogDetails"

mybatis中映射文件和实体类的关联性

mybatis的映射文件写法多种多样,不同的写法和用法,在实际开发过程中所消耗的开发时间.维护时间有很大差别,今天我就把我认为比较简单的一种映射文件写法记录下来,供大家修改建议,争取找到一个最优写法~~: 以User对象和UserMap.xml为例讲解,代码如下: User为用户实体类(仅作为讲解,可以只关注引用类型变量,get/set方法省略): import com.google.common.collect.Lists; import com.gukeer.common.persisten

利用MyEclipse自动创建PO类、hbm文件(映射文件)、DAO

前提条件:表sjzdfl  表sjzdxx (使用数据库MySQL) 表sjzdfl (两个字段sjzdflId 和 sjzdflmc) 表sjzdfl 建表语句: [sql] view plaincopy DROP TABLE IF EXISTS `sjzdfl`; CREATE TABLE `sjzdfl` ( `sjzdflId` int(11) NOT NULL auto_increment, `sjzdflmc` varchar(255) default NULL, PRIMARY K

mybatis 关系映射

一:订单商品数据模型 1.数据库执行脚本 创建数据库表代码: 1 CREATE TABLE items ( 2 id INT NOT NULL AUTO_INCREMENT, 3 itemsname VARCHAR(32) NOT NULL COMMENT '商品名称', 4 price FLOAT(10,1) NOT NULL COMMENT '商品定价', 5 detail TEXT COMMENT '商品描述', 6 pic VARCHAR(64) DEFAULT NULL COMMENT