美文网首页程序员
java表的分组查询collection用法

java表的分组查询collection用法

作者: Iam光星人 | 来源:发表于2018-12-24 14:22 被阅读27次
温暖.jpg

最近在学习java做项目中遇到了关联查询之后要填充modl,但是model是分组的,里面有多个数组,如果只是用关联查询,对于这种会出现重复查询,所以这里要用到Mybatis的collection来处理

1、首先介绍一下我们的model属性(可以看到有2个指定对象的数组)

    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里写关联查询

<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中表属性对应的要与自己创建的匿名表列名对应(区分相同字段在不同表中的情况)
详细的collection用法和Association用法可参考https://blog.csdn.net/qq_38157516/article/details/79712721

相关文章

  • java表的分组查询collection用法

    最近在学习java做项目中遇到了关联查询之后要填充modl,但是model是分组的,里面有多个数组,如果只是用关联...

  • mysql分组查询

    进阶5:分组查询 /*语法: select 查询列表from 表【where 筛选条件】group by 分组的字...

  • JDBC中常用的SQL总结02之SELECT语句用法详解

    0. README 本篇文章主要总结SELECT语句的具体用法, 包含条件查询, 顺序查询, 分组查询, 结合聚集...

  • MongoDB联表查询

    创建表user、order 查询 联表查询 拆分orders 分组求和并返回字段数据 查询用户的订单信息(订单id...

  • Django 中的聚合查询与分组查询

    聚合 常用的聚合函数 Avg, Max, Min 实例1 实例2 分组 emp表模型 总结 :跨表分组查询本质就...

  • MySQL 13-16章实操

    第十三章 分组数据 创建分组 过滤分组 分组和排序 第十四章 使用子查询 作为计算字段使用子查询 第十五章 联结表...

  • 数据分组

    group by关键字 作用:对查询结果进行分组处理 用法: 1.分组之后,不能将除分组字段之外的字段放在sele...

  • mysql语句

    分组查询: 分组查询统计并且降序排序 2.更改cash_user表中的reg_channel列值为'kaniu_m...

  • MyBatis多对多映射查询 Day12 2018-11-30

    7 一对多查询 元素的解释: column 表示当前查询数据库表的列名DEPARTMENT...

  • 延迟加载

    resultMap中的association、collection支持延迟加载的功能 延迟加载:先从单表查询,需要...

网友评论

    本文标题:java表的分组查询collection用法

    本文链接:https://www.haomeiwen.com/subject/stqzkqtx.html