这个是虚坏叔叔的主站。牢记域名: [xuhss.com](拼音首字母)。首页可以搜索或: 虚幻账号

自定义映射resultMap

虚幻大学 虚幻 256℃ 0评论

? 优质资源分享 ?

学习路线指引(点击解锁) 知识定位 人群定位
? Python实战微信订餐小程序 ? 进阶级 本课程是python flask+微信小程序的完美结合,从项目搭建到腾讯云部署上线,打造一个全栈订餐系统。
?Python量化交易实战? 入门级 手把手带你打造一个易扩展、更安全、效率更高的量化交易系统

resultMap处理字段和属性的映射关系

如果字段名与实体类中的属性名不一致,该如何处理映射关系?

  • 第一种方法:为查询的字段设置别名,和属性名保持一致

下面是实体类中的属性名:

  private Integer empId;
  private String empName;
  private Integer age;
  private String gender;

这是建表时设置的字段名:

emp_id    emp_name    age    gender

我们只需要在Mapper.xml中在写sql语句时,对字段名进行设置别名,使得与属性名一致:

 select emp_id empId,emp_name empName,age,gender from t_emp where emp_id = #{empId}
  • 第二种方法:当字段符合Mysql要求使用下划线,而属性名符合Java要求使用驼峰,此时可以在Mybatis的核心配置文件中设置一个全局配置信息mapUnderscoreToCamelCase,就可以在查询表中数据时,自动将下划线类型的字段名转换为驼峰。
 <settings>

        <setting name="mapUnderscoreToCamelCase" value="true"/>
 settings>
  • 第三种方法:使用resultMap处理

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

    <select id="getEmpByEmpId" resultMap="empResultMap">
        select * from t_emp where emp_id = #{empId}
    select>

多对一的映射关系

1.级联方式处理映射关系

当Emp实体类中具有Dept对象,但是字段中不存在这个属性,我们需要将Dept对象中的属性与查询的字段名建立映射关系。

    <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>
    <select id="getEmpAndDeptByEmpId" resultMap="empAndDeptResultMap">
        select t_emp.*,t_dept.*
        from t_emp left join t_dept on t_emp.dept_id = t_dept.dept_id
        where t_emp.emp_id = #{empId}
    select>

2.使用association处理映射关系

  • association:处理多对一的映射关系(处理实体类类型的属性)
  • property:设置需要处理映射关系的属性的属性名
  • javaType:设置要处理的属性的类型
    <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>
        <association property="dept" javaType="Dept">
            <id column="dept\_id" property="deptId">id>
            <result column="dept\_name" property="deptName">result>
        association>
    resultMap>

3.分步查询

  • 首先查询员工的信息
    /**
 * 通过分步查询员工的信息
 * @param empId
 * @return
 */
    Emp getEmpAndDeptByStepOne(@Param("empId") Integer empId);

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

        <association property="dept"
 select="com.atguigu.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo"
 column="dept\_id">association>
    resultMap>

    <select id="getEmpAndDeptByStepOne" resultMap="empAndDeptByStepResultMap">
        select * from t_emp where emp_id = #{empId}
    select>
  • 根据员工所对应的部门id查询部门信息
    /**
 * 分步查询第二步:根据员工所对应的id查询部门信息
 * @param deptId
 * @return
 */
    Dept getEmpAndDeptByStepTwo(@Param("deptId") Integer deptId);

    <select id="getEmpAndDeptByStepTwo" resultType="Dept">
        select * from t_dept where depy_id = #{deptId}
    select>
  • 分步查询的优点:可以实现延迟加载,但是必须在核心配置文件中设置全局配置信息:

lazyLoadingEnabled:延迟加载的全局开关,当开启时,所有关联对象都会延迟加载。

aggressiveLazyLoading:当开启时,任何方法的调用都会加载该对象的所有属性。否则,每个属性会按需加载

此时就可以实现按需加载,获取的数据是什么,就会执行相应的sql。此时可通过association和collection中的fetchType属性设置当前的分步查询是否使用延迟加载。


一对多的映射关系

1.collection

    /**
 * 根据部门id查部门中员工的信息
 * @param deptId
 * @return
 */
    Dept getDeptAndEmpByDeptId(@Param("deptId") Integer deptId);
   <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="gender" property="gender">result>
        collection>
    resultMap>

    <select id="getDeptAndEmpByDeptId" resultMap="deptAndEmpResultMap">
        select *
        from t_dept
        LEFT JOIN t_emp
        ON t_dept.dept_id = t_emp.dept_id
        WHERE t_dept.dept_id = #{deptId};
    select>

2.分步查询

  • 查询部门信息
    /**
 * 分步查询部门以及部门中的员工信息第一步
 * @param id
 * @return
 */
    Dept getDeptAndEmpByStepOne(@Param("id") Integer id);
  <resultMap id="deptAnEmpResultMapByStep" type="Dept">
        <id column="dept\_id" property="depyId">id>
        <result column="dept\_name" property="deptName">result>
        <collection property="emps"
 select="com.atguigu.mybatis.mapper.EmpMapper.getDeptAndEmpByStepTwo"
 column="dept\_id">collection>
    resultMap>

    <select id="getDeptAndEmpByStepOne" resultMap="">
        select * from t_dept where dept_id = #{deptId}
    select>
  • 根据部门id查询部门中的员工信息
    /**
 * 分步查询部门以及部门中的员工信息第二步
 * @param dept\_id
 * @return
 */
    List getDeptAndEmpByStepTwo(@Param("dept\_id") Integer dept\_id);
    <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>
        <association property="dept"
 select="com.atguigu.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo"
 column="dept\_id">association>
    resultMap>

    <select id="getDeptAndEmpByStepTwo" resultType="Emp">
        select * from t_emp where dept_id = #{deptId}
    select>

转载请注明:虚坏叔叔 » 自定义映射resultMap

喜欢 (0)

您必须 登录 才能发表评论!