1. 程式人生 > >MyBatis的級聯查詢(兩種方式)

MyBatis的級聯查詢(兩種方式)

https://blog.csdn.net/zhupengqq/article/details/78575767


與上次唯一不同的一下幾個類

Department.java

package com.cn.zhu.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 [departmentName=" + departmentName + ", id=" + id
                + "]";
    }
    
}
Employee.java
package com.cn.zhu.bean;
 
import org.apache.ibatis.type.Alias;
 
@Alias("emp")
public class Employee {
    private Integer id;
    private String lastName;
    private String email;
    private String gender;
    private Department dept;
    
    public Employee() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    public Employee(Integer id, String lastName, String email, String gender) {
        super();
        this.id = id;
        this.lastName = lastName;
        this.email = email;
        this.gender = gender;
    }
 
    
    public Department getDept() {
        return dept;
    }
 
    public void setDept(Department dept) {
        this.dept = dept;
    }
 
    public String getLastName() {
        return lastName;
    }
 
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
 
    
    public Integer getId() {
        return id;
    }
 
    public void setId(Integer id) {
        this.id = id;
    }
 
    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 [email=" + email + ", gender=" + gender + ", id=" + id
        + ", lastName=" + lastName + "]";
    }
 
}

EmployeeMapperPlus.java
package com.cn.mybatis.dao;
 
import java.util.List;
import java.util.Map;
 
 
import org.apache.ibatis.annotations.MapKey;
import org.apache.ibatis.annotations.Param;
 
import com.cn.zhu.bean.Employee;
 
public interface EmployeeMapperPlus {
    public Employee getEmpById(Integer id);
    public Employee getEmpAndDept(Integer id);
 }

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.cn.mybatis.dao.EmployeeMapperPlus">
 
    <!--
        自定義某個javaBean的封裝規則 id 定義主鍵會底層有優化 type: 自定義規則的java型別 property :
        唯一id方便引用
    -->
    <!--
        <resultMap type="com.cn.zhu.bean.Employee" id="MySimpleEmp"> 指定主鍵的封裝規則
        column 指定哪一列 property 指定對應的javaBean屬性 <id column="id" property="id"/>
        定義普通列封裝 <result column="last_name" property="lastName"/> 其他不指定的列會自動封裝
        ,我們只要寫resultMap就把全部的對映規則都寫上 <result column="email" property="email"/>
        <result column="gender" property="gender"/> </resultMap>
    --><!-- public Employee getEmpById(Integer id); -->
    <!-- resultMap: 自定義結果集對映規則 -->
    <!--
        <select id="getEmpById" resultMap="MyEmp"> select * from tal_employee
        where id=#{id} </select>
    -->
    <!--
        場景一 : 查詢Employee的同時查詢員工對應的部門 Employee ===department 一個員工有與之對應的部門資訊
    -->
 
    <resultMap type="com.cn.zhu.bean.Employee" id="MyDifEmp">
        <!--
            指定主鍵的封裝規則 column 指定哪一列 property 指定對應的javaBean屬性
        -->
        <!-- 
                  聯合查詢 :級聯屬性封裝結果
        -->
        <id column="id" property="id" />
        <!-- 定義普通列封裝 -->
        <result column="last_name" property="lastName" />
        <!-- 其他不指定的列會自動封裝 ,我們只要寫resultMap就把全部的對映規則都寫上 -->
        <result column="gender" property="gender" />
        <result column="did" property="dept.id" />
        <result column="dept_name" property="dept.departmentName" />
    </resultMap>
 
  <!--  使用association  定義單個物件的封裝規則 -->
    <resultMap type="com.cn.zhu.bean.Employee" id="MyDifEmp2">
        <id column="id" property="id" />
        <!-- 定義普通列封裝 -->
        <result column="last_name" property="lastName" />
        <!-- 其他不指定的列會自動封裝 ,我們只要寫resultMap就把全部的對映規則都寫上 -->
        <result column="gender" property="gender" />
        <!--association  可以指定聯合的javaBean物件
          property 指定哪個是聯合的物件
          javaType: 指定這個屬性物件的型別
          
          -->
        <association property="dept" javaType="com.cn.zhu.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.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>
以上是兩種對映封裝結果


增加EmployeeMapperPlus.xml 後,一定要在mybatis-config.xml里加上這個配置檔案的宣告,
<mappers>
        <mapper resource="mybatis/mapper/EmployeeMapperPlus.xml" />
    </mappers>
另外sql語句一定要正確,可以先在navict資料庫圖形化管理工具中,測試sql


下面編寫測試類(這裡直接寫測試方法吧):詳細請看對映一

@Test
    public void test05() throws IOException{
        SqlSessionFactory sqlsessionFactory=getSqlSessionFactory();
        // 1  獲取到的sqlsession不會自動提交資料
        SqlSession openSession=sqlsessionFactory.openSession();
 
        try{
            
            EmployeeMapperPlus mapper=openSession.getMapper(EmployeeMapperPlus.class);
            Employee empAndDept=mapper.getEmpAndDept(1);
            System.out.println(empAndDept);
            System.out.println(empAndDept.getDept());
        }finally{
            openSession.commit();
        }
    }

測試完成

https://www.cnblogs.com/xdp-gacl/p/4264440.html  還可參考該文章