Mybatis05_自定义映射ResultMap

25 篇文章 0 订阅
6 篇文章 0 订阅
文章介绍了在Mybatis中如何进行自定义映射,包括当字段名和属性名不一致时的处理,以及一对一和一对多关系的映射方法。通过ResultMap配置,可以解决字段别名问题,实现字段名与Java属性名的对应。同时,文章还提到了使用级联、association和collection标签处理复杂关联查询,以及延迟加载和分步查询的配置策略。
摘要由CSDN通过智能技术生成

Mybatis05_自定义映射ResultMap

  1. 一对一,字段名和属性名不一致

    • 将查询结果的字段名设置别名对应上属性名

      <select id="getEmpById" resultType="Emp">
          select emp_id empId,emp_name empName,age,gender from emp where emp_id = #{emp_id}
      </select>
      
    • 在mybatis核心配置文件中的setting设置

      但是要保证字段名符合mysql的规则使用下划线,属性名符合java规则使用驼峰

      <settings>
              <setting name="mapUnderscoreToCamelCase" value="true"/>
      </settings>
          
      select * from emp where emp_id = #{emp_id}
      
    • 使用自定义映射

      resulType改为设置resultMap的id

      • id对应字段主键,result对应普通字段
      • column对应字段名,property对应属性名
      <resultMap id="EmpResultMap" type="Emp">
              <id column="emp_id" property="empId"></id>
              <result column="emp_name" property="empName"></result>
              <result column="age" property="age"></result>
              <result column="gender" property="gender"></result>
          </resultMap>
          
      <!--    Emp getEmpById(@Param("emp_id") int emp_id);-->
          <select id="getEmpById" resultMap="EmpResultMap">
              select * from emp where emp_id = #{emp_id}
          </select>
      
  2. 一对多关系映射

    • 使用级联方式

      <select id="getEmpAndDeptById" resultMap="empAndDeptResultMap">
              select * from emp
              left join dept
              on emp.dept_id = dept.dept_id
              where emp_id = #{empId}
          </select>
      
      <resultMap id="empAndDeptResultMap" type="Emp">
          <id column="emp_id" property="empId"></id>
          <result column="emp_name" property="empName"></result>
          <result column="age" property="age"></result>
          <result column="gender" property="gender"></result>
          <result column="dept_id" property="dept.deptId"></result>
          <result column="dept_name" property="dept.deptName"></result>
      </resultMap>
      
    • 使用association

      <select id="getEmpAndDeptById" resultMap="empAndDeptResultMapAsso">
              select * from emp
              left join dept
              on emp.dept_id = dept.dept_id
              where emp_id = #{empId}
          </select>
      
      <resultMap id="empAndDeptResultMapAsso" type="Emp">
              <id column="emp_id" property="empId"></id>
              <result column="emp_name" property="empName"></result>
              <result column="age" property="age"></result>
              <result column="gender" property="gender"></result>
      <!--        association:处理多对一映射,即实体类属性
                  property:处理的实体类属性,javaType:属性的类型
                  column字段名property实体类属性的成员属性名-->
              <association property="dept" javaType="Dept">
                  <id column="dept_id" property="deptId"></id>
                  <result column="dept_name" property="deptName"></result>
              </association>
          </resultMap>
      
    • 使用分步查询

      <select id="getEmpandDeptByStepOne" resultMap="empAndDeptByStepResultMap">
              select * from emp where emp_id = #{empId}
          </select>
      
      <resultMap id="empAndDeptByStepResultMap" type="Emp">
              <id column="emp_id" property="empId"></id>
              <result column="emp_name" property="empName"></result>
              <result column="age" property="age"></result>
              <result column="gender" property="gender"></result>
      <!--    property映射的实体类属性
              select下一步查询的sql唯一标识
              column下一步查询的sql的查询条件-->
              <association property="dept"
                           select="com.canyan7n.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo"
                           column="dept_id">
              </association>
          </resultMap>
          
          <select id="getEmpAndDeptByStepTwo" resultType="Dept">
              select * from dept where dept_id = #{deptId}
          </select>
      
  3. 延迟加载

    • 延迟加载,默认值为false

      <setting name="lazyLoadingEnabled" value="true"/>
      
    • 按需加载,默认值为false

      <setting name="aggressiveLazyLoading" value="true"/>
      
  4. 一对多映射

    • collection

      <!--    Dept getDeptAndEmpByDeptId(@Param("deptId") Integer deptId);-->
          <select id="getDeptAndEmpByDeptId" resultMap="DeptAndEmpResultMap">
              select * from dept left join emp on dept.dept_id = emp.dept_id where dept.dept_id = #{deptId}
          </select>
          
      <!--    collection-->
          <resultMap id="DeptAndEmpResultMap" type="Dept">
              <id column="dept_id" property="deptId"></id>
              <result column="dept_name" property="deptName"></result>
              <collection property="emps" ofType="Emp">
                  <id column="emp_id" property="empId"></id>
                  <result column="emp_name" property="empName"></result>
                  <result column="age" property="age"></result>
                  <result column="genger" property="gender"></result>
              </collection>
          </resultMap>
      
    • 分步

      <!--    Dept getDeptAndEmpByStepOne(@Param("deptId") Integer deptId);-->
      <select id="getDeptAndEmpByStepOne" resultMap="DeptAndEmpByStepResultMap">
          select * from dept where dept_id = #{deptId}
      </select>
      
      <!--    分步-->
      <resultMap id="DeptAndEmpByStepResultMap" type="Dept">
          <id column="dept_id" property="deptId"></id>
          <result column="dept_name" property="deptName"></result>
          <association property="emps"
                       select="com.canyan7n.mybatis.mapper.EmpMapper.getDeptAndEmpByStepTwo"
                       column="dept_id" fetchType="eager">
          </association>
      </resultMap>
      
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值