mapper中的自定义映射

自定义映射的用法之一,解决表格查询的字段名和实体类中不一致的情况

<resultMap id="userMap" type="user">
        <id column="id" property="id"></id>
        <result column="user_name" property="userName"></result>
        <result column="password" property="password"></result>
</resultMap>

mybatis对象的关联关系

     一对多关系处理(一方)

<resultMap id="selectByEIdMap" type="cn.kgc.mybatis.entity.Emp">
        <result column="id" property="dept.id" ></result>
        <result column="dname" property="dept.dName"></result>
 </resultMap>


    <resultMap id="selectByEIdMap2" type="cn.kgc.mybatis.entity.Emp">
        <id column="eid" property="eId"></id>
        <result column="ename" property="EName"></result>
        <result column="age" property="age"></result>
        <result column="deptno" property="deptNo"></result>
        <!--实体对象标识-->
       <association property="dept" javaType="dept">
           <id column="id" property="id"></id>
           <result column="dname" property="dName"></result>
       </association>
    </resultMap>

	<!-- 分步查询 -->
    <resultMap id="selectByEIdMap3" type="cn.kgc.mybatis.entity.Emp">
        <id column="eid" property="eId"></id>
        <result column="ename" property="EName"></result>
        <result column="age" property="age"></result>
        <result column="deptno" property="deptNo"></result>
        <association property="dept" select="cn.kgc.mybatis.mapper.DeptMapper.selectById" 
                                    column="deptno" 
                                    fetchType="eager">
        </association>
    </resultMap>

注:延迟加载设置  :
		1. <setting name="lazyLoadingEnabled" value="true"/> 
		2. <setting name="aggressiveLazyLoading" value="false"/>  3.4.1之前的版本需要设置

    <select id="selectByEId" resultMap="selectByEIdMap2">
            select  * from emp left  join  dept on  emp.deptno = dept.id  where eid = #{eid}
    </select>

    <select id="selectByEId2" resultMap="selectByEIdMap3">
            select  * from emp  where eid = #{eid}
    </select>

一对多关系处理(多方)

 <resultMap id="BaseResultMap" type="cn.kgc.mybatis.entity.Dept">
            <id property="id" column="id" />
            <result property="dName" column="dname" />
            <collection property="emps" ofType="emp">
                <id column="eid" property="eId"></id>
                <result column="ename" property="EName"></result>
                <result column="age" property="age"></result>
                <result column="deptno" property="deptNo"></result>
            </collection>
    </resultMap>


    <resultMap id="BaseResultMap2" type="cn.kgc.mybatis.entity.Dept">
        <id property="id" column="id" />
        <result property="dName" column="dname" />
        <collection property="emps" select="cn.kgc.mybatis.mapper.EmpMapper.selectByDeptId" column="id"></collection>
    </resultMap>

    <select id="selectById" resultType="dept">
        select  * from dept where id = #{id}
    </select>

    <select id="selectById2" resultMap="BaseResultMap">
        select emp.* ,dept.* from dept left join emp on dept.id = emp.deptno where id = #{id}
    </select>

    <select id="selectById3" resultMap="BaseResultMap2">
        select dept.* from dept  where id = #{id}
    </select>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值