MyBatis中ResultMap与多表查询的处理方法

 更新时间:2023年09月13日 12:12:44   作者:清河__  
这篇文章主要介绍了MyBatis中ResultMap与多表查询的处理方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

ResultMap与多表查询的处理

当字段名与实类名不一致时

使用别名进行处理

字段名:emp_name

实体类名:empName

映射文件中写法:

    <select id="getAllEmp" resultType="Emp">
        select eid, emp_name empName, age, sex, email, did from t_emp
    </select>

使用全局配置将下划线命名映射为驼峰

在mybatis-config.xml文件的properties标签和typeAlias标签之间添加settings标签如下,可以将下划线式命名映射为驼峰:

    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>

使用resultMap创建自定义的映射关系

在mapper.xml文件中进行定义:

定义resultMap:

    <!-- 就算是自定义映射关系,也需要相对应的实体类 -->
    <resultMap id="empResultMap" type="Emp">
        <!-- id用来声明主键,property用来表示实体类中的属性名、column用来标识数据库表中的字段名 -->
        <id property="eid" column="eid"></id>
        <!-- result用来声明普通字段 -->
        <result property="empName" column="emp_name"></result>
        <result property="age" column="age"></result>
        <result property="sex" column="sex"></result>
        <result property="email" column="email"></result>
    </resultMap>

传入resultMap的id以用来使用自定义映射

    <select id="getAllEmp" resultMap="empResultMap">
        select * from t_emp
    </select>

注意:resultMap一般用于处理多对一、一对多的关系

一对多情况的处理

对于多对一的情况(一个员工只会在一个部门中)

只需要在员工实体类中添加一个部门属性:

    private Integer eid;
    private String empName;
    private Integer age;
    private String sex;
    private String email;
    private Integer did;
    private Dept dept;

通过级联属性赋值resultMap解决多对一问题

在mapper.xml文件中创建resultMap:

    <resultMap id="empAndDeptResultMap" type="Emp">
        <id property="eid" column="eid"></id>
        <result property="empName" column="emp_name"></result>
        <result property="age" column="age"></result>
        <result property="sex" column="sex"></result>
        <result property="email" column="email"></result>
        <result property="dept.did" column="did"></result>
        <result property="dept.deptName" column="dept_name"></result>
    </resultMap>

再将这个传入语句进行调用

<!--    Emp getEmpAndDept(@Param("eid") Integer eid);-->
    <select id="getEmpAndDept" resultMap="empAndDeptResultMap">
        select *
        from t_emp left join t_dept on t_emp.did = t_dept.did
        where eid=#{eid}
    </select>

级联属性赋值的方式一般不使用

使用association解决多对一问题

在mapper.xml文件中创建resultMap:

    <resultMap id="empAndDeptResultMapAsscoiation" type="Emp">
        <id property="eid" column="eid"></id>
        <result property="empName" column="emp_name"></result>
        <result property="age" column="age"></result>
        <result property="sex" column="sex"></result>
        <result property="email" column="email"></result>
        <association property="dept" javaType="Dept">
            <id property="did" column="did"></id>
            <result property="deptName" column="dept_name"></result>
        </association>
    </resultMap>

再将之传入即可

通过分步查询解决多对一问题

在mapper.xml建立如下:

<!--    注意在分步查询的过程中,association中的column代表传入的条件-->
    <resultMap id="empAndDeptByStepResultMap" type="emp">
        <id property="eid" column="eid"></id>
        <result property="empName" column="emp_name"></result>
        <result property="age" column="age"></result>
        <result property="sex" column="sex"></result>
        <result property="email" column="email"></result>
        <association property="dept"
                     select="com.qinghe.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo"
                     column="did"></association>
    </resultMap>

Dept的mapper如下:

<!--    Dept getEmpAndDeptByStepTwo(@Param("did") Integer did);-->
    <select id="getEmpAndDeptByStepTwo" resultType="Dept">
        select * from t_dept where did = #{did}
    </select>

调用:

    <select id="getEmpAndDeptByStepOne" resultMap="empAndDeptByStepResultMap">
        select * from t_emp where eid = #{eid}
    </select>

分布查询的优点:延迟加载

当mybatis的查询语句由多步查询构成时,我们可以开启mybatis的懒加载,此时若我们只需要第一步的某个属性,mybatis就不会去调用第二步的sql语句,这样在多种场景下最大限度的保证了性能。

在全局配置(mybatis-config.xml)中添加如下配置

<!--    全局配置:自动将下划线转为驼峰,懒加载-->
    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
        <setting name="lazyLoadingEnabled" value="true"></setting>
    </settings>

同时,我们也可以在association中使用fetchType标签来使延迟加载变得可控,eager代表立即加载、lazy代表延迟加载

<!--    注意在分步查询的过程中,association中的column代表传入的条件-->
    <resultMap id="empAndDeptByStepResultMap" type="emp">
        <id property="eid" column="eid"></id>
        <result property="empName" column="emp_name"></result>
        <result property="age" column="age"></result>
        <result property="sex" column="sex"></result>
        <result property="email" column="email"></result>
        <association property="dept"
                     select="com.qinghe.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo"
                     column="did"
                     fetchType="eager">
        </association>
    </resultMap>

多对一情况的处理

在dept实体类中添加多的的那个List:

    private List<Emp> empList;

使用collection标签进行一口气的处理

在deptMapper下进行如下操作:

    <resultMap id="deptAndEmpResultMap" type="Dept">
        <id property="did" column="did"></id>
        <result property="deptName" column="dept_name"></result>
<!--        这里的ofType代表传入的List的泛型-->
        <collection property="empList" ofType="Emp">
            <id property="eid" column="eid"></id>
            <result property="empName" column="emp_name"></result>
            <result property="age" column="age"></result>
            <result property="sex" column="sex"></result>
            <result property="email" column="email"></result>
        </collection>
    </resultMap>
    <select id="getDeptAndEmp" resultMap="deptAndEmpResultMap">
        select *
        from t_emp left join t_dept on t_emp.did = t_dept.did
        where t_dept.did=#{did}
    </select>

通过分步查询进行处理

在DeptMapper中建立第一步的接口:

    /**
     * 通过分步查询部门以及部门中的员工信息
     */
    Dept getDeptAndEmpByStepOne(@Param("did") Integer did);

在EmpMapper中建立第二步的接口:

    /**
     * 分步查询第二步,根据did查询员工信息
     */
    List<Emp> getDeptAndEmpByStepTwo(@Param("did") Integer did);

在EmpMapper.xml文件中定义xml:

<!--    List<Emp> getDeptAndEmpByStepTwo(@Param("did") Integer did);-->
    <select id="getDeptAndEmpByStepTwo" resultType="Emp">
        select * from t_emp
    </select>

在DeptMapper.xml文件中定义xml:

    <resultMap id="deptAndEmpByStepResultMap" type="Dept">
        <id property="did" column="did"></id>
        <result property="deptName" column="dept_name"></result>
        <collection property="empList"
                    select="com.qinghe.mybatis.mapper.EmpMapper.getDeptAndEmpByStepTwo"
                    column="did"
        ></collection>
    </resultMap>
    <select id="getDeptAndEmpByStepOne" resultMap="deptAndEmpByStepResultMap">
        select * from t_dept where did = #{did}
    </select>

到此这篇关于MyBatis中ResultMap与多表查询的处理的文章就介绍到这了,更多相关ResultMap多表查询内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Logback的使用及如何配置

    Logback的使用及如何配置

    这篇文章主要介绍了Logback的使用及如何配置,帮助大家更好的理解和学习使用Logback,感兴趣的朋友可以了解下
    2021-03-03
  • Java ForkJoinPool线程池的使用之并行计算数组求和实例

    Java ForkJoinPool线程池的使用之并行计算数组求和实例

    这篇文章主要介绍了Java ForkJoinPool线程池的使用之并行计算数组求和实例,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2025-05-05
  • Spring boot 跳转到jsp页面的实现方法

    Spring boot 跳转到jsp页面的实现方法

    本篇文章主要介绍了Spring boot 跳转到jsp页面的实现方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-04-04
  • java.sql.SQLRecoverableException关闭的连接异常问题及解决办法

    java.sql.SQLRecoverableException关闭的连接异常问题及解决办法

    当数据库连接池中的连接被创建而长时间不使用的情况下,该连接会自动回收并失效,就导致客户端程序报“ java.sql.SQLException: Io 异常: Connection reset” 或“java.sql.SQLException 关闭的连接”异常问题,下面给大家分享解决方案,一起看看吧
    2024-03-03
  • 详解Java注解的实现与使用方法

    详解Java注解的实现与使用方法

    这篇文章主要介绍了详解Java注解的实现与使用方法的相关资料,希望通过本文大家能够理解掌握Java注解的知识,需要的朋友可以参考下
    2017-09-09
  • Javassist用法详解

    Javassist用法详解

    这篇文章主要介绍了Javassist用法的相关资料,帮助大家更好的理解和学习使用Java,感兴趣的朋友可以了解下
    2021-02-02
  • Spring Boot中使用Spring-data-jpa的配置方法详解

    Spring Boot中使用Spring-data-jpa的配置方法详解

    今天小编就为大家分享一篇关于Spring Boot中使用Spring-data-jpa的配置方法详解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-03-03
  • Spring Boot2.X国际化文件编写配置

    Spring Boot2.X国际化文件编写配置

    这篇文章主要介绍了Spring Boot2.X国际化文件编写配置,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-02-02
  • jdk8的datetime时间函数使用示例

    jdk8的datetime时间函数使用示例

    这篇文章主要介绍了jdk8的datetime时间函数使用示例,需要的朋友可以参考下
    2014-03-03
  • Java 通配符详解:?、? extends、? super 一篇搞懂

    Java 通配符详解:?、? extends、? super 一篇搞懂

    本文深入解析Java泛型中的通配符(Wildcard)机制,重点讲解无界通配符(?)、上界通配符(? extends T)和下界通配符(? super T)的用法与区别,感兴趣的可以了解一下
    2025-10-10

最新评论