java表的分組查詢collection用法

溫暖.jpg

最近在學(xué)習(xí)java做項(xiàng)目中遇到了關(guān)聯(lián)查詢之后要填充modl,但是model是分組的,里面有多個(gè)數(shù)組,如果只是用關(guān)聯(lián)查詢,對于這種會出現(xiàn)重復(fù)查詢,所以這里要用到Mybatis的collection來處理

1、首先介紹一下我們的model屬性(可以看到有2個(gè)指定對象的數(shù)組)

    private Long id;
    private String customerNo;
    private Long listId;
    private String name;
    private String identifyType;
    private String identifyNo;   
    private String phoneA;
    private String phoneB;
    private String phoneC;
    private String telephone;
    private String email;
    private String idcardAdress;
    private String householdAddress;
    private String liveAddress;
    private String workAddress;
    private Date gmtCreate;
    private Date gmtModified;
    private Long createBy;
    private Long lastUpdateBy;
    private String customerType;
    private List<CmCustomerContact> contactList;
    private List<CmCustomerBankcard> bankcardList;

2、在mapper里面對接輸出model

<resultMap id="BaseResultMap1" type="com.paic.gamma_cm.model.dto.CmCustomerForExcel">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="customer_no" jdbcType="VARCHAR" property="customerNo" />
    <result column="list_id" jdbcType="BIGINT" property="listId" />
    <result column="name" jdbcType="VARCHAR" property="name" />
    <result column="identify_type" jdbcType="VARCHAR" property="identifyType" />
    <result column="identify_no" jdbcType="VARCHAR" property="identifyNo" />
    <result column="phone_a" jdbcType="VARCHAR" property="phoneA" />
    <result column="phone_b" jdbcType="VARCHAR" property="phoneB" />
    <result column="phone_c" jdbcType="VARCHAR" property="phoneC" />
    <result column="telephone" jdbcType="VARCHAR" property="telephone" />
    <result column="email" jdbcType="VARCHAR" property="email" />
    <result column="idcard_adress" jdbcType="VARCHAR" property="idcardAdress" />
    <result column="household_address" jdbcType="VARCHAR" property="householdAddress" />
    <result column="live_address" jdbcType="VARCHAR" property="liveAddress" />
    <result column="work_address" jdbcType="VARCHAR" property="workAddress" />
    <result column="gmt_create" jdbcType="TIMESTAMP" property="gmtCreate" />
    <result column="gmt_modified" jdbcType="TIMESTAMP" property="gmtModified" />
    <result column="create_by" jdbcType="BIGINT" property="createBy" />
    <result column="last_update_by" jdbcType="BIGINT" property="lastUpdateBy" />
    <result column="customer_type" jdbcType="VARCHAR" property="customerType" />
    <collection property="contactList" resultMap="CMContactResultMap" />
    <collection property="bankcardList" resultMap="CMBankResultMap" />
    </resultMap>
<resultMap id="CMContactResultMap" type="com.paic.gamma_cm.model.dto.CmCustomerContact">
            <id column="contact_id" property="id" jdbcType="BIGINT"/>
            <result column="customer_id" property="customerId" jdbcType="BIGINT"/>
            <result column="relation_type" property="relationType" jdbcType="VARCHAR"/>
            <result column="relation_desc" property="relationDesc" jdbcType="VARCHAR"/>
            <result column="contact_identify_type" property="identifyType" jdbcType="VARCHAR"/>
            <result column="contact_identify_no" property="identifyNo" jdbcType="VARCHAR"/>
            <result column="contact_name" property="contactName" jdbcType="VARCHAR"/>
            <result column="contact_phone" property="contactPhone" jdbcType="VARCHAR"/>
            <result column="gmt_create" property="gmtCreate" jdbcType="TIMESTAMP"/>
            <result column="gmt_modified" property="gmtModified" jdbcType="TIMESTAMP"/>
  </resultMap>
  <resultMap id="CMBankResultMap" type="com.paic.gamma_cm.model.dto.CmCustomerBankcard">
            <id column="bankcard_id" property="id" jdbcType="BIGINT"/>
            <result column="customer_id" property="customerId" jdbcType="BIGINT"/>
            <result column="card_bank" property="cardBank" jdbcType="VARCHAR"/>
            <result column="account_type" property="accountType" jdbcType="VARCHAR"/>
            <result column="bank_card_no" property="bankCardNo" jdbcType="VARCHAR"/>
            <result column="gmt_create" property="gmtCreate" jdbcType="TIMESTAMP"/>
            <result column="gmt_modified" property="gmtModified" jdbcType="TIMESTAMP"/>
  </resultMap>

3、在mapper的xml里寫關(guān)聯(lián)查詢

<select id="selectCustomerAllList" resultMap="BaseResultMap1">
  SELECT
customer . id,
customer .customer_no ,
customer .list_id ,
customer . name ,
customer .identify_type,
customer .identify_no ,
customer .phone_a ,
customer .phone_b ,
customer .phone_c ,
customer .telephone,
customer .email,
customer .idcard_adress,
customer .household_address,
customer .live_address,
customer .work_address,
customer .customer_type,
contact . id AS contact_id,
contact .relation_type,
contact .identify_type AS contact_identify_type,
contact .identify_no AS contact_identify_no,
contact .contact_name,
contact .contact_phone,
bankcard. id AS bankcard_id,
bankcard.card_bank,
bankcard.account_type,
bankcard.bank_card_no
FROM
cm_customer customer
LEFT JOIN cm_customer_contact contact ON customer.id = contact.customer_id
LEFT JOIN cm_customer_bankcard bankcard ON customer.id = bankcard.customer_id 
  </select>

簡單3步就完成了分組查詢,其中要注意BaseResultMap1中表屬性對應(yīng)的要與自己創(chuàng)建的匿名表列名對應(yīng)(區(qū)分相同字段在不同表中的情況)
詳細(xì)的collection用法和Association用法可參考https://blog.csdn.net/qq_38157516/article/details/79712721

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容