MyBatis实现多表联合查询resultType的返回值

 更新时间:2022年03月10日 17:17:23   作者:aloofAnd  
这篇文章主要介绍了MyBatis多表联合查询resultType的返回值,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

多表联合查询resultType的返回值

一般数据按参数类型返回

<select id="queryCarIdList" resultType="long">
        select id from t_car_car
</select>
  <select id="queryDept" resultType="string">
        SELECT deptname FROM t_car_run where deptid = #{deptid} GROUP BY deptname
    </select>

根据某字段查询

返回的类型是实体类,因为查询结果数据均为实体类中字段的数据

<select id="queryNumber" resultType="io.renren.modules.generator.entity.TCarRunEntity">
        select number from t_car_car where id = #{carid}
</select>

查询结果为多条记录,存放在list中返回

返回的类型是实体类,因为查询结果数据均为实体类中字段的数据

<select id="queryCar" resultType="io.renren.modules.generator.entity.TCarCarEntity">
        select * from t_car_car
</select>

多表联合查询

  • t_car_car
  • t_car_driver
  • t_car_cardriver

t_car_cardriver存放的两个字段分别是t_car_car和t_car_driver的主键id

解决方案

1.resultType的返回类型是java.util.Map

返回得到的是List中存放的所有数据

<select id="queryDriver" resultType="java.util.Map">
        select driverid from t_car_cardriver where carid = #{id}
</select>

2.新建一个实体类

里面存放的是查询结果里需要的字段名

// TCarCarDriver
private Long carid;
private Long driverid;

返回类型为该实体类

<select id="queryDriver" resultType="TCarCarDriver">
        select driverid from t_car_cardriver where carid = #{id}
</select>

多表联查,返回结果嵌套list

多层集合嵌套返回结果用resultMap,collection中再次使用resultMap

<resultMap id="chainVo" type="com.suncnpap.intelligentqa.vo.ChainVo">
    <id column="cid" property="id"/>
    <result column="access_key" property="accessKey"/>
    <result column="secret_key" property="secretKey"/>
    <result column="outer_chain_name" property="outerChainName"/>
    <result column="outer_chain_document" property="outerChainDocument"/>
    <collection property="intentionVos" ofType="com.suncnpap.intelligentqa.vo.ChainIntentionVo"
                resultMap="intentionVos"/>
</resultMap>
 
<resultMap id="intentionVos" type="com.suncnpap.intelligentqa.vo.ChainIntentionVo">
    <id column="iid" property="id"/>
    <result column="intention_name" property="intentionName"/>
    <collection property="questionVoList" ofType="com.suncnpap.intelligentqa.vo.MultiQuestionVo">
        <id column="qid" property="id"/>
        <result column="question" property="question"/>
    </collection>
    <collection property="wordVos" ofType="com.suncnpap.intelligentqa.vo.ChainIntentionWordVo">
        <id column="wid" property="id"/>
        <result column="word_slot" property="wordSlot"/>
        <result column="word_slot_miss_question" property="wordSlotMissQuestion"/>
        <result column="entity_type_ids" property="entityTypeIds"/>
    </collection>
</resultMap>
 
<select id="detail" resultMap="chainVo">
    select tc.id   as tid,
           tci.id  as iid,
           tciw.id as wid,
           tmq.id  as qid,
           access_key,
           secret_key,
           outer_chain_name,
           outer_chain_document,
           intention_name,
           question,
           word_slot,
           word_slot_miss_question,
           entity_type_ids
    from t_chain tc
             left join t_chain_intention tci on tc.id = tci.chain_id
             left join t_chain_intention_word tciw on tci.id = tciw.intention_id
             left join t_multi_question tmq on tci.id = tmq.parent_id
    where tc.id = #{id}
      and tc.deleted = 0
</select>

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Java中的线程安全问题详细解析

    Java中的线程安全问题详细解析

    这篇文章主要介绍了Java中的线程安全问题详细解析,线程安全是如果有多个线程在同时运行,而这些线程可能会同时运行这段代码,程序每次运行结果和单线程运行的结果是一样的,而且其他的变量的值也和预期的是一样的,此时我们就称之为是线程安全的,需要的朋友可以参考下
    2023-11-11
  • Spring boot整合jsp和tiles模板示例

    Spring boot整合jsp和tiles模板示例

    这篇文章主要介绍了Spring boot整合jsp模板和tiles模板的示例演示过程,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-03-03
  • java实现识别二维码图片功能

    java实现识别二维码图片功能

    这篇文章主要为大家详细介绍了java实现识别二维码图片功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-04-04
  • Spring AOP事务管理的示例详解

    Spring AOP事务管理的示例详解

    这篇文章将通过转账案例为大家详细介绍一下Spring AOP是如何进行事务管理的,文中的示例代码讲解详细,感兴趣的小伙伴可以了解一下
    2022-06-06
  • Mybatis-plus解决兼容oracle批量插入的示例详解

    Mybatis-plus解决兼容oracle批量插入的示例详解

    Mybatis-Plus 是一个 MyBatis 的增强工具,提供无侵入、损耗小的 CRUD 操作,本文给大家介绍了Mybatis-plus解决兼容oracle批量插入,文中通过大家介绍的非常详细,需要的朋友可以参考下
    2024-11-11
  • 教你使用idea搭建ssm详细教程(Spring+Spring Mvc+Mybatis)

    教你使用idea搭建ssm详细教程(Spring+Spring Mvc+Mybatis)

    今天教大家使用idea搭建ssm详细教程(Spring+Spring Mvc+Mybatis),文中有非常详细的图文介绍及代码示例,对正在学习使用idea的小伙伴很有帮助,需要的朋友可以参考下
    2021-05-05
  • Java遍历Map对象集合的六种方式代码示例

    Java遍历Map对象集合的六种方式代码示例

    Java中的Map是一种键值对映射的数据结构,它提供了一些常用的方法用于获取、添加、删除和修改元素,下面这篇文章主要给大家介绍了关于Java遍历Map对象集合的六种方式,需要的朋友可以参考下
    2024-02-02
  • SpringBoot使用AOP实现统一角色权限校验

    SpringBoot使用AOP实现统一角色权限校验

    这篇文章主要介绍了SpringBoot如何使用AOP实现 统一角色权限校验,文中有详细的代码示例讲解和操作流程,具有一定的参考价值,需要的朋友可以参考下
    2023-07-07
  • Spring Boot虚拟线程Webflux在JWT验证和MySQL查询性能比较

    Spring Boot虚拟线程Webflux在JWT验证和MySQL查询性能比较

    这篇文章主要为大家介绍了Spring Boot虚拟线程与Webflux在JWT验证和MySQL查询上的性能比较,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-09-09
  • SpringBoot整合Netty的流程步骤

    SpringBoot整合Netty的流程步骤

    Netty是一个基于Java的开源网络应用框架,它提供了高性能、异步事件驱动的网络编程能力,Netty旨在帮助开发者构建高性能、高可靠性的网络应用程序,本文给大家详细介绍了SpringBoot整合Netty的流程步骤,需要的朋友可以参考下
    2023-09-09

最新评论