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多表查询内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • java 中sleep() 和 wait() 的对比

    java 中sleep() 和 wait() 的对比

    这篇文章主要介绍了java 中sleep() 和 wait() 的对比的相关资料,需要的朋友可以参考下
    2017-04-04
  • IntelliJ IDEA窗口组件具体操作方法

    IntelliJ IDEA窗口组件具体操作方法

    IDEA刚接触不久,各种常用工具窗口找不到,不小心关掉不知道从哪里打开,今天小编给大家分享这个问题的解决方法,感兴趣的朋友一起看看吧
    2021-09-09
  • 解析SpringCloud简介与微服务架构

    解析SpringCloud简介与微服务架构

    这篇文章主要介绍了SpringCloud简介与微服务架构,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-01-01
  • java如何拷贝复制对象和集合问题

    java如何拷贝复制对象和集合问题

    这篇文章主要介绍了java如何拷贝复制对象和集合问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-09-09
  • JAVA 笔记 ClassLoader.getResourceAsStream() 与 Class.getResourceAsStream()的区别

    JAVA 笔记 ClassLoader.getResourceAsStream() 与 Class.getResourc

    这篇文章主要介绍了JAVA 笔记 ClassLoader.getResourceAsStream() 与 Class.getResourceAsStream()的区别,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-07-07
  • Java中BigDecimal类的add()的使用详解

    Java中BigDecimal类的add()的使用详解

    这篇文章主要介绍了Java中BigDecimal类的add()的使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-01-01
  • springmvc+shiro+maven 实现登录认证与权限授权管理

    springmvc+shiro+maven 实现登录认证与权限授权管理

    Shiro 是一个 Apache 下的一开源项目项目,旨在简化身份验证和授权,下面通过实例代码给大家分享springmvc+shiro+maven 实现登录认证与权限授权管理,感兴趣的朋友一起看看吧
    2017-09-09
  • 浅析Java 9 Optional API 新增方法

    浅析Java 9 Optional API 新增方法

    本文我们介绍了Java 9 Optional Api新增的三个方法。or方法在Optional为空时返回Optional对象。 ifPresentOrElse()在值存在时执行Consumer参数,反之执行另一个参数回调参数。感兴趣的朋友跟随小编一起看看吧
    2019-12-12
  • Java适配器模式定义与用法示例

    Java适配器模式定义与用法示例

    这篇文章主要介绍了Java适配器模式定义与用法,结合具体实例形式分析了java适配器模式的功能、组成、定义、使用方法及适配程度等,需要的朋友可以参考下
    2017-06-06
  • Mybatis逆工程jar包的修改和打包

    Mybatis逆工程jar包的修改和打包

    这篇文章主要介绍了Mybatis逆工程jar包的修改和打包的相关资料,需要的朋友可以参考下
    2016-06-06

最新评论