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

作者:aloofAnd 时间:2023-03-11 22:25:37 

多表联合查询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>

来源:https://blog.csdn.net/weixin_44238871/article/details/106282507

标签:MyBatis,查询,resultType,返回值
0
投稿

猜你喜欢

  • c#项目将dll打包到exe中的步骤

    2021-06-21 14:44:25
  • C#新手常犯的错误汇总

    2021-10-29 05:05:53
  • Android仿知乎悬浮功能按钮FloatingActionButton效果

    2021-08-04 16:33:47
  • Flutter 日历组件简单实现

    2023-10-21 11:04:35
  • 浅谈Java中replace与replaceAll区别

    2021-07-05 12:56:56
  • Springboot RestTemplate设置超时时间的简单方法

    2022-06-12 23:40:24
  • 完美解决Spring声明式事务不回滚的问题

    2023-07-12 14:38:50
  • Java定时器Timer使用方法详解

    2023-08-25 17:32:20
  • SpringBoot 上传文件判空以及格式检验流程

    2023-01-19 05:07:36
  • springboot整合微信支付sdk过程解析

    2021-12-30 22:47:29
  • C/C++ Qt 基本文件读写的基本使用(2种实现)

    2021-09-17 05:15:36
  • java字符串常用操作方法(查找、截取、分割)

    2023-11-29 03:21:13
  • 如何自定义hibernate validation注解示例代码

    2021-08-31 12:05:51
  • Java synchronized同步方法详解

    2022-06-27 02:46:37
  • Android实现多线程断点下载

    2023-08-11 00:45:28
  • 如何安装系统认证签名过的APK

    2023-07-24 21:35:40
  • android实现倒计时功能代码

    2022-06-28 03:57:31
  • 布隆过滤器(Bloom Filter)的Java实现方法

    2022-09-14 18:06:06
  • MyBatis注解式开发映射语句详解

    2023-06-07 20:31:23
  • Java中SSM+Shiro系统登录验证码的实现方法

    2022-06-09 17:05:14
  • asp之家 软件编程 m.aspxhome.com