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() {
        super();
    }  
    public Employee(Integer eId,String eName, Integer gender, String email) {
        super();
        this.eId=eId;
        this.eName = eName;
        this.gender = gender;
        this.email = email;
    }


    public Integer geteId() {
        return eId;
    }
    public void seteId(Integer eId) {
        this.eId = eId;
    }
   
    public String getEName() {
        return eName;
    }
    public void setEname(String ename) {
        this.eName = ename;
    }
    public Integer getGender() {
        return gender;
    }
    public void setGender(Integer gender) {
        this.gender = gender;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    } 
    public Department getDept() {
        return dept;
    }
    public void setDept(Department dept) {
        this.dept = dept;
    }
    @Override
    public String toString() {
        return "Employee [eId=" + eId + ", ename=" + eName + ", gender=" + gender + ", email=" + email + "]";
    }

}

 

 Department.java實體類

 

package com.hand.mybatis.bean;
import java.util.List;

public class Department {
    private Integer id;
    private String departName;
    private List<Employee> empList;
    
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getDepartName() {
        return departName;
    }
    public void setDepartName(String departName) {
        this.departName = departName;
    }
    
    public List<Employee> getEmpList() {
        return empList;
    }
    public void setEmpList(List<Employee> empList) {
        this.empList = empList;
    }
     
    @Override
    public String toString() {
        return "Department [id=" + id + ", departName=" + departName + "]";
    }
   
}

DepartmentMapper.java接口

 

package com.hand.mybatis.dao;
import com.hand.mybatis.bean.Department;

public interface DepartmentMapper {

    Department getDeptById(Integer did);
    
    //根據部門id,查詢部門信息,同時將部門下的所有員工信息查詢出來
     Department getDeptByIdPlus(Integer did);
    
     //分步查詢部門信息
     Department getDeptByIdStep(Integer did);
}

 

DepartmentMapper.xm l映射文件

 

 <?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.hand.mybatis.dao.DepartmentMapper">

   <!-- Department getDeptById(Integer did); -->
   <select id="getDeptById"  resultType="com.hand.mybatis.bean.Department">
       SELECT did id,deptname departName FROM dept WHERE did=#{did}
   </select>
   
    <!--1.查詢部門的時候將部門對應的所有員工信息也查詢出來
    -->
   
    <resultMap type="com.hand.mybatis.bean.Department" id="departmentMap">
        <result property="id" column="did"/>
        <result property="departName" column="deptname"/>
        <!--collection定義關聯集合類型的屬性的封裝規則
            ofType:指定集合里面元素的類型  -->
        <collection property="empList" ofType="com.hand.mybatis.bean.Employee">
         <!-- 定義這個集合元素的封裝規則 -->
          <id column="eid" property="eId"/>
          <result column="ename" property="eName"/>
          <result column="email" property="email"/>
          <result column="gender" property="gender"/>
        </collection>
    </resultMap>
      
    <!-- Department getDeptByIdStep(Integer did); -->   接口一:根據部門did查詢部門信息,員工信息   
    <select id="getDeptByIdPlus" resultMap="departmentMap">
        SELECT d.did,d.deptname,e.eid,e.ename,e.email,e.gender
        FROM dept d
        LEFT JOIN emp e
        ON d.did=e.did
        WHERE d.did=#{did}
    </select>
    
    
    <!--2. select_resultMap關聯查詢collection分步查詢 -->
    
    <resultMap type="com.hand.mybatis.bean.Department" id="departmentMapPlus">
      <id column="did" property="id"/>
      <result column="deptname" property="departName"/>
      <collection property="empList"
          select="com.hand.mybatis.dao.EmployeeMapper.getEmpByDeptId"
          column="{dId=did}" fetchType="lazy">//可以多列傳值
      </collection>
    </resultMap>

   <!--Department getDeptByIdStep(Integer did); -->接口二:根據部門id分步查詢員工信息
   <select id="getDeptByIdStep" resultMap="departmentMapPlus">
      SELECT did,deptname
      from dept
      WHERE did=#{did}
   </select>
 
  <!--  擴展,多列的值傳遞過去,將多列的值封裝map傳遞
       column="{key1=column1,key2=column2}"
       fetchType="lazy":表示使用延遲加載
       - lazy:延遲
       - eager:立即-->
 
</mapper>

 代碼:https://github.com/shuaishuaihand/mybatis.git


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM