如果只有一方有getter或setter(私有字段被命名为不同的名称,以确保它不直接访问私有字段),该怎么办?
如果一个有getter而另一个有setter来处理完全不匹配的私有字段呢?(但是getter和setter的名称匹配。)
我编写了一个测试程序,在https://www.jdoodle.com/online-java-compiler中运行:
import org.dozer.DozerBeanMapper;
public class Main {
public static class MySource {
// a -> a
private String a;
// getB() -> b
private String hidden_b;
public String getB() { return hidden_b; }
// c -> setC(c)
private String c;
// getD() -> setD(d)
private String hidden_d;
// proper getters and setters on both sides
private String proper;
public String getProper() { return proper; }
// public void setProper(String proper_) { proper = proper_; }
public MySource() {
a = "A Room with a View";
hidden_b = "The Bridge of San Luis Rey";
c = "Civilwarland in Bad Decline";
hidden_d = "Darkness at Noon";
proper = "This should copy, at minimum.";
}
public void print() {
System.out.println("Source");
System.out.println("================================");
System.out.println("a = " + a);
System.out.println("hidden_b = " + hidden_b);
System.out.println("c = " + c);
System.out.println("hidden_d = " + hidden_d);
System.out.println("--------------------------------");
System.out.println("proper = " + proper);
System.out.println("");
}
}
public static class MyTarget {
private String a;
private String b;
private String hidden_c;
private String hidden_e;
public void setC(String param) { hidden_c = param; }
public void setD(String param) { hidden_e = param; }
private String proper;
// public String getProper() { return proper; }
public void setProper(String proper_) { proper = proper_; }
public MyTarget() {}
public void print() {
System.out.println("Target");
System.out.println("================================");
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("hidden_c = " + hidden_c);
System.out.println("hidden_e = " + hidden_e);
System.out.println("--------------------------------");
System.out.println("proper = " + proper);
System.out.println("");
}
}
public static void main(String args[]) {
MySource s = new MySource();
s.print();
System.out.println("Now dozing...");
System.out.println("");
MyTarget t = new DozerBeanMapper().map(s, MyTarget.class);
t.print();
}
}
Group ID: net.sf.dozer
Artifact ID: dozer
Version: 5.5.1
Source
================================
a = A Room with a View
hidden_b = The Bridge of San Luis Rey
c = Civilwarland in Bad Decline
hidden_d = Darkness at Noon
--------------------------------
proper = This should copy, at minimum.
Now dozing...
Target
================================
a = null
b = null
hidden_c = null
hidden_e = null
--------------------------------
proper = This should copy, at minimum.
好吧,这是我的发现。希望这能帮助到某人。
推土机5.5.1应该能够通过“类级是可访问的”来做到这一点。然而,有一个bug。它在未来的版本中得到了修正,例如Dozer6.1+。(包移动到一个新组org.github.dozermapper
。)虽然步骤有点复杂,但最终我放弃了尝试ModelMapper,它要好得多。这是我的代码。
包括此软件包:
Group ID: org.modelmapper
Artifact ID: modelmapper
Version: 2.3.2
以下是使用方法:
import org.modelmapper.ModelMapper;
import org.modelmapper.config.Configuration;
public class Main {
public static class MySource {
// a -> a
private String a;
// getB() -> b
private String hidden_b;
public String getB() { return hidden_b; }
// c -> setC(c)
private String c;
// getD() -> setD(d)
private String hidden_d;
// proper getters and setters on both sides
private String proper;
public String getProper() { return proper; }
// public void setProper(String proper_) { proper = proper_; }
public MySource() {
a = "A Room with a View";
hidden_b = "The Bridge of San Luis Rey";
c = "Civilwarland in Bad Decline";
hidden_d = "Darkness at Noon";
proper = "This should copy, at minimum.";
}
public void print() {
System.out.println("Source");
System.out.println("================================");
System.out.println("a = " + a);
System.out.println("hidden_b = " + hidden_b);
System.out.println("c = " + c);
System.out.println("hidden_d = " + hidden_d);
System.out.println("--------------------------------");
System.out.println("proper = " + proper);
System.out.println("");
}
}
public static class MyTarget {
private String a;
private String b;
private String hidden_c;
private String hidden_e;
public void setC(String param) { hidden_c = param; }
public void setD(String param) { hidden_e = param; }
private String proper;
// public String getProper() { return proper; }
public void setProper(String proper_) { proper = proper_; }
public MyTarget() {}
public void print() {
System.out.println("Target");
System.out.println("================================");
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("hidden_c = " + hidden_c);
System.out.println("hidden_e = " + hidden_e);
System.out.println("--------------------------------");
System.out.println("proper = " + proper);
System.out.println("");
}
}
public static void main(String args[]) {
ModelMapper modelMapper = new ModelMapper();
modelMapper.getConfiguration()
.setFieldMatchingEnabled(true)
.setFieldAccessLevel(Configuration.AccessLevel.PRIVATE);
MySource s = new MySource();
s.print();
System.out.println("Now dozing...");
System.out.println("");
MyTarget t = modelMapper.map(s, MyTarget.class);
t.print();
}
}
下面是我的输出:
Source
================================
a = A Room with a View
hidden_b = The Bridge of San Luis Rey
c = Civilwarland in Bad Decline
hidden_d = Darkness at Noon
--------------------------------
proper = This should copy, at minimum.
Now dozing...
Target
================================
a = A Room with a View
b = The Bridge of San Luis Rey
hidden_c = Civilwarland in Bad Decline
hidden_e = null
--------------------------------
proper = This should copy, at minimum.
第四个案子没有复制过来,但我真的不关心那个案子。不过,我认为可以通过不同的ModelMapper配置轻松实现。也许试试松散的复制。或者最坏的情况是,手动绑定配置中的getter和setter方法。
我试图找到一个更好的解决方案,以防止Hibernate代理初始化时,通过MapSTRt将实体映射到响应DTO。 我一直在将我们的代码库转换为使用ModelMapper中的MapStruct。如果我想用ModelMapper完成我的要求,我可以做如下简单的事情: 自定义getter方法允许我检查是否已经从数据库中获取了字段,以避免N 1次初始化。 它看起来像: 我不能简单地覆盖普通的getter,因
我想让推土机地图成为我的职业: 到生成的,如下所示: . 也就是说,我正在尝试将嵌套类的字段映射到平面键目标。我使用的是JavaAPI的Dozer,而不是xml。我无法找到适当的构建器配置来管理它。基本代码类似于:
我成功地将Jackson配置为序列化一个类,而不在类内部的私有字段上使用任何getter或注释 但我无法使这个非静态类的JSON字符串反序列化为一个没有任何参数化构造函数或设置器的对象。这是不是可能的--我想应该通过思考,但我不知道具体是怎么... 这里有两个测试,需要通过,以达到我的需要:
e(fx)clipse插件为Eclipse提供了一个generate命令,该命令将生成如上所述的JavaFX getter/setter。IntelliJ IDEA有类似的功能吗?
我有一个类,我们用一个映射字段将其称为a,它被转换为B类,用于数据库存储/检索,其中该字段映射到字符串。从A到B的映射非常有效。然而,当从B到A时,我得到了一个IllegalArgument异常,它表示无法将字符串转换为映射。让我困惑的是,Dozer的文档中说,这确实适用于以下情况: 数据类型转换由Dozer映射引擎自动执行。目前,Dozer支持以下类型的转换:(这些都是双向的) 然后它继续列出要
问题内容: 我正在开发一个新项目(),并创建了一个包含大量变量的Object。由于我打算为所有这些添加吸气剂和设置器,所以我想知道:是否存在在给定类中自动生成吸气剂和设置器的捷径? 问题答案: 在所需类的源代码窗口中弹出上下文菜单(即右键单击)。然后选择子菜单;从该菜单中进行选择将导致出现向导窗口。 选择您要为其创建getter和setter的变量,然后单击。