• 【java实战】项目经验_04


    1 sql函数

    1.1 String字符串中查找单个字符串,Locate函数使用

    例如:我想在12;13;14中查找12或13或14。我看谁还在用模糊查询??
    先来看下LOCATE这个函数

    LOCATE(substring,str)返回substring在字符串str中第一次出现的位置
    
    LOCATE(substring,str ,pos)返回substring在字符串str的pos位置之后第一次出现的位置。
    
    如果不包含该str,则返回0
    • 1
    • 2
    • 3
    • 4
    • 5

    所以我们只要包含那个元素,返回的结果就是大于0(> 0),那么我们的SQL可以写为:

    <if test="co.currentUser != null">
      and LOCATE(#{co.currentUser,jdbcType=VARCHAR}, line.audit_contact) &gt; 0)
    </if>
    
    • 1
    • 2
    • 3

    1.2 查询json字段包含某个元素的查询语句, json_contains函数使用

    SELECT *  FROM member
    where json_contains(user,CONCAT('"',#{user},'"'))
    
    • 1
    • 2

    2 JDK8新特性,流式操作

     requestFormList.stream()
                    .collect(Collectors.groupingBy(ProjectRequestFormInstanceBO::getProjectInstanceId,
                            Collectors.collectingAndThen(Collectors.toList(),
                                    list ->
                                            list.stream()
                                                    .anyMatch(item ->
                                                            Objects.equals(RequestFormStatus.STAGING.getKey(), item.getStatus()) ||
                                                                    Objects.equals(RequestFormStatus.CANCEL.getKey(), item.getStatus()) ||
                                                                    Objects.equals(RequestFormStatus.REJECT.getKey(), item.getStatus())
                                                    ))
                    ));
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    Collectors.groupingBy()

    将list按照某个元素分组

    Collectors.collectingAndThen()

    Java 8 流的新类 java.util.stream.Collectors 实现了 java.util.stream.Collector 接口,同时又提供了大量的方法对流 ( stream ) 的元素执行 reduce and map 操作,或者统计操作。

    1.什么是reduce操作?
    聚合操作,中⽂意思是 “减少”
    根据⼀定的规则将Stream中的元素进⾏计算后返回⼀个唯⼀的值

    Collectors.collectingAndThen() 函数应该最像 reduce and map 了,它可接受两个参数,第一个参数用于 reduce操作,而第二参数用于 map操作

    也就是,先把流中的所有元素传递给第一个参数,然后把生成的集合传递给第二个参数来处理。

    stream().anyMatch

    java8 stream接口终端操作 anyMatch,allMatch,noneMatch

    1. anyMatch:判断的条件里,任意一个元素成功,返回true

    2. allMatch:判断条件里的元素,所有的都是,返回true

    3. noneMatch:与allMatch相反,判断条件里的元素,所有的都不是,返回true

    count方法,跟List接口中的 .size() 一样,返回的都是这个集合流的元素的长度,不同的是,流是集合的一个高级工厂,中间操作是工厂里的每一道工序,我们对这个流操作完成后,可以进行元素的数量的和;

    所以上述代码返回的是Map<Long, Boolean>
    可以用来作为权限判断、操作列按钮显示等。

    3. 时间获取、时间格式

    获取当前日期上一季度 开始时间

        /**
         * 获取当前日期上一季度 开始时间
         *
         * @return
         */
        public static Date getStartQuarter(Date date) {
            Calendar startCalendar = Calendar.getInstance();
            startCalendar.setTime(date);
            startCalendar.set(Calendar.MONTH, ((int) startCalendar.get(Calendar.MONTH) / 3 - 1) * 3);
            startCalendar.set(Calendar.DAY_OF_MONTH, 1);
            return getDateMin(startCalendar.getTime());
        }
            public static Date getDateMin(Date date) {
            try {
                SimpleDateFormat datetimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                return datetimeFormat.parse(formatDate(date) + " 00:00:00");
            } catch (Exception ex) {
                log.error(MessageFormat.format("date:{0} ", date), ex);
            }
            return date;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    获取当前日期上一季度 结束时间

        /**
         * 获取当前日期上一季度 结束时间
         *
         * @return
         */
        public static Date getLastQuarter(Date date) {
            Calendar endCalendar = Calendar.getInstance();
            endCalendar.setTime(date);
            endCalendar.set(Calendar.MONTH, ((int) endCalendar.get(Calendar.MONTH) / 3 - 1) * 3 + 2);
            endCalendar.set(Calendar.DAY_OF_MONTH, endCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));
    
            return getDateMax(endCalendar.getTime());
        }
        public static Date getDateMax(Date date) {
            try {
                SimpleDateFormat datetimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                return datetimeFormat.parse(formatDate(date) + " 23:59:59");
            } catch (Exception ex) {
                log.error(MessageFormat.format("date:{0} ", date), ex);
            }
            return date;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
  • 相关阅读:
    react 项目搭建步骤
    Spring中Bean的生命周期
    码农必备,一款超好用Json编辑工具
    【响应式】使用媒体查询实现响应式页面
    每日一题——将一个正整数分解质因数
    记Windows的一个存在了十多年的bug
    说明书SMW200A信号发生器
    数据库索引这么做才有谱
    【运维】dockerfile 中的COPY 会覆盖文件夹吗
    基于自适应Sigmoid型函数的内镜图像增强与空间变颜色再现方法
  • 原文地址:https://blog.csdn.net/qq1437722579/article/details/125422550