Maven 项目中常用的工具包

1 时间相关

1.1 依赖包

            <dependency>
                <groupId>joda-time</groupId>
                <artifactId>joda-time</artifactId>
                <version>2.9.4</version>
            </dependency>

1.2 使用案例

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

2 fastjson

依赖包

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.75</version>
        </dependency>

使用:

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

3.org.apache.commons

依赖:

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.11</version>
        </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工具类。

依赖:

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.6</version>
</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

        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.5.2</version>
        </dependency>

使用:

        HttpUtil.get();
        ...等等

一个 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

        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>${google.guava.version}</version>
        </dependency>

使用

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

MySql等数据库依赖

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <!-- <version>8.0.21</version> 版本Spring-Boot-Parent中已带 -->
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.2.11</version>
</dependency>


<!-- mybaytis plus -->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.1</version>
</dependency>

lombok

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.16.10</version>
</dependency>

中间件

1.redis
<dependency>
	<groupId>redis.clients</groupId>
	<artifactId>jedis</artifactId>
	<version>2.9.0</version>
</dependency>
2.消息队列
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

3.RabbitMQ
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值