mybatis:resultType、resultMap(级联,association,association分步,collection,collection分步,延迟,discriminator)

一、resultType

****************封装结果resultType***************    
1 封装实体javaBean:resultType就是类的名
2 封装的是List<Employee>:resultType就是List的泛型类的全类名,比如这里就是Employee实体类的全类名
3 封装的如果是单条map,map中key是字段名,值是字段对应的值,:resultType 就是map别名
4 封装的如果是多条map,并且key值是指定的javaBean属性,值是javaBean那么resulttype就是javaBean的全类名,并且接口中用@Mapkey(“lastName”)指定map找那个的key是什么

//多条记录map:key是指定某个列,值就是javaeBean对象
//@Mapkey告诉mabatis中用哪个javabean的属性作为封装的key
@MapKey("id")
public Map<Integer ,Employee> getEmpListByLastNameLikeReturnMap(String lastName);

二、resultMap

****************封装结果:resultMap******************************************

1 、级联封装

<!--联合查询:级联属性封装结果集-->
    <resultMap id="myDifEmp" type="com.mybatis.entity.Employee">
        <id column="id" property="id"/>
        <result column="email" property="email"/>
        <result column="gender" property="gender" />
        <result column="last_name" property="lastName" />
        <result column="d_id" property="dept.id"/>
        <result column="dId" property="dept.id"/>
        <result column="detp_name" property="dept.departmentName"/>
    </resultMap>
    
    <select id="getEmpAndDept" resultMap="myDifEmp" >
        select c.id,c.email,c.gender,c.last_name,c.d_id d_id,d.id dId,d.detp_name detp_name from
         tb_employee c ,tb_dept d where c.d_id = d.id and c.id = #{id}

    </select>

2、association可以指定联合的javaBean对象    

<resultMap id="myDifEmp2" type="com.mybatis.entity.Employee">
        <id column="id" property="id"/>
        <result column="email" property="email"/>
        <result column="gender" property="gender" />
        <result column="last_name" property="lastName" />
        <result column="d_id" property="dept.id"/>
        
        <!--  association可以指定联合的javaBean对象
                property="dept":指定哪个属性是联合的对象
                javaType:指定这个属性对象的类型[不能省略]
                -->
        <association property="dept" javaType="com.mybatis.entity.Department">
            <id column="dId" property="id"/>
            <result column="detp_name" property="departmentName"/>
        </association>
    </resultMap>

<select id="getEmpAndDept" resultMap="myDifEmp2" >
    select c.id,c.email,c.gender,c.last_name,c.d_id d_id,d.id dId,d.dept_name detp_name from
     tb_employee c ,tb_dept d where c.d_id = d.id and c.id = #{id}
</select>

3、association进行分步查询:

<!-- 使用association进行分步查询:
        1、先按照员工id查询员工信息
        2、根据查询员工信息中的d_id值去部门表查出部门信息
        3、部门设置到员工中;
     -->
      <resultMap id="myEmpByStep" type="com.mybatis.entity.Employee">
        <id column="id" property="id"/>
        <result column="email" property="email"/>
        <result column="gender" property="gender" />
        <result column="last_name" property="lastName"/>
        <!-- association定义关联对象的封装规则
             select:表明当前属性是调用select指定的方法查出的结果
             column:指定将哪一列的值传给这个方法

             流程:使用select指定的方法(传入column指定的这列参数的值)查出对象,并封装给property指定的属性

             column="{id=d_id}:其中key就是com.mybatis.dao.DepartmentMapper.getDeptById接口方法的参数名

             如果只有一个参数,key可以省略

       DepartmentMapper接口中的方法:public Department getDeptById(Integer id);

          -->
        <association property="dept" select="com.mybatis.dao.DepartmentMapper.getDeptById" column="{id=d_id}"/>
    </resultMap>

   <!--public Employee getEmpAndDepByStep(Integer id);-->
    <select id="getEmpAndDepByStep" resultMap="myEmpByStep" >
        select * from tb_employee where id = #{id}
    </select>
 

4、collection 封装列表:嵌套结果集

 <!--嵌套结果集的方式,使用collection标签定义关联的集合类型的属性封装规则  -->
    <resultMap id="myDeptPlus" type="com.mybatis.entity.Department">
        <id column="dId" property="id"/>
        <result column="dept_name" property="departmentName" />
        <!--
            collection定义关联集合类型的属性的封装规则
            ofType:指定集合里面元素的类型

        -->
        <collection property="emps" ofType="com.mybatis.entity.Employee">
            <!-- 定义这个集合中元素的封装规则
            column 表示字段是对应字典表中哪个,如果有别名就是用别名,
            -->
            <id column="eId" property="id"/>
            <result column="email" property="email"/>
            <result column="gender" property="gender" />
            <result column="last_name" property="lastName"/>
        </collection>
    </resultMap>
    <!-- public Department getDeptByIdPlus(Integer id);select中的用字段名-->

    <select id="getDeptByIdPlus" resultMap="myDeptPlus">
select d.id dId,d.dept_name,e.id eId,e.last_name,e.email,e.gender from TB_DEPT d left join TB_EMPLOYEE e on d.id=e.d_id where d.id = #{id}
    </select>

5、 collection 分步查询

--  EmployeeMapperPlus接口中定义的方法:public List<Employee> getEmpByDeptId(Integer deptId);

 <resultMap id="MyDeptStep" type="com.mybatis.entity.Department">
        <id column="id" property="id"/>
        <result column="dept_name" property="departmentName"/>
        <collection property="emps" select="com.mybatis.dao.EmployeeMapperPlus.getEmpByDeptId" column="{deptId=id}"/>
    </resultMap>
    
<!-- public Department geteptByIdStep(Integer id);-->

    <select id="geteptByIdStep" resultMap="MyDeptStep" >
        select *  from tb_dept where id = #{id}
    </select>

 

6  、延迟加载
      1、全局配置文件中配置

<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>    
      2、 也可以在association 或者collection的fetchType指定是否延迟加载

7 、discriminator 鉴别器

<!-- =======================鉴别器============================ -->
    <!-- <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>    
    

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值