几个常用工具类的简单理解

常用的几个工具类

StringUtils 工具类

org.apache.commons.lang这个包下的

使用方式

一般使用 isBlank() 方法判断字符串是否为空
官方解释:
Checks if a String is whitespace, empty ("") or null.
StringUtils.isBlank(null) = true
StringUtils.isBlank("") = true
StringUtils.isBlank(" “) = true
StringUtils.isBlank(“bob”) = false
StringUtils.isBlank(” bob ") = false

Parameters:str the String to check, may be nullReturns:true if the String is null, empty or whitespaceSince:2.0

DigestUtils 工具类

org.apache.commons.codec.digest 包下

使用方式

一般给密码加密,常用方法

1. 加密 md5Hex()

/**
* Calculates the MD5 digest and returns the value as a 32 character hex string.
*// 计算MD5摘要并将值作为32个字符的十六进制字符串返回
* @param data
* Data to digest
* @return MD5 digest as a hex string
*/
public static String md5Hex(final String data) {
return Hex.encodeHexString(md5(data));
}

2. 加密 sha256Hex()

/**
* Calculates the SHA-256 digest and returns the value as a hex string.
*


* Throws a RuntimeException on JRE versions prior to 1.4.0.
*


*
* @param data
* Data to digest
* @return SHA-256 digest as a hex string
* @since 1.4
*/
public static String sha256Hex(final byte[] data) {
return Hex.encodeHexString(sha256(data));
}

RandomStringUtils

org.apache.commons.lang 包下

一般使用方法 random()

1.random(int count, boolean letters, boolean numbers)

/**
*

Creates a random string whose length is the number of characters
* specified.


*
*

Characters will be chosen from the set of alpha-numeric
* characters as indicated by the arguments.


*
* @param count the length of random string to create
* @param letters if true, generated string will include
* alphabetic characters
* @param numbers if true, generated string will include
* numeric characters
* @return the random string
*/
public static String random(int count, boolean letters, boolean numbers) {
return random(count, 0, 0, letters, numbers);
}
2. randomNumeric(int count)

/**
*

Creates a random string whose length is the number of characters
* specified.


*
*

Characters will be chosen from the set of numeric
* characters.


*
* @param count the length of random string to create
* @return the random string
*/
public static String randomNumeric(int count) {
return random(count, false, true);
}

生成指定位数的字符串(只包含数字)

BeanUtils.copyProperties(A, B)

org.apache.commons.beanutils 包下
将实体类A赋值给实体类B

UUID.randomUUID()

解释:
UUID.randomUUID().toString()是javaJDK提供的一个自动生成主键的方法。UUID(Universally Unique Identifier)全局唯一标识符,是指在一台机器上生成的数字,它保证对在同一时空中的所有机器都是唯一的,是由一个十六位的数字组成,表现出来的形式。由以下几部分的组合:当前日期和时间(UUID的第一个部分与时间有关,如果你在生成一个UUID之后,过几秒又生成一个UUID,则第一个部分不同,其余相同),时钟序列,全局唯一的IEEE机器识别号(如果有网卡,从网卡获得,没有网卡以其他方式获得),UUID的唯一缺陷在于生成的结果串会比较长。

常用于作为唯一id使用

猜你喜欢

转载自blog.csdn.net/bnxf_xv/article/details/88943797