初次上班,写业务过程中发现项目里面没有common.lang3.BeanUtils类,只有spring自己引入的BeanUtils,只能copy单个对象,不能copy List或者Map,导致有些业务场景冗余代码过多,对后期维护不太友好, 限于公司不能随便加依赖以及改动项目整体架构,根据SpringFramework提供的Beanutils,就自己想到开发一个java bean copy工具类。
我写的这个工具类是基于SpringFramework自带的BeanUtils写的。
第三方包都有提供这样的功能,如果各位小伙伴项目中有第三方BeanUtils工具类,可以直接用第三方包解决即可。
/**
* User实体类
* @Author GaoJun
* @Date Created in 14:34 2020/3/24
*/
public class User {
private Integer id;
private String name;
private String userName;
public Integer getId() {
return id;
}
public User() {
}
public User(Integer id, String name, String userName) {
this.id = id;
this.name = name;
this.userName = userName;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", userName='" + userName + '\'' +
'}';
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
/**
* User的Vo类
* @Author GaoJun
* @Date Created in 14:34 2020/3/24
*/
public class UserVo {
private Integer id;
private String name;
private String nameVo;
public UserVo(Integer id, String name, String nameVo) {
this.id = id;
this.name = name;
this.nameVo = nameVo;
}
public UserVo() {
}
@Override
public String toString() {
return "UserVo{" +
"id=" + id +
", name='" + name + '\'' +
", nameVo='" + nameVo + '\'' +
'}';
}
public String getNameVo() {
return nameVo;
}
public void setNameVo(String nameVo) {
this.nameVo = nameVo;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
import org.springframework.beans.BeanUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;
/**
* java bean copy工具类
* 作用于:集合内属性互转
* @Author GaoJun
* @Date 15:45 2020/3/24
* {@code List互转:
* List users = Arrays.asList(new User(1, "name1", "user1"), new User(2, "name2", "user2"), new User(3, "name3", "user3"));
* List userVoList = BeanCopyUtils.copyListProperties(users,UserVo::new,(user,userVO) -> {
* userVO.setNameVo(user.getUserName());
* });
* sout:
* UserVo{id=1, name='name1', nameVo='user1'}
* UserVo{id=2, name='name2', nameVo='user2'}
* UserVo{id=3, name='name3', nameVo='user3'}
* }
* {@code Map互转:
* List users = Arrays.asList(new User(1, "name1", "user1"), new User(2, "name2", "user2"), new User(3, "name3", "user3"));
* Map> map = new HashMap<>();
* map.put("1",users);
* List users1 = Arrays.asList(new User(11, "name11", "user11"), new User(22, "name22", "user22"), new User(33, "name33", "user33"));
* map.put("2",users1);
* Map> map1 = Utils.copyMapProperties(map, UserVo::new, (user,userVo) -> {
* userVo.setNameVo(user.getUserName());
* });
* sout:
* 1: UserVo{id=1, name='name1', nameVo='user1'}
* UserVo{id=2, name='name2', nameVo='user2'}
* UserVo{id=3, name='name3', nameVo='user3'}
* 2: UserVo{id=11, name='name11', nameVo='user11'}
* UserVo{id=22, name='name22', nameVo='user22'}
* UserVo{id=33, name='name33', nameVo='user33'}
* }
*/
public class BeanCopyUtils extends BeanUtils {
/**
* List数据的拷贝
* @param sources: 数据源类
* @param target: 目标类::new(eg: UserVO::new)
* @return
*/
public static <S, T> List<T> copyListProperties(List<S> sources, Supplier<T> target) {
return copyListProperties(sources, target, null);
}
/**
* Map数据的拷贝
* @param sources: 数据源类
* @param target: 目标类::new(eg: UserVO::new)
* @return
*/
public static <K, S, T> Map<K, List<T>> copyMapProperties(Map<K, List<S>> sources, Supplier<T> target){
return copyMapProperties(sources, target, null);
}
/**
* 带回调函数的List数据的拷贝(可自定义字段拷贝规则)
*
* @param sources: 数据源类
* @param target: 目标类::new(eg: UserVO::new)
* @param callBack: 回调函数
* @return
*/
public static <S, T> List<T> copyListProperties(List<S> sources, Supplier<T> target, BeanCopyUtilCallBack<S, T> callBack) {
List<T> list = new ArrayList<>(sources.size());
for (S source : sources) {
T t = target.get();
copyProperties(source, t);
list.add(t);
if (callBack != null) {
// 回调
callBack.callBack(source, t);
}
}
return list;
}
/**
* 带回调函数的Map数据的拷贝(可自定义字段拷贝规则)
* @param sources: 数据源类
* @param target: 目标类::new(eg: UserVO::new)
* @param callBack: 回调函数
* @return
*/
public static <K, S, T> Map<K, List<T>> copyMapProperties(Map<K, List<S>> sources, Supplier<T> target, BeanCopyUtilCallBack<S, T> callBack){
Map<K, List<T>> map = new HashMap<>(sources.size());
for (Map.Entry<K, List<S>> source : sources.entrySet()) {
List<T> ts = copyListProperties(source.getValue(), target, callBack);
map.put(source.getKey(),ts);
}
return map;
}
}
@FunctionalInterface
interface BeanCopyUtilCallBack <S, T> {
/**
* 定义默认回调方法
* @param t
* @param s
*/
void callBack(S t, T s);
}
import java.util.*;
/**
* Main测试类
* @Author GaoJun
* @Date Created in 14:37 2020/3/24
*/
public class Test01 {
public static void main(String[] args) {
List<User> users = Arrays.asList(new User(1, "name1", "user1"), new User(2, "name2", "user2"), new User(3, "name3", "user3"));
Map<String,List<User>> map = new HashMap<>();
map.put("1",users);
List<User> users2 = Arrays.asList(new User(11, "name11", "user11"), new User(22, "name22", "user22"), new User(33, "name33", "user33"));
map.put("2",users2);
Map<String, List<UserVo>> map1 = BeanCopyUtils.copyMapProperties(map, UserVo::new, (user,userVo) -> {
userVo.setNameVo(user.getUserName());
});
map1.forEach((key,value) -> {
System.out.print(key + ":");
value.forEach(System.out::println);
});
}
}