mybatis example group by count 分组求和 - java分组求和

一、简单说明

本以为mybatis的example可以搞定group by,后面看到说不行
于是曲线救国,直接查出一个list,然后再用java对数据进行操作
不过话说回来,这样写是比写sql麻烦一点,但是个人感觉这样效率会高一点(未做对比测试)

二、需求说明

  1. 查出数据库中,过去30天,每个用户(merId)每天的发送条数
  2. 改进版,在1的基础上,加上所有日期的统计信息,对应日期没记录也加上

三、所用技术概要

  1. jodatime:方便对时间进行操作,转换等
  2. stream:对java进行操作,很是方便,此处只是stream用法的冰山一角

四、代码实现

省略了生成的实体类、mapper、example等

需求1实现

public Map<Integer,Integer> coountDays(String merId){
    SmsCustomerSendDetailExample example = new SmsCustomerSendDetailExample();

    // 查询条件
    DateTime dt = new DateTime();
    // 当天00:00:00往前推30天
    DateTime days = dt.withTimeAtStartOfDay().minusDays(30);
    Criteria criteria = example.createCriteria();
    criteria.andCustomerIdEqualTo(merId);
    criteria.andCreateTimeBetween(days.toDate(),dt.toDate());

    List<SmsCustomerSendDetail> details = sendDetailMapper.selectByExample(example);

    // 对时间进行更改,清洗为yyyyMMdd(后面按天分组用)
    for (SmsCustomerSendDetail detail : details) {
      DateTime dateTime = new DateTime(detail.getCreateTime().getTime()).withTimeAtStartOfDay();
      detail.setCreateTime(dateTime.toDate());
    }

    //根据日期分组
    Map<Date, List<SmsCustomerSendDetail>> dateListMap = details.stream()
            .collect(Collectors.groupingBy(SmsCustomerSendDetail::getCreateTime));

    // 遍历map,求出当天记录的条数
    HashMap<Integer, Integer> resMap = new HashMap<>(128);
    for (Entry<Date, List<SmsCustomerSendDetail>> detailEntry:dateListMap.entrySet()){
      String day = new DateTime(detailEntry.getKey().getTime()).toString("yyyyMMdd");
      int daySize = detailEntry.getValue().size();
      resMap.put(Integer.valueOf(day),daySize);
    }
    // 排序
    Stream<Entry<Integer, Integer>> st = resMap.entrySet().stream();
    Map<Integer, Integer> result = new LinkedHashMap<>(32);
    st.sorted(Comparator.comparing(e -> e.getKey())).forEach(e -> result.put(e.getKey(), e.getValue()));
    return result;
  }
结果:{20180306:44,20180307:14,20180308:9}

需求2实现

  public Map<Integer,Integer> coountDays(String merId){
    SmsCustomerSendDetailExample example = new SmsCustomerSendDetailExample();
    // 查询条件
    DateTime dt = new DateTime();
    // 当天00:00:00往前推30天
    DateTime days = dt.withTimeAtStartOfDay().minusDays(30);
    Criteria criteria = example.createCriteria();
    criteria.andCustomerIdEqualTo(merId);
    criteria.andCreateTimeBetween(days.toDate(),dt.toDate());

    List<SmsCustomerSendDetail> details = sendDetailMapper.selectByExample(example);

    // 对时间进行更改,清洗为yyyyMMdd(后面按天分组用)
    for (SmsCustomerSendDetail detail : details) {
      DateTime dateTime = new DateTime(detail.getCreateTime().getTime()).withTimeAtStartOfDay();
      detail.setCreateTime(dateTime.toDate());
    }

    //根据日期分组
    Map<Date, List<SmsCustomerSendDetail>> dateListMap = details.stream()
            .collect(Collectors.groupingBy(SmsCustomerSendDetail::getCreateTime));

    // 遍历map,求出当天记录的条数
    HashMap<Integer, Integer> resMap = new HashMap<>(128);
    for (Entry<Date, List<SmsCustomerSendDetail>> detailEntry:dateListMap.entrySet()){
      String day = new DateTime(detailEntry.getKey().getTime()).toString("yyyyMMdd");
      int daySize = detailEntry.getValue().size();
      resMap.put(Integer.valueOf(day),daySize);
    }
    // 添加过去30天所有天为key,从resMap中取出有数据的,否则为0
    Map<Integer, Integer> result = new LinkedHashMap<>(32);
    for (int i = 0; i < 30; i++) {
      String yyyyMMdd = days.plusDays(i).toString("yyyyMMdd");
      Integer day = Integer.valueOf(yyyyMMdd);
      int value = 0;
      if (resMap.containsKey(day)){
        value = resMap.get(day);
      }
      result.put(Integer.valueOf(yyyyMMdd),value);
    }
    return result;
  }

结果:{20180213:0,20180214:0,20180215:0,20180216:0,20180217:0,20180218:0,20180219:0,20180220:0,20180221:0,20180222:0,20180223:0,20180224:0,20180225:0,20180226:0,20180227:0,20180228:0,20180301:0,20180302:0,20180303:0,20180304:0,20180305:0,20180306:44,20180307:14,20180308:9,20180309:0,20180310:0,20180311:0,20180312:0,20180313:0,20180314:0}
    原文作者:hisenyuan
    原文地址: https://www.jianshu.com/p/8bc735586910
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞