MyBatis学习总结(九)---基于XML多表联合查询(一对一、一对多、多对多)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_40348465/article/details/84677890

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>

猜你喜欢

转载自blog.csdn.net/qq_40348465/article/details/84677890