resultMap_关联查询_collection 使用规则

1、项目结构

2、Department.java

package com.atguigu.mybatis.bean;

import java.util.List;

public class Department {

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

    public List<Employee> getEmps() {
        return emps;
    }
    public void setEmps(List<Employee> emps) {
        this.emps = emps;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getDepartmentName() {
        return departmentName;
    }
    public void setDepartmentName(String departmentName) {
        this.departmentName = departmentName;
    }
    @Override
    public String toString() {
        return "Department [id=" + id + ", departmentName=" + departmentName + ", emps=" + emps + "]";
    }

}

3、Employee.java

package com.atguigu.mybatis.bean;

public class Employee {

    private Integer id;
    private String lastName;
    private String email;
    private String gender;
    private Department dept;

    public Department getDept() {
        return dept;
    }
    public void setDept(Department dept) {
        this.dept = dept;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
    @Override
    public String toString() {
        return "Employee [id=" + id + ", lastName=" + lastName + ", email=" + email + ", gender=" + gender + "]";
    }
    public Employee(Integer id, String lastName, String email, String gender) {
        super();
        this.id = id;
        this.lastName = lastName;
        this.email = email;
        this.gender = gender;
    }
    public Employee() {
        super();
        // TODO Auto-generated constructor stub
    }

}

4、DepartmentMapper.java

package com.atguigu.mybatis.dao;

import com.atguigu.mybatis.bean.Department;

public interface DepartmentMapper {

    public Department getDeptById(Integer id);

    public Department getDeptByIdPlus(Integer id);

}

5、EmployeeMapperPlus.java

package com.atguigu.mybatis.dao;

import com.atguigu.mybatis.bean.Employee;

public interface EmployeeMapperPlus {

    public Employee getEmpById(Integer id);

    public Employee getEmpAndDept(Integer id) ;

    public Employee getEmpByIdStep(Integer id) ;
}

6、MybatisTest.java

package com.atguigu.mybatis.test;

import java.io.IOException;
import java.io.InputStream;
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.atguigu.mybatis.bean.Department;
import com.atguigu.mybatis.bean.Employee;
import com.atguigu.mybatis.dao.DepartmentMapper;
import com.atguigu.mybatis.dao.EmployeeMapperPlus;

public class MybatisTest {

    private SqlSessionFactory getSqlSessionFactory() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        return new SqlSessionFactoryBuilder().build(inputStream);
    }

    @Test
    public void test3() throws IOException {
        SqlSessionFactory sqlSessionFactory =getSqlSessionFactory();
        //1、获取到的SqlSession不会自动提交
        SqlSession openSession= sqlSessionFactory.openSession();
        try {

            DepartmentMapper mapper=openSession.getMapper(DepartmentMapper.class);
            Department department=mapper.getDeptByIdPlus(1);

            System.out.println(department);
            System.out.println(department.getEmps());
            //3、手动提交
            openSession.commit();

        } finally {
            openSession.close();
        }
    }

}

7、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.DepartmentMapper">

    <!-- public Department getDeptById(Integer id); -->
    <select id="getDeptById" resultType="com.atguigu.mybatis.bean.Department">
        select id,dept_name departmentName from tbl_dept where id=#{id}
    </select>

    <!--
        private Integer id;
        private String departmentName;
        private List<Employee> emps;
            public Department getDeptByIdPlus(Integer id); -->
    <resultMap type="com.atguigu.mybatis.bean.Department" id="Mydept">
        <id column="did" property="id"/>
        <result column="dept_name" property="departmentName"/>
        <!--
            collection定义关联集合类型的属性的封装规则
                指定集合里元素类型
        -->
        <collection property="emps" ofType="com.atguigu.mybatis.bean.Employee">
            <id column="eid" property="id"/>
            <result column="last_name" property="lastName"/>
            <result column="email" property="email"/>
            <result column="gender" property="gender"/>
        </collection>
    </resultMap>
    <select id="getDeptByIdPlus" resultMap="Mydept">
        SELECT d.id did,d.dept_name dept_name,
            e.id eid,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>

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

    ofType:指定集合里元素类型,其他与association一样

    <collection property="emps" ofType="com.atguigu.mybatis.bean.Employee">
            <id column="eid" property="id"/>
            <result column="last_name" property="lastName"/>
            <result column="email" property="email"/>
            <result column="gender" property="gender"/>
        </collection>

8、EmployeeMapperPlus.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.Employee" id="MySimpleEmp">
        <id column="id" property="id"/>
        <result column="last_name" property="lastName"/>
    </resultMap>
    <!-- public Employee getEmpById(Integer id); -->
    <select id="getEmpById" resultMap="MySimpleEmp">
        select * from tbl_employee where id=#{id}
    </select>

    <!-- 场景一:
            查询employee的同时查询员工对应的部门
            SELECT e.id id,e.last_name lastName,e.gender gender,e.d_id d_id,
                d.id did,d.dept_name dept_name FROM tbl_employee e,tbl_dept d
                    WHERE e.d_id=d.id AND e.id=2
    -->
    <!--
        联合查询:级联属性封装结构集
     -->
    <resultMap type="com.atguigu.mybatis.bean.Employee" id="MyDifEmp">
        <id column="id" property="id"/>
        <result column="email" property="email"/>
        <result column="last_name" property="lastName"/>
        <result column="gender" property="gender"/>
        <result column="did" property="dept.id"/>
        <result column="dept_name" property="dept.departmentName"/>
    </resultMap>
    <resultMap type="com.atguigu.mybatis.bean.Employee" id="MyDifEmp2">
        <id column="id" property="id"/>
        <result column="email" property="email"/>
        <result column="last_name" property="lastName"/>
        <result column="gender" property="gender"/>
        <!--
            association可以指定联合的JavaBean对象
            property=“dept”:指定哪个属性是联合的对象
            JavaType:指定这个属性的类型
         -->
        <association property="dept" javaType="com.atguigu.mybatis.bean.Department">
            <id column="did" property="id"/>
            <result column="dept_name" property="departmentName"/>
        </association>
    </resultMap>
    <!-- public Employee getEmpAndDept(Integer id) ; -->
    <select id="getEmpAndDept" resultMap="MyDifEmp2">
        SELECT e.id id,e.email email,e.last_name last_name,e.gender gender,e.d_id d_id,
                d.id did,d.dept_name dept_name FROM tbl_employee e,tbl_dept d
                    WHERE e.d_id=d.id AND e.id=#{id}
    </select>

    <!-- 使用association分步查询:
            1、先按照员工id查询员工信息
            2、根据查询员工信息的d_id值去部门部门表查出部门信息
            3、部门设置到员工中
     -->
    <resultMap type="com.atguigu.mybatis.bean.Employee" id="MyEmpStep">
        <id column="id" property="id"/>
        <result column="last_name" property="lastName"/>
        <result column="email" property="email"/>
        <result column="gender" property="gender"/>
        <!-- association定义关联对象的封装规则
            select:表明当前属性是调用select指定的方法查出的结果
            column:指定将那一列的值的传给这个方法
            流程:使用select指定的方法(传入column指定的这列参数值)查出对象,并封装给property指定的属性
            -->
        <association property="dept"
                select="com.atguigu.mybatis.dao.DepartmentMapper.getDeptById"
                        column="d_id">
        </association>
    </resultMap>
    <!-- public Employee getEmpByIdStep(Integer id) ; -->
    <select id="getEmpByIdStep" resultMap="MyEmpStep">
        select * from tbl_employee where id=#{id}
    </select>

    <!-- =================================association=================================================== -->
    <!--
        场景二:
            DepartmentMapper.xml中
     -->

</mapper>

9、mybatis-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

<!--
    <settings>
        <setting name="lazyLoadingEnabled" value="true"/>
        <setting name="aggressiveLazyLoading" value="false"/>
    </settings>
     -->
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC" />
            <!-- 配置数据库连接信息 -->
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver" />
                <property name="url" value="jdbc:mysql://localhost:3306/mybatis" />
                <property name="username" value="root" />
                <property name="password" value="111111" />
            </dataSource>
        </environment>
    </environments>

       <mappers>

        <mapper resource="com/atguigu/mybatis/dao/EmployeeMapperPlus.xml"/>
        <mapper resource="com/atguigu/mybatis/dao/DepartmentMapper.xml"/>
       </mappers> 

</configuration>

      

原文地址:https://www.cnblogs.com/2016024291-/p/8252835.html

时间: 2024-10-09 23:44:33

resultMap_关联查询_collection 使用规则的相关文章

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() {

关联查询resultMap使用规则总结——(十一)

resultType: 作用: 将查询结果按照sql列名pojo属性名一致性映射到pojo中. 场合: 常见一些明细记录的展示,比如用户购买商品明细,将关联查询信息全部展示在页面时,此时可直接使用resultType将每一条记录映射到pojo中,在前端页面遍历list(list中是pojo)即可. resultMap: 使用association和collection完成一对一和一对多高级映射(对结果有特殊的映射要求). association: 作用: 将关联查询信息映射到一个pojo对象中.

JAVA-Unit03: SQL(基础查询) 、 SQL(关联查询)

Unit03: SQL(基础查询) . SQL(关联查询) 列别名 当SELECT子句中查询的列是一个函数 或者表达式时,那么查询出来的结果集 中对应的该字段的名字就是这个函数或者 表达式的名字.为此可以为这一列添加 别名,这样结果集中该字段就使用别名 作为该列的名字. 若希望别名区分大小写或者含有空格,那么 该别名可以使用双引号括起来. SELECT ename,sal*12 "s al" FROM emp AND,OR AND优先级高于OR,可以通过括号 提高优先级. SELECT

MySQL多表关联查询与存储过程

1.多表关联查询 --  **************关联查询(多表查询)**************** -- 需求:查询员工及其所在部门(显示员工姓名,部门名称) -- 1.1 交叉连接查询(不推荐.产生笛卡尔乘积现象:4 * 4=16,有些是重复记录) SELECT empName,deptName FROM employee,dept; -- 需求:查询员工及其所在部门(显示员工姓名,部门名称) -- 多表查询规则:1)确定查询哪些表   2)确定查询哪些字段   3)表与表之间连接条件

mysql-(四)-关联查询

建立数据表 //部门表create table dept( id int primary key, deptName varchar(20) ) //员工表 create table employee( id int primary key, empName varchar(20), deptId int , --部门名称 CONSTRAINT employee_dept_fk FOREIGN KEY(deptId) references dept(id) 外键名称 外键 参考表(参考字段) )

使用resultMap定义查询结果集,实现关联查询

接下来介绍resultMap定义查询结果集,实现关联查询 1 首先在接口中定义操作的方法 public interface EmployeeMapperPlus { public Employee getEmpAndDept(Integer id); } 2在xml里进行配置 <!--第一种进行配置 联合查询:级联属性封装结果集 --> <resultMap type="com.atguigu.mybatis.bean.Employee" id="MyDif

Hibernate-ORM:11.Hibernate中的关联查询

------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 本篇博客将讲述Hibernate中的关联查询,及其级联(cascade)操作,以及指定哪一方维护关联关系的(inverse) 一,讲述目录如下: 1.单向一对多:(增加一个区县及其它以下的对应街道) 2.单项一对多:(查询一个区县,以及它下面所有的对应街道) 3.单项多对一:(查询一个指定的街道,并同时展示出其对应的区县) 4.双向一对多(多对一):(值得注意:toString()套路不对容易引发错误Err

第八节:mybatis关联查询之一对一查询

一对一也就是 A 表的一条记录对应 B 表的一条记录,下面的测试数据中,从employee 表来看,一个员工对应一个部门,是一对一关系,如果从部门角度来看,则是一对多的关系,一个部门对应多个员工,本节主要研究一对一的关系. 1,数据表建立 新建数据表department,有两个字段,插入两条数据如下: id dept_name 1 CIA 2 FSB 新建数据表employee,有三个字段,其中dept_id是外键,关联department表的主键id.插入数据如下: id last_name

mybatis中的关联查询

1>在实体映射层中引入关联对象 package com.jinglin.hotelsup.model; import java.io.Serializable; public class Goodsinfo implements Serializable{ private Integer goodsid; private Integer companyid; private Integer goodstypeid; private Integer unitid; private String c