1. 程式人生 > >MyBatis學習總結(九)---基於XML多表聯合查詢(一對一、一對多、多對多)

MyBatis學習總結(九)---基於XML多表聯合查詢(一對一、一對多、多對多)

1、一對一的關聯

 使用association,association元素用於處理“has-one”(一對一)這種型別關係。
 作用:針對pojo物件屬性的對映,它的兩個主要引數此時對應的值: javaType對應pojo類名,  property對應pojo的屬性名,  。

 示例:

  

  Employee.java

public class Employee implements Serializable{
	private int empId;
	private String empName;
    private Date empBirthDay;
	private String empSex;
	private Department dept;
    
    //Getters and Setters
    //consructor


}

   Department.java

public class Department {
	private int deptId;
	private String deptName;
	private List<Employee> emps;

   //...
}

方式一:巢狀結果:使用巢狀結果對映來處理重複的聯合結果的子集,封裝聯表查詢的資料(去除重複的資料)。

根據條件查詢員工,每一個員工對應一個部門

 employeeMapper.xml

!-- 一對一關聯,方式一:巢狀結果:使用巢狀結果對映來處理重複的聯合結果的子集
             封裝聯表查詢的資料(去除重複的資料) -->
<select id="selectEmpsByYears" parameterType="hashmap" resultMap="emp" >
   SELECT * FROM tb_emp WHERE empBirthDay between #{startTime} and #{endTime} ;
</select>

<resultMap type="com.mybatisstudy.model.Employee" id="emp">
   <id property="empId" column="empId" />
   <result property="empName" column="empName"/>
   <result property="empBirthDay" column="empBirthDay"/>
   <result property="empSex" column="empSex"/>
   <association property="dept" javaType="com.mybatisstudy.model.Department">    
	 <result property="deptId" column="deptId"/>
    </association>
</resultMap>

 方式二:巢狀查詢:通過執行另外一個SQL對映語句來返回預期的複雜型別

   employeeMapper.xml

<select id="selectAllEmp" resultMap="allEmp">
   SELECT * FROM tb_emp;
</select>

<resultMap type="com.mybatisstudy.model.Employee" id="allEmp">
   <id property="empId" column="empId" />
   <result property="empName" column="empName"/>
   <result property="empBirthDay" column="empBirthDay"/>
   <result property="empSex" column="empSex"/>
   <association property="dept" 
  		javaType="com.mybatisstudy.model.Department"
  		column="deptId" 
  		select="com.mybatisstudy.dao.IDepartmentDao.selectDeptById">
    </association>
</resultMap>

  departmentMapper.xml

 <select id="selectDeptById" resultMap="baseCardResultMap">
      SELECT * FROM tb_dept d WHERE d.deptId=#{deptId};
   </select> 
   
   <resultMap type="com.mybatisstudy.model.Department" id="baseCardResultMap">
        <id property="deptId" column="deptId" />
         <result property="deptName" column="deptName"/>
   </resultMap>

2 、一對多關聯

使用collection ,此時其ofType對應pojo的類名,javaType對應ArrayList。

方式一: 方式一: 巢狀結果: 使用巢狀結果對映來處理重複的聯合結果的子集

  departmentMapper.xml

 <!-- 一對多 , 方式一: 巢狀結果: 使用巢狀結果對映來處理重複的聯合結果的子集 -->
   <select id="getDeptsAndEmps" resultMap="deptsAndEmps" >
      SELECT empId,empName,empBirthDay,empSex,tb_dept.deptId,tb_dept.deptName FROM `tb_emp`,tb_dept WHERE tb_emp.deptId = tb_dept.deptId;
   </select>
   
   <resultMap id="deptsAndEmps" type="com.mybatisstudy.model.Department">
      <id property="deptId" column="deptId" />
      <result property="deptName" column="deptName"/>
      <collection property="emps" ofType="com.mybatisstudy.model.Employee">
         <result property="empId" column="empId" />
         <result property="empName" column="empName" />
         <result property="empBirthDay" column="empBirthDay" />
         <result property="empSex" column="empSex" />
      </collection>
   </resultMap>

   方式二:巢狀查詢:通過執行另外一個SQL對映語句來返回預期的複雜型別 

     departmentMapper.xml

 <!-- 一對多, 方式二:巢狀查詢:通過執行另外一個SQL對映語句來返回預期的複雜型別 -->
    <resultMap id="allDeptandEmp" type="com.mybatisstudy.model.Department"  >
         <id property="deptId" column="deptId" />
         <result property="deptName" column="deptName"/>
         <collection property="emps" 
                     column="deptId" 
  		             ofType="com.mybatisstudy.model.Employee" 		            
  		             select="com.mybatisstudy.dao.IEmployeeDao.selectEmpsBydeptId">
         </collection>
    </resultMap>
    
    <select id="selectAllDeptAndEmp" resultMap="allDeptandEmp">
       SELECT * FROM tb_dept;
    </select> 

    employeeMapper.xml 

<!-- 協助一對多,方式二 -->
 <resultMap id="baseStudentResultMap" type="com.mybatisstudy.model.Employee">
   <id property="empId" column="empId" />
   <result property="empName" column="empName"/>
   <result property="empBirthDay" column="empBirthDay"/>
   <result property="empSex" column="empSex"/>
</resultMap>
<select id="selectEmpsBydeptId" resultMap="baseStudentResultMap">
   SELECT * FROM tb_emp WHERE deptId=#{deptId};
</select> 

2 、多對多關聯

   如商品表和訂單表之間就是一種多對多的關聯。

   此處主要是通過巢狀查詢的方式去處理。(如在findGoodsById的resultMap中巢狀一個根據商品的Id去查詢訂單)。

  Goods.java

public class Goods implements Serializable {
	private Integer goodsId;
	private String name;
	private Double price;
	private String remark;
	private List<Order> orders;
	//getters and setters
    //constructor
}

  goodsMapper.xml 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd" >
<mapper namespace="com.gec.mapper.goodsMapper">
	<resultMap type="article" id="basegoodsResultMap">
		<id column="id" property="goodsId"/>
		<result column="name" property="name"/>
		<result column="price" property="price"/>
		<result column="remark" property="remark"/>
	</resultMap>
	<resultMap type="article" id="findArtcleByIdResultMap" extends="basegoodsResultMap">
		<collection property="orders" javaType="ArrayList"
			ofType="com.gec.domain.Goods" column="id"
			select="com.gec.mapper.OrderMapper.findOrderBygoodsId"
		>
		</collection>
	</resultMap>
	<!-- 根據訂單id查詢商品 -->
	<select id="findGoodsByOrderId" resultMap="basegoodsResultMap">
		select * from tb_goods  where id 
		in (select goods_id from tb_item where order_id=#{id}) 
	</select>
	<select id="findGoodsById" resultMap="findGoodsByIdResultMap">
		select * from tb_goods  where id=#{id}
	</select>
</mapper>

   Order.java

public class Order {
	private Integer orderid;
	private String code;
	private Double total;
	private List<goods> goods;
	//...
}

  orderMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd" >
<mapper namespace="com.gec.mapper.OrderMapper">
	<resultMap type="order" id="baseOrderResultMap">
		<id column="orderId" property="orderid"/>
		<result column="code" property="code"/>
		<result column="total" property="total"/>
	</resultMap>
	<resultMap type="order" id="queryOrderByUserIdRsultMap" extends="baseOrderResultMap">
		<collection property="goods" javaType="ArrayList"
			ofType="Goods" column="orderId"
			select="com.gec.mapper.ArticleMapper.findArtcleByOrderId">
		</collection>
	</resultMap>
	<!-- 根據商品id查詢訂單 -->
	<select id="findOrderBygoodsId" resultMap="baseOrderResultMap">
		select * from tb_order  where id 
		in (select order_id from tb_item where goods_id=#{id}) 
	</select>
</mapper>