关于BeanCopy的常用方法

package hcj.info.hcjinfo.OftenUseApi;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.BeanUtils;

import java.lang.reflect.InvocationTargetException;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
* BeanUtil.copyProperties 和 BeanUtils.copyProperties用法
* 主要区别:
* BeanUtil.copyProperties只要key相同就会copy
* BeanUtils.copyProperties需要key和类型相同才会copy
* <p>
* Map与实体直接相互转换
*
* @author hcj
*/
public class BeanCopyApi {
public static void main(String[] args) throws InvocationTargetException, IllegalAccessException {
Student stu = new Student(20, "zz", LocalDateTime.now());
// age=20, name=zz, date=2021-10-26T11:07:52.471
System.out.println("stu ==" + stu);

Student1 stu1 = new Student1();
// key值相同,即复制,若类型不一致可能会导致转换错误
BeanUtil.copyProperties(stu, stu1);
System.out.println("stu1==" + stu1);
// age=20, name=zz, date=2021-10-26T11:07:52.471

// key值相同,且类型相同即复制
Student1 stu1s = new Student1();
BeanUtils.copyProperties(stu, stu1s);
System.out.println("stu1s==" + stu1s);
// age=null, name=zz, date=null


Student1 stu11 = new Student1();
// 复制时排除age属性字段
BeanUtil.copyProperties(stu, stu11, "age");
System.out.println("stu11==" + stu11);
// age=null, name=zz, date=2021-10-26T11:07:52.471

Student2 stu2 = new Student2();
// 属性值不一样,不进行copy,区分大小写
BeanUtil.copyProperties(stu, stu2);
System.out.println("stu2==" + stu2);
// Age=null, Name=null, date=2021-10-26T11:07:52.471

Student2 stu22 = new Student2();
// 忽略大小写
BeanUtil.copyProperties(stu, stu22, true);
System.out.println("stu22==" + stu22);
// Age=20, Name=zz, date=2021-10-26T11:07:52.471

// 有返回值的copy
Student1 stu3 = BeanUtil.copyProperties(stu, Student1.class);
System.out.println("stu3==" + stu3);
// age=20, name=zz, date=2021-10-26T11:07:52.471

Map map = new HashMap<>(3);
map.put("age", "26");
map.put("name", "hh");
map.put("date", LocalDateTime.now());
System.out.println("map==" + map);

Student stu4 = BeanUtil.toBean(map, Student.class);
System.out.println("stu4==" + stu4);

Student stu5 = BeanUtil.toBeanIgnoreError(map, Student.class);
System.out.println("stu5==" + stu5);

Map<String, Object> map11 = BeanUtil.beanToMap(stu);
System.out.println("map11==" + map11);

// 将实体类 转换为 Map
Map map111 = JSON.parseObject(JSON.toJSONString(stu), Map.class);
System.out.println(map111);
}

@Data
@NoArgsConstructor
@AllArgsConstructor
public static class Student {
private Integer age;
private String name;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime date;
}

@Data
@NoArgsConstructor
@AllArgsConstructor
public static class Student1 {
private String age;
private String name;
private String date;
}

@Data
@NoArgsConstructor
@AllArgsConstructor
public static class Student2 {
private String Age;
private String Name;
private String date;
}

}
posted @ 2022-10-18 11:12  Mr|Huang  阅读(706)  评论(0)    收藏  举报