需求:将对象属性值快速复制到另一个对象上,忽略空值属性和id
直接上代码:BeanUtils 工具类
注意:如果需要关注复制性能的话,推荐用插件https://blog.youkuaiyun.com/qiaodaima0/article/details/126240532?spm=1001.2014.3001.5501
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import cn.newi.common.util.StringUtil;
public class BeanUtils {
/**
* 拷贝bean中的属性
* @param source
* @param target
* @param ignores
*/
public static void copyProperties(Object source, Object target, String... ignores){
org.springframework.beans.BeanUtils.copyProperties(source, target, ignores);
}
/**
* 拷贝bean中的属性 忽略掉空值属性和ID属性
* @param source
* @param target
*/
public static void copyPropertiesIgnoreNullValueAndId(Object source, Object target){
copyProperties(source, target, getNullPropertyNamesAndIgnoreNames(source, "id"));
}
/**
* 拷贝bean中的属性 忽略掉空值属性
* @param source
* @param target
*/
public static void copyPropertiesIgnoreNullValue(Object source, Object target){
copyProperties(source, target, getNullPropertyNamesAndIgnoreNames(source));
}
/**
* 获取空值属性名称或者忽略
* @param source
* @param ignores
* @return
*/
public static String[] getNullPropertyNamesAndIgnoreNames (Object source, String... ignores ) {
final BeanWrapper src = new BeanWrapperImpl(source);
java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();
Set<String> emptyNames = new HashSet<String>();
if (ignores != null && ignores.length > 0){
emptyNames.addAll(Arrays.asList(ignores));
}
for(java.beans.PropertyDescriptor pd : pds) {
Object srcValue = src.getPropertyValue(pd.getName());
if (srcValue == null) emptyNames.add(pd.getName());
}
String[] result = new String[emptyNames.size()];
return emptyNames.toArray(result);
}
public static String[] convertStringToArray(String ids){
if (StringUtil.isEmpty(ids)){
return null;
}
return ids.split(",");
}
/**
* 分解URL协议、host、端口、path等参数
* "http://www.baidu.com:8080/1223/sdfsd?abc=rer";
* String protocol = "";
String host = "";
String port = "";
String path = "";
["http","www.baidu.com","8080","/1223/sdfsd?abc=rer"]
* @param url
* @return
*/
public static List<String> splitUrlPath(String url){
if (StringUtil.isEmpty(url)){
return null;
}
url = url.trim();
String protocol = "";
String host = "";
String port = "";
String path = "";
List<String> params = new ArrayList<>();
String[] protocols = url.split("://");
if (protocols == null || protocols.length != 2){
return null;
}
protocol = protocols[0];
if (StringUtil.isEmpty(protocols[1])){
return null;
}
if (protocols[1].indexOf(':') < 0){//没有端口号,即80端口
port = "80";
String[] hosts = protocols[1].split("/");
if (hosts == null || hosts.length < 2){
host = protocols[1];
path = "";
} else {
host = hosts[0];
path = protocols[1].substring(host.length(), protocols[1].length());
}
} else {
String[] hosts = protocols[1].split(":");
host = hosts[0];
String[] ports = hosts[1].split("/");
port = ports[0];
path = hosts[1].substring(port.length(), hosts[1].length());
}
params.add(protocol);
params.add(host);
params.add(port);
params.add(path);
return params;
}
}
使用方法:
BeanUtils.copyProperties(被复制对象, 粘贴内容的对象);
2万+

被折叠的 条评论
为什么被折叠?



