mybatis中resultMap的几种用法

基本定义

  1. resultMap自定义结果集映射规则
  2. resultMap与resultType只能二选一
 <resultMap type="emp" id="emps">
    <id column="id" property="id" />
    <result column="last_name" property="lastName" />
    <result column="email" property="email" />
    <result column="gender" property="gender"/>
  </resultMap>


    <select id="getResultMap"  resultMap="emps" databaseId="mysql"> 
   select last_name, email,id,gender from employee where id = #{id} 
    </select> 
  1. ID标签对应主键属性
    2.resultMap要对应相应resultMap的id

关联查询,级联属性封装

<resultMap type="emp" id="emps">
    <id column="id" property="id" />
    <result column="last_name" property="lastName" />
    <result column="email" property="email" />
    <result column="gender" property="gender"/>
    <result column="id" property="departMent.id"/>
    <result column="dept_name" property="departMent.departmentName" />
  </resultMap>

    <select id="getEmpAndDep"  resultMap="emps" databaseId="mysql"> 
   select * from employee e,department d where e.d_id=d.id and e.id=#{id}
    </select> 

利用association定义关联对象封装规则

  1. association可以指定联合的javaBean对象
  2. property=“”指定哪个属性是联合的对象
  3. javaType 指定这个属性对象的类型(不能省略)
 <resultMap type="emp" id="empsk">
    <id column="id" property="id" />
    <result column="last_name" property="lastName" />
    <result column="email" property="email" />
    <result column="gender" property="gender"/>
    <association property="departMent" javaType="dep">
        <id column="did" property="id" />
        <result column="dept_name" property="departmentName" />
    </association>
  </resultMap>




    <select id="getEmpAndDep"  resultMap="empsk" databaseId="mysql"> 
   select e.last_name last_name, e.email email, e.id,e.gender gender,e.d_id ,d.id did, d.dept_name dept_name from employee e,department d where e.d_id=d.id and e.id=#{id}
    </select> 

利用association进行分步查询

  1. 先按照员工id查询员工信息
  2. 根据查询员工信息中的d_id值去部门表查询
  3. 部门设置到员工中
  4. select表明当前属性是调用select指定的方法查出的结果
  5. column 指定将哪一列的值传给这个方法
<resultMap type="emp" id="emps3">
    <id column="id" property="id" />
    <result column="last_name" property="lastName" />
    <result column="email" property="email" />
    <result column="gender" property="gender"/>
    <association property="departMent" 
     select="com.dao.DepartMentMapper.getDepartMent"
     column="d_id">
    </association>
  </resultMap>



    <select id="getEmpAndDep"  resultMap="emps3" databaseId="mysql"> 
   select last_name,email,id,gender,d_id from employee e where id=#{id}
    </select> 

分布查询之延迟加载

 <setting name="lazyLoadingEnabled" value="true"/>
    <setting name="aggressiveLazyLoading" value="false"/>
  1. 需要在全局配置文件中加入这两个设置
  2. lazyLoadingEnabled 会导致分布查询时候按需加载,只有在用到某个属性或对象的时候才会去查询
  3. aggressiveLazyLoading 侵入懒加载是指,当需要某个对象的级联属性时,其它属性也会顺便加载,而false则只加载当前属性。

关联查询 collection定义关联集合封装规则

<resultMap type="dep" id="depls">
    <id column="did" property="id"/>
    <result column="dept_name" property="departmentName"/>
    <collection property="empls" ofType="emp">
        <id column="eid" property="id"/>
        <result column="last_name" property="lastName" />
        <result column="email" property="email" />
        <result column="gender" property="gender"/>
    </collection>

 </resultMap>

    <select id="getDepartMentBylist"  resultMap="depls" databaseId="mysql"> 
        select d.id did,d.dept_name,e.id eid,e.last_name,e.email,e.gender 
        from department d 
        left join employee e 
        on d.id=e.d_id 
        where d.id=#{id}
    </select> 
  1. collection标签的oftype是指集合中元素的类型
  2. property属性指明属于哪一个属性

collection分步查询

 <resultMap type="dep" id="deplf">
    <id column="id" property="id"/>
    <result column="dept_name" property="departmentName"/>
    <collection property="empls"     fetchType="lazy" select="com.dao.EmployeeMapper.getEmpByDid" column="id"></collection>
 </resultMap>
<select id="getDepartMent" resultMap="deplf">
        select id, dept_name from department where id=#{id}
    </select>
  1. 分步查询需要将第一次查询的某个字段值传递给第二步作为参数
  2. 传递多列的值,将多列的值封装成map进行传递column="{key=column1,key2=column2}",然后根据key取出对应值
  3. fetchType=”lazy”表示使用延迟加载,eager立即加载

discriminator 监视器属性

  1. 这个属性会根据某个字段值的情况,动态选择不同的映射
  2. 注意在case标签里面注明返回的类型,即resultType
 <resultMap type="emp" id="empd">
    <id column="id" property="id" />
    <result column="last_name" property="lastName" />
    <result column="email" property="email" />
    <result column="gender" property="gender"/>

    <discriminator javaType="int" column="gender">
        <case value="1" resultType="emp">
            <association property="departMent" javaType="dep">
                <id column="did" property="id" />
                <result column="dept_name" property="departmentName" />
            </association>
        </case>
        <case value="0" resultType="emp">
            <id column="id" property="id" />
            <result column="email" property="lastName" />
            <result column="email" property="email" />
            <result column="gender" property="gender"/>
        </case>
    </discriminator>
  </resultMap>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值