• Maven 项目中常用的工具包


    1 时间相关

    1.1 依赖包

    1. <dependency>
    2. <groupId>joda-time</groupId>
    3. <artifactId>joda-time</artifactId>
    4. <version>2.9.4</version>
    5. </dependency>

    1.2 使用案例

    1. Period p = new Period(new DateTime('开始时间戳'), new DateTime('结束时间戳'), PeriodType.yearDayTime());
    2. System.out.println(p.getYears() + "年" + p.getDays() + "天" + p.getHours() + "小时" + p.getMinutes() + "分钟");

    2 fastjson

    依赖包

    1. <dependency>
    2. <groupId>com.alibaba</groupId>
    3. <artifactId>fastjson</artifactId>
    4. <version>1.2.75</version>
    5. </dependency>

    使用:

    1. JSONObject jsonObject = JSON.parseObject("");
    2. JSONArray objects = JSON.parseArray("");
    3. User user = JSON.parseObject("", User.class);
    4. ...等等

    3.org.apache.commons

    依赖:

    1. <dependency>
    2. <groupId>org.apache.commons</groupId>
    3. <artifactId>commons-lang3</artifactId>
    4. <version>3.11</version>
    5. </dependency>

    使用:

    1.空字符串检查

    使用函数: StringUtils.isBlank(testString)
    函数介绍: 当testString为空,长度为零或者仅由空白字符(whitespace)组成时,返回True;否则返回False

    函数StringUtils.isNotBlank(testString)的功能与StringUtils.isBlank(testString)相反.

    2.清除空白字符
    使用函数: StringUtils.trimToNull(testString)
    函数介绍:清除掉testString首尾的空白字符,如果仅testString全由空白字符

    3.取得字符串的缩写
    使用函数: StringUtils.abbreviate(testString,width)和StringUtils.abbreviate(testString,offset,width)
    函数介绍:在给定的width内取得testString的缩写,当testString的长度小于width则返回原字符串.

    4.劈分字符串
    使用函数: StringUtils.split(testString,splitChars,arrayLength)
    函数介绍:splitChars中可以包含一系列的字符串来劈分testString,并可以设定得
    到数组的长度.注意设定长度arrayLength和劈分字符串间有抵触关系,建议一般情况下
    不要设定长度.

    5.查找嵌套字符串
    使用函数:StringUtils.substringBetween(testString,header,tail)
    函数介绍:在testString中取得header和tail之间的字符串。不存在则返回空

    6.去除尾部换行符
    使用函数:StringUtils.chomp(testString)
    函数介绍:去除testString尾部的换行符

    7.重复字符串
    使用函数:StringUtils.repeat(repeatString,count)
    函数介绍:得到将repeatString重复count次后的字符串

    8.颠倒字符串
    使用函数:StringUtils.reverse(testString)
    函数介绍:得到testString中字符颠倒后的字符串

    9.判断字符串内容的类型

    StringUtils.isNumeric( testString ) :如果testString全由数字组成返回True

    StringUtils.isAlpha( testString ) :如果testString全由字母组成返回TrueStringUtils.isAlphanumeric( testString ) :如果testString全由数字或数字组成返回True

    11.部分截取字符串
    使用函数:
    StringUtils.substringBetween(testString,fromString,toString ):取得两字符
    之间的字符串
    StringUtils.substringAfter( ):取得指定字符串后的字符串
    StringUtils.substringBefore( ):取得指定字符串之前的字符串
    StringUtils.substringBeforeLast( ):取得最后一个指定字符串之前的字符串
    StringUtils.substringAfterLast( ):取得最后一个指定字符串之后的字符串

    commons-io

    commons-io 是Apache封装好的库,包含使用程序类、流实现、文件过滤、文件比较器、endian转换类等。代码中经常用到io流,自己写代码比较繁琐, 推荐使用commons-io工具类。

    依赖:

    1. <dependency>
    2. <groupId>commons-io</groupId>
    3. <artifactId>commons-io</artifactId>
    4. <version>2.6</version>
    5. </dependency>

    IOUtils.copy(InputStream input, OutputStream output) // 此方法有多个重载方法,满足不同的输入输出流

    IOUtils.copy(InputStream input, OutputStream output, int bufferSize)

    // 这个方法适合拷贝较大的数据流,比如2G以上

    IOUtils.copyLarge(Reader input, Writer output) // 默认会用1024*4的buffer来读取

    IOUtils.copyLarge(Reader input, Writer output, char[] buffer)

     

    // 复制文件夹

    FileUtils.copyDirectory(File srcDir, File destDir) // 复制文件夹(文件夹里面的文件内容也会复制)

    FileUtils.copyDirectory(File srcDir, File destDir, FileFilter filter) // 复制文件夹,带有文件过滤功能

    commons-io工具包的基本使用_淋雨一直走啊的博客-CSDN博客_commons-io

    4.cn.hutool

    1. <dependency>
    2. <groupId>cn.hutool</groupId>
    3. <artifactId>hutool-all</artifactId>
    4. <version>5.5.2</version>
    5. </dependency>

    使用:

    1. HttpUtil.get();
    2. ...等等

    一个 Java 基础工具类,对文件、流、加密解密、转码、正则、线程、XML等JDK方法进行封装,组成各种Util工具类,同时提供以下组件:

    模块    介绍
    hutool-aop    JDK动态代理封装,提供非IOC下的切面支持
    hutool-bloomFilter    布隆过滤,提供一些Hash算法的布隆过滤
    hutool-cache    简单缓存实现
    hutool-core    核心,包括Bean操作、日期、各种Util等
    hutool-cron    定时任务模块,提供类Crontab表达式的定时任务
    hutool-crypto    加密解密模块,提供对称、非对称和摘要算法封装
    hutool-db    JDBC封装后的数据操作,基于ActiveRecord思想
    hutool-dfa    基于DFA模型的多关键字查找
    hutool-extra    扩展模块,对第三方封装(模板引擎、邮件、Servlet、二维码、Emoji、FTP、分词等)
    hutool-http    基于HttpUrlConnection的Http客户端封装
    hutool-log    自动识别日志实现的日志门面
    hutool-script    脚本执行封装,例如Javascript
    hutool-setting    功能更强大的Setting配置文件和Properties封装
    hutool-system    系统参数调用封装(JVM信息等)
    hutool-json    JSON实现
    hutool-captcha    图片验证码实现
    hutool-poi    针对POI中Excel和Word的封装
    hutool-socket    基于Java的NIO和AIO的Socket封装
    可以根据需求对每个模块单独引入,也可以通过引入hutool-all方式引入所有模块。

    5、google.guava

    1. <dependency>
    2. <groupId>com.google.guava</groupId>
    3. <artifactId>guava</artifactId>
    4. <version>${google.guava.version}</version>
    5. </dependency>

    使用

    1. // 异常输出
    2. Throwables.getStackTraceAsString(e)

    MySql等数据库依赖

    1. <dependency>
    2. <groupId>mysql</groupId>
    3. <artifactId>mysql-connector-java</artifactId>
    4. <!-- <version>8.0.21</version> 版本Spring-Boot-Parent中已带 -->
    5. </dependency>
    6. <dependency>
    7. <groupId>com.alibaba</groupId>
    8. <artifactId>druid</artifactId>
    9. <version>1.2.11</version>
    10. </dependency>
    11. <!-- mybaytis plus -->
    12. <dependency>
    13. <groupId>com.baomidou</groupId>
    14. <artifactId>mybatis-plus-boot-starter</artifactId>
    15. <version>3.4.1</version>
    16. </dependency>

    lombok

    1. <dependency>
    2. <groupId>org.projectlombok</groupId>
    3. <artifactId>lombok</artifactId>
    4. <version>1.16.10</version>
    5. </dependency>

    中间件

    1. 1.redis
    2. <dependency>
    3. <groupId>redis.clients</groupId>
    4. <artifactId>jedis</artifactId>
    5. <version>2.9.0</version>
    6. </dependency>
    7. 2.消息队列
    8. <dependency>
    9. <groupId>org.springframework.cloud</groupId>
    10. <artifactId>spring-cloud-starter-bus-amqp</artifactId>
    11. </dependency>
    12. 3.RabbitMQ
    13. <dependency>
    14. <groupId>org.springframework.cloud</groupId>
    15. <artifactId>spring-cloud-starter-stream-rabbit</artifactId>
    16. </dependency>

  • 相关阅读:
    Java配置28-CentOS7下配置redis主从关系
    大数据运维实战第一课 大话 Hadoop 生态圈
    网络-网络状态&网络速度
    【华为路由器】配置企业通过5G链路接入Internet示例
    竞赛 深度学习 python opencv 火焰检测识别
    JS数组方法map 和 forEach 的区别
    1.5 JAVA程序运行的机制
    Android-Handler源码解析-Message
    单目操作符
    LeetCode 热题 HOT 100:二叉树专题
  • 原文地址:https://blog.csdn.net/yangyifan0/article/details/128000917