首頁 > 軟體

MyBatis實現多表聯合查詢resultType的返回值

2022-03-10 19:20:46

多表聯合查詢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>

以上為個人經驗,希望能給大家一個參考,也希望大家多多支援it145.com。


IT145.com E-mail:sddin#qq.com