resultMap_关联查询1_级联属性

1、项目结构

2、Department.java

package com.atguigu.mybatis.bean;

public class Department {
    
    private Integer id;
    private String departmentName;
    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 + "]";
    }
    

}

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

3、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) ;
}

4、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.Employee;
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 test() throws IOException {
        SqlSessionFactory sqlSessionFactory =getSqlSessionFactory();
        //1、获取到的SqlSession不会自动提交
        SqlSession openSession= sqlSessionFactory.openSession();
        try {
            /*2.获取接口的实现对象*/
            EmployeeMapperPlus mapper= openSession.getMapper(EmployeeMapperPlus.class);
            
            Employee empAndDept=mapper.getEmpAndDept(2);
            
            System.out.println(empAndDept);
            System.out.println(empAndDept.getDept());
            //3、手动提交
            openSession.commit();
            
        } finally {
            openSession.close();
        }
    }
    
}

5、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>
    
    
    <!-- public Employee getEmpAndDept(Integer id) ; -->
    <select id="getEmpAndDept" resultMap="MyDifEmp"> 
        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>
</mapper>

注:① 联合查询:级联属性封装结构集

  <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>
   ②sql语句: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}
  ③result标签中column对应表中列的别名,property对应javabean中属性名

6、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>

    <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"/>
        
       </mappers> 
   
</configuration>

7、控制台打印







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