zoukankan      html  css  js  c++  java
  • mybatis 联结查询

    一、查询 员工(tbl_employee)时,关联 查询出 员工对于的部门信息 (tab1_dept),一对一查询,或者多对一查询 适用

      emp bean里面 包含 部门bean dept属性对象

      1.select 如下,

    	<select id="getEmpAndDept" resultMap="MyDifEmp">
    		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>
    

      2. resultMap:有两种方式,

      第一种,直接用关联的dept.id.

      第二种,association可以指定联合的javaBean对象

    	<!--
    		联合查询:级联属性封装结果集
    	  -->
    	<resultMap type="com.atguigu.mybatis.bean.Employee" id="MyDifEmp">
    		<id column="id" property="id"/>
    		<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>
    
    
    	<!-- 
    		使用association定义关联的单个对象的封装规则;
    	 -->
    	<resultMap type="com.atguigu.mybatis.bean.Employee" id="MyDifEmp2">
    		<id column="id" property="id"/>
    		<result column="last_name" property="lastName"/>
    		<result column="gender" property="gender"/>
    		
    		<!--  association可以指定联合的javaBean对象
    		property="dept":指定哪个属性是联合的对象
    		javaType:指定这个属性对象的类型[不能省略]
    		-->
    		<association property="dept" javaType="com.atguigu.mybatis.bean.Department">
    			<id column="did" property="id"/>
    			<result column="dept_name" property="departmentName"/>
    		</association>
    	</resultMap>

    二、association分步查询 ,

    1、先按照员工id查询员工信息
    2、根据查询员工信息中的d_id值去部门表查出部门信息
    3、部门设置到员工中;

    sql:

    	 <select id="getEmpByIdStep" resultMap="MyEmpByStep">
    	 	select * from tbl_employee where id=#{id}
    	 	<if test="_parameter!=null">
    	 		and 1=1
    	 	</if>
    	 </select>
    

    resultMap:指定association 标签里面,定义一个select 引入查询部门的

    	 <resultMap type="com.atguigu.mybatis.bean.Employee" id="MyEmpByStep">
    	 	<id column="id" property="id"/>
    	 	<result column="last_name" property="lastName"/>
    	 	<result column="email" property="email"/>
    	 	<result column="gender" property="gender"/>
    	 	<!-- association定义关联对象的封装规则
    	 		select:表明当前属性是调用select指定的方法查出的结果
    	 		column:指定将哪一列的值传给这个方法
    	 		
    	 		流程:使用select指定的方法(传入column指定的这列参数的值)查出对象,并封装给property指定的属性
    	 	 -->
     		<association property="dept" 
    	 		select="com.atguigu.mybatis.dao.DepartmentMapper.getDeptById"
    	 		column="d_id">
     		</association>
    	 </resultMap>

    三、查询部门时,查出所有下的员工,一对多

    sql:

    	<select id="getDeptByIdPlus" resultMap="MyDept">
    		SELECT d.id did,d.dept_name dept_name,
    				e.id eid,e.last_name last_name,e.email email,e.gender gender
    		FROM tbl_dept d
    		LEFT JOIN tbl_employee e
    		ON d.id=e.d_id
    		WHERE d.id=#{id}
    	</select>  

    resultMap:使用collection标签定义关联的集合类型

    	<!--嵌套结果集的方式,使用collection标签定义关联的集合类型的属性封装规则  -->
    	<resultMap type="com.atguigu.mybatis.bean.Department" id="MyDept">
    		<id column="did" property="id"/>
    		<result column="dept_name" property="departmentName"/>
    		<!-- 
    			collection定义关联集合类型的属性的封装规则 
    			ofType:指定集合里面元素的类型
    		-->
    		<collection property="emps" ofType="com.atguigu.mybatis.bean.Employee">
    			<!-- 定义这个集合中元素的封装规则 -->
    			<id column="eid" property="id"/>
    			<result column="last_name" property="lastName"/>
    			<result column="email" property="email"/>
    			<result column="gender" property="gender"/>
    		</collection>
    	</resultMap> 

    四、collection:分段查询

    sql

    <select id="getDeptByIdStep" resultMap="MyDeptStep">
    		select id,dept_name from tbl_dept where id=#{id}
    	</select>  

    resultMap:指定collection标签里面,定义一个select 引入查询员工的

    	<!-- collection:分段查询 -->
    	<resultMap type="com.atguigu.mybatis.bean.Department" id="MyDeptStep">
    		<id column="id" property="id"/>
    		<id column="dept_name" property="departmentName"/>
    		<collection property="emps" 
    			select="com.atguigu.mybatis.dao.EmployeeMapperPlus.getEmpsByDeptId"
    			column="{deptId=id}" fetchType="lazy"></collection>
    	</resultMap>
    

      

     五、鉴别器

    <!-- =======================鉴别器============================ -->
    	<!-- <discriminator javaType=""></discriminator>
    		鉴别器:mybatis可以使用discriminator判断某列的值,然后根据某列的值改变封装行为
    		封装Employee:
    			如果查出的是女生:就把部门信息查询出来,否则不查询;
    			如果是男生,把last_name这一列的值赋值给email;
    	 -->
    	 <resultMap type="com.atguigu.mybatis.bean.Employee" id="MyEmpDis">
    	 	<id column="id" property="id"/>
    	 	<result column="last_name" property="lastName"/>
    	 	<result column="email" property="email"/>
    	 	<result column="gender" property="gender"/>
    	 	<!--
    	 		column:指定判定的列名
    	 		javaType:列值对应的java类型  -->
    	 	<discriminator javaType="string" column="gender">
    	 		<!--女生  resultType:指定封装的结果类型;不能缺少。/resultMap-->
    	 		<case value="0" resultType="com.atguigu.mybatis.bean.Employee">
    	 			<association property="dept" 
    			 		select="com.atguigu.mybatis.dao.DepartmentMapper.getDeptById"
    			 		column="d_id">
    		 		</association>
    	 		</case>
    	 		<!--男生 ;如果是男生,把last_name这一列的值赋值给email; -->
    	 		<case value="1" resultType="com.atguigu.mybatis.bean.Employee">
    		 		<id column="id" property="id"/>
    			 	<result column="last_name" property="lastName"/>
    			 	<result column="last_name" property="email"/>
    			 	<result column="gender" property="gender"/>
    	 		</case>
    	 	</discriminator>
    	 </resultMap>
    

      

  • 相关阅读:
    get函数理解
    get函数理解
    Absolute C++ 第9章字符串 编程练习4
    Absolute C++ 第9章字符串 编程练习4
    Absolute C++ 第9章字符串 编程练习4
    Absolute C++ 第9章字符串 编程练习4
    C字符串使用陷阱 “=”和“==” 学习笔记
    [Android]ListView中分割线的设置
    如何在adapter 中调用activity的方法
    Android开发中Handler的经典总结
  • 原文地址:https://www.cnblogs.com/GotoJava/p/7439829.html
Copyright © 2011-2022 走看看