Mybatis联合查询

mybatis返回结果ResultMap

<?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">
<!-- namespace:要实现的接口的全类名 -->
<mapper namespace="com.atguigu.mybatis.mapper.EmployeeMapper2">
    <!-- 
        需求:在查询员工信息的同时将部门信息也查询出来
     -->
     <select id="getEmployeeByIdContainsDept" resultMap="cutomMap">
         SELECT e.id e_id,e.last_name lastName,e.email,e.salary,d.id d_id,d.name d_name 
        FROM employees e
        LEFT JOIN departments d
        ON e.dept_id = d.id
        WHERE e.id = #{id};
     </select>
     
     <!-- 自定义结果集 -->
     <!-- 
         type:指定要映射的对象所对应的类的全类名
         id:起一个唯一的标识,以便被引用
      -->
     <resultMap type="com.atguigu.mybatis.entities.Employee2" id="cutomMap">
         <!-- 映射主键 
             column:指定数据库中的列名
             property:指定要映射的对象的属性名
         -->
         <id column="e_id" property="id"/>
         <!-- 映射其他列 -->
         <result column="lastName" property="lastName"/>
         <result column="email" property="email"/>
         <result column="salary" property="salary"/>
         <!-- 通过级联属性进行映射 -->
         <result column="d_id" property="dept.id"/>
         <result column="d_name" property="dept.deptName"/>
     </resultMap>
</mapper>

 

    <!-- 
        需求:在查询员工信息的同时将部门信息也查询出来
     -->
     <select id="getEmployeeByIdContainsDept" resultMap="cutomMap">
         SELECT e.id e_id,e.last_name lastName,e.email,e.salary,d.id d_id,d.name d_name 
        FROM employees e
        LEFT JOIN departments d
        ON e.dept_id = d.id
        WHERE e.id = #{id};
     </select>
     
     <!-- 自定义结果集 -->
     <!-- 
         type:指定要映射的对象所对应的类的全类名
         id:起一个唯一的标识,以便被引用
      -->
     <resultMap type="com.atguigu.mybatis.entities.Employee" id="cutomMap">
         <!-- 映射主键 
             column:指定数据库中的列名
             property:指定要映射的对象的属性名
         -->
         <id column="e_id" property="id"/>
         <!-- 映射其他列 -->
         <result column="lastName" property="lastName"/>
         <result column="email" property="email"/>
         <result column="salary" property="salary"/>
         <!-- 通过级联属性进行映射 -->
         <result column="d_id" property="dept.id"/>
         <result column="d_name" property="dept.deptName"/>
     </resultMap>
     使用<association >联合查询
     <select id="getEmployeeByWithAssociation" resultMap="customMap2">
         SELECT e.*,d.id d_id,d.name d_name 
        FROM employees e
        LEFT JOIN departments d
        ON e.dept_id = d.id
        WHERE e.id = #{id};
     </select>
     
     <!-- 自定义结果集 -->
     <resultMap type="com.atguigu.mybatis.entities.Employee" id="customMap2">
         <!-- 映射主键 -->
         <id column="id" property="id"/>
         <!-- 映射其他列 -->
         <result column="last_name" property="lastName"/>
         <result column="email" property="email"/>
         <result column="salary" property="salary"/>
         <!-- 通过association进行联合查询
             property:设置要映射属性的属性名
             javaType:设置要映射的属性的类型
             
          -->
         <association property="dept" javaType="com.atguigu.mybatis.entities.Department">
             <!-- 映射主键 -->
             <id column="d_id" property="id"/>
             <!-- 映射其他列 -->
             <result column="d_name" property="deptName"/>
         </association>
     </resultMap>

分步骤查询

public Employee getDepartmentById(Integer id);

<mapper namespace="com.atgui.spring.mybatis.mapper.DepartmentMapper">
 <select id="getDepartmentById" resultType="com.atgui.spring.mybatis.entities.Department">
    select id,name from departments where id=#{id}
  </select>

 

public Employee getEmployeeAndDepartmentByAssociationFenbu(Integer id);

<!-- 
         通过association进行分步查询:
             1.根据员工的id将Employee查询出来
             2.根据Employee中部门的id将部门查询出来
             3.将部门设置到Employee中
      -->
<select id="getEmployeeAndDepartmentByAssociationFenbu" resultMap="customMap3">
    select * from employees where id=#{id}
  </select>
  
    

 <resultMap type="com.atgui.spring.mybatis.entities.Employee" id="customMap3">
      <id column="e_id" property="id"></id>
      <result column="last_name" property="lastName"/>
      <result column="email" property="email"/>
      <result column="salary" property="salary"/>
    <result column="dept_id" property="deptId"/>
      <association property="dept" select="com.atgui.spring.mybatis.mapper.DepartmentMapper.getDepartmentById"
       column="dept_id">
      </association>
  </resultMap>

<!-- 自定义 结果集 -->
      <resultMap type="com.atguigu.mybatis.entities.Employee" id="customMap3">
          <!-- 映射主键 -->
         <id column="id" property="id"/>
         <!-- 映射其他列 -->
         <result column="last_name" property="lastName"/>
         <result column="email" property="email"/>
         <result column="salary" property="salary"/>
         <!-- 通过association进行分步查询 
             select:设置调用那个接口的那个方法查询该属性
             column:设置将Employe中那一列的值传入到select属性中的方法中
         -->

 

多个参数查询

<!-- 
              扩展:分步查询传入多个值得情况:
                  使用{key1=column1,key2=column2…}的形式来传入多个值
                  
           -->

    <resultMap type="com.atgui.spring.mybatis.entities.Employee" id="customMap3">
      <id column="e_id" property="id"></id>
      <result column="last_name" property="lastName"/>
      <result column="email" property="email"/>
      <result column="salary" property="salary"/>
    <result column="dept_id" property="deptId"/>
      <association property="dept" select="com.atgui.spring.mybatis.mapper.DepartmentMapper.getDepartmentById"
       column="{deptId=dept_id}" fetchType="eager">
      </association>
  </resultMap>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值