当前位置: 首页 > 工具软件 > Npoi.Mapper > 使用案例 >

mapper中的自定义映射

丌官远
2023-12-01

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

<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>

 类似资料: