最近也是本人在写项目当中,遇到了一些通用的,便抽出来了,当公共工具类使用,下面给大家奉献出来,需要导入两个依赖,目前先写这么多,以后有使用的在继续更新2.0版本
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
数据转换工具类(Map转实体互转、等等)
package com.yada.tax.util;
import org.springframework.util.CollectionUtils;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
/**
* The type Data convert utils.
*
* @author hbx
* @date 2022 /8/18 14:12
*/
public class DataConvertUtils {
/**
* Copy properties to list list.
*
* @param <R> the type parameter
* @param sourceList the source list 数据源List
* @param targetClass the target class 目标对象.class
* @return the list
*/
public static <R> List<R> copyPropertiesToList(List<?> sourceList, Class<R> targetClass) {
assert !CollectionUtils.isEmpty(sourceList);
return sourceList.stream()
.map(item -> BeanUtils.copyValues(item, targetClass))
.collect(Collectors.toList());
}
/**
* Convert entity list to m list map list.
*
* @param <T> the type parameter
* @param entityList the entity list
* @return the list
*/
public static <T> List<Map<String, Object>> convertEntityListToMListMap(List<T> entityList) {
assert !CollectionUtils.isEmpty(entityList);
return entityList.stream()
.map(item -> {
LinkedHashMap<String, Object> objectMap = new LinkedHashMap<>(item.getClass().getDeclaredFields().length);
Arrays.stream(item.getClass().getDeclaredFields())
.forEach(field -> {
field.setAccessible(true);
String key = field.getName();
Object value = null;
try {
value = field.get(item);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
objectMap.put(key, Objects.isNull(value) ? "" : value);
});
return objectMap;
}).collect(Collectors.toList());
}
/**
* Convert map list to list entity list.
*
* @param <T> the type parameter
* @param mapList the map list
* @param clazz the clazz
* @return the list
*/
public static <T> List<T> convertMapListToListEntity(List<Map<String, Object>> mapList, Class<T> clazz) {
assert !CollectionUtils.isEmpty(mapList);
List<T> rtn = new ArrayList<>(mapList.size());
for (Map<String, Object> objectMap : mapList) {
T entity = convertMapToEntity(objectMap, clazz);
rtn.add(entity);
}
return rtn;
}
/**
* Convert object to big decimal big decimal.
*
* @param obj the obj
* @return the big decimal
*/
public static BigDecimal convertObjectToBigDecimal(Object obj) {
return new BigDecimal(StringUtils.convertToString(obj));
}
/**
* Convert map to entity t.
*
* @param <T> the type parameter
* @param map the map
* @param clazz the clazz
* @return the t
*/
public static <T> T convertMapToEntity(Map<String, Object> map, Class<T> clazz) {
assert !CollectionUtils.isEmpty(map);
T bean = ReflexUtils.getNewInstance(clazz);
Arrays.stream(clazz.getDeclaredFields())
.forEach(field -> {
field.setAccessible(true);
map.forEach((key, value) -> {
String name = field.getName();
if (name.equalsIgnoreCase(key) || name.equalsIgnoreCase(StringUtils.underlineSmallHump(key))) {
try {
if (!Objects.isNull(value)) {
if (StringUtils.containsIgnoreCase(value.getClass().getTypeName(), "BigDecimal")) {
field.set(bean, StringUtils.convertToString(value));
} else {
field.set(bean, value);
}
} else {
field.set(bean, null);
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
});
});
return bean;
}
}
有的同学导到这里报错了,有一个ReflexUtils还需要复制过去, 这个是利用Java反射获取对象实例
package com.yada.tax.util;
/**
* @author hbx
* @date 2022/8/22 14:47
*/
public class ReflexUtils {
public static <T> T getNewInstance(Class<T> clazz){
T instance = null;
try {
instance = clazz.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
return instance;
}
}
下面这个是BeanUtils,对于一些对象的值进行copy(这里是深拷贝哦)
package com.yada.tax.util;
/**
* The type Bean utils.
*
* @author hbx
* @date 2022 /8/22 13:51
*/
public class BeanUtils extends org.springframework.beans.BeanUtils {
/**
* Copy values t.
*
* @param <T> the type parameter
* @param source the source 数据源
* @param targetClass the target class 目标对象.class
* @return the t
*/
public static <T> T copyValues(Object source, Class<T> targetClass) {
T target = ReflexUtils.getNewInstance(targetClass);
org.springframework.beans.BeanUtils.copyProperties(source, target);
return target;
}
/**
* Copy values t.
*
* @param <T> the type parameter
* @param source the source 数据源
* @param targetClass the target class 目标对象.class
* @param ignoreValues the ignore values 不进行copy的值,可以多个
* @return the t
*/
public static <T> T copyValues(Object source, Class<T> targetClass, String... ignoreValues) {
T target = ReflexUtils.getNewInstance(targetClass);
org.springframework.beans.BeanUtils.copyProperties(source, target, ignoreValues);
return target;
}
/**
* Copy values t.
*
* @param <T> the type parameter
* @param source the source 数据源
* @param targetClass the target class 目标对象.class
* @param editable the editable 目标对象的限制类型(是否instansof)
* @return the t
*/
public static <T> T copyValues(Object source, Class<T> targetClass, Class<?> editable) {
T target = ReflexUtils.getNewInstance(targetClass);
org.springframework.beans.BeanUtils.copyProperties(source, target, editable);
return target;
}
}
最后一个是StringUtils,里面有一些个人使用的一些方法便放进去了
package com.yada.tax.util;
import java.util.Objects;
import java.util.Random;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* The type String utils. 字符串工具类
*
* @author hbx
* @date 2022 /8/2 15:22
*/
public class StringUtils extends org.apache.commons.lang3.StringUtils {
/**
* 补全左侧字符
*
* @param text the text 字符串内容
* @param length the length 总长度
* @param character the character 补全的字符
* @return the string
*/
public static String padLeft(String text, int length, char character) {
StringBuilder textBuilder = new StringBuilder(text);
while (textBuilder.length() < length) {
textBuilder.insert(0, character);
}
text = textBuilder.toString();
return text;
}
/**
* 补全右侧字符
*
* @param text the text 字符串内容
* @param length the length 总长度
* @param character the character 补全的字符
* @return the string
*/
public static String padRight(String text, int length, char character) {
StringBuilder textBuilder = new StringBuilder(text);
while (textBuilder.length() < length) {
textBuilder.append(character);
}
text = textBuilder.toString();
return text;
}
/**
* Convert to string string.
* 任意类型转换string类型
*
* @param object the object
* @return the string
*/
public static String convertToString(Object object) {
return Objects.nonNull(object) ? object.toString() : "";
}
/**
* Replace blank string.
* 去除所有空格、制表、换行符号
*
* @param str the str
* @return the string
*/
public static String replaceBlank(String str) {
if (StringUtils.isEmpty(str)) {
return "";
}
return Pattern.compile("\\s*|\t|\r|\n").matcher(str).replaceAll("");
}
/**
* Gets uuid.
* 32位UUID
*
* @return the uuid
*/
public static String getUUID() {
return UUID.randomUUID().toString().replace("-", "");
}
/**
* Gets random num.
* 获取18位数字字符串
* 拼接方式 当前时间格式:yymmddhhmmss + 4位随机数
*
* @return the random num
*/
public static String getRandomNum() {
return DateTimeUtil.getNowDateTime()
.concat(StringUtils.convertToString(new Random().nextInt(9999)));
}
/**
* Replace lt and gt string. < > 替换成<>
*
* @param text the text
* @return the string
*/
public static String replaceLtAndGt(String text) {
if (!StringUtils.containsIgnoreCase(text, "<") &&
!StringUtils.containsIgnoreCase(text, ">")) {
return text;
}
return StringUtils.replace(
StringUtils.replace(text, "<", "<"),
">",
">");
}
/**
* Underline small hump string. 下划线小驼峰字符串
*
* @param str the str
* @return the string
*/
public static String underlineSmallHump(String str){
Matcher matcher = Pattern.compile("_([a-z])").matcher(str.toLowerCase());
StringBuffer sb = new StringBuffer(str);
if (matcher.find()) {
sb = new StringBuffer();
matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
matcher.appendTail(sb);
} else {
return sb.toString().replaceAll("_", "");
}
return underlineSmallHump(sb.toString());
}
}