在两个实体类里,有相同的属性,有不同的属性,相同名称的属性类型还可能不同,如何实现相同属性的拷贝/复制呢?
实体类
代码清单1:Neo4jNode.java
package com.graph.domain;
import lombok.Data;
import java.util.HashMap;
import java.util.Map;
/**
* @Author:四个空格-https://www.4spaces.org
*/
@Data
public class Neo4jNode {
private String id;
private String label;
private String labelName;
private String typeUri;
private Double x;
private Double y;
private Map<String, Object> properties = new HashMap<>();
}
代码清单2:SigmaNode.java
package com.graph.domain;
import lombok.Data;
import java.util.List;
import java.util.Map;
/**
* @Author:四个空格-https://www.4spaces.org
*/
@Data
public class SigmaNode {
private String id;
private String label;
private String labelName;
private String typeUri;
private Double x;
private Double y;
private List<Map<String, Object>> properties;
}
方式1:org.springframework.beans.BeanUtils
package com.graph.domain;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.BeanUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class BeanCopyExample {
public static void main(String[] args) {
SigmaNode sigmaNode = new SigmaNode();
sigmaNode.setId("1");
sigmaNode.setLabel("https://www.4spaces.org");
sigmaNode.setLabelName("四个空格");
Map<String, Object> propertieMap = new HashMap<>();
propertieMap.put("title", "博客");
propertieMap.put("subject", "技术");
List<Map<String, Object>> properties = new ArrayList<>();
properties.add(propertieMap);
sigmaNode.setProperties(properties);
Neo4jNode neo4jNode = new Neo4jNode();
BeanUtils.copyProperties(sigmaNode, neo4jNode);
System.out.println(JSON.toJSONString(neo4jNode));
}
}
方式2:org.apache.commons.beanutils.BeanUtils
package com.graph.domain;
import com.alibaba.fastjson.JSON;
import org.apache.commons.beanutils.BeanUtils;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class BeanCopyExample {
public static void main(String[] args) {
SigmaNode sigmaNode = new SigmaNode();
sigmaNode.setId("1");
sigmaNode.setLabel("https://www.4spaces.org");
sigmaNode.setLabelName("四个空格");
Map<String, Object> propertieMap = new HashMap<>();
propertieMap.put("title", "博客");
propertieMap.put("subject", "技术");
List<Map<String, Object>> properties = new ArrayList<>();
properties.add(propertieMap);
sigmaNode.setProperties(properties);
Neo4jNode neo4jNode = new Neo4jNode();
try {
BeanUtils.copyProperties(sigmaNode, neo4jNode);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
System.out.println(JSON.toJSONString(neo4jNode));
}
}
总结
由于两个实体类properties属性类型不一样,org.apache.commons.beanutils.BeanUtils
会抛出:java.lang.IllegalArgumentException
异常,而org.springframework.beans.BeanUtils
则不会对该属性赋值,不会抛出异常。
参考文章:
最新评论
网飞没问题, 迪士尼+有解决方案么?
pp助手是安卓手机用的,根本下载用不来苹果
已解决
这样的话数据库里的结构为{"attachment":{"content":"xxx"}}, 要怎么才能变成{"content":"xxx"},从而使结构保持一致?
赞! make test不过的坑都写到的,谢谢楼主~
谢谢你
用了root用户还是一直502是怎么回事呢
student id 是空的