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

发布时间:2022-03-11 09:09:21 作者:iii
来源:亿速云 阅读:560

这篇“MyBatis如何实现多表联合查询resultType的返回值”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“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_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>

以上就是关于“MyBatis如何实现多表联合查询resultType的返回值”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注亿速云行业资讯频道。

推荐阅读:
  1. Spring4+SpringMVC+MyBatis整合思路
  2. sqlserver多表联合查询和多表分页查询的代码讲解

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

mybatis resulttype

上一篇:C++字符数组、字符数组指针和string类怎么用

下一篇:JavaScript中变量的作用域实例分析

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》