Mapping 如何使用推土机将父字段映射到子对象字段
我正在使用推土机将我的对象A映射到BMapping 如何使用推土机将父字段映射到子对象字段,mapping,dozer,Mapping,Dozer,我正在使用推土机将我的对象A映射到B Class A { int indicatorA; List<A> listA; // get set methods... } Class B{ int indicatorB; List<B> listB; // get set methods... } 映射之后,我希望看到像这样的对象B B parentB//带指示符号B=10 和parentB.listB,其中有两个对象child1B和chld2B,indicatorB值设
Class A {
int indicatorA;
List<A> listA;
// get set methods...
}
Class B{
int indicatorB;
List<B> listB;
// get set methods...
}
映射之后,我希望看到像这样的对象B
B parentB//带指示符号B=10
和parentB.listB,其中有两个对象child1B和chld2B,indicatorB值设置为10
如何编写自定义转换器或任何简单的方法来做到这一点?
非常感谢您的帮助。。
谢谢是的,您需要一个CustomConverter实现,因为不能通过直接映射在列表的元素内设置值 推土机XML需要一个简单的配置标记,引用CustomConverter类:
<?xml version="1.0" encoding="UTF-8"?>
<mappings xmlns="http://dozer.sourceforge.net"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://dozer.sourceforge.net
http://dozer.sourceforge.net/schema/beanmapping.xsd">
<configuration>
<custom-converters>
<converter type="somepackage.MyConverter" >
<class-a>somepackage.A</class-a>
<class-b>somepackage.B</class-b>
</converter>
</custom-converters>
</configuration>
</mappings>
这是CustomConverter:
public class MyConverter implements CustomConverter {
public Object convert(Object destination, Object source, Class destClass, Class sourceClass){
if (source == null) {
return null;
}
B dest = null;
if (source instanceof A) {
// check to see if the object already exists
if (destination == null) {
dest = new B();
} else {
dest = (B) destination;
}
//dest is your parentB instance
//setting indicatorB value in parentB
dest.setIndicatorB(((A) source).getIndicatorA());
//creating child instances of B
B child1B = new B();
B child2B = new B();
//setting indicatorB values to the child instances as well
child1B.setIndicatorB(((A) source).getIndicatorA());
child2B.setIndicatorB(((A) source).getIndicatorA());
//adding child Bs to parentB list
List<B> listOfBs = new ArrayList<B>();
listOfBs.add(child1B);
listOfBs.add(child2B);
dest.setListB(listOfBs);
return dest;
}
return null;
}
}
如果只有在运行时才知道childa的数量,则可以使用CustomConverter中的循环来创建相应数量的childb。是的,您需要一个CustomConverter实现,因为不能通过直接映射在列表的元素内设置值 推土机XML需要一个简单的配置标记,引用CustomConverter类:
<?xml version="1.0" encoding="UTF-8"?>
<mappings xmlns="http://dozer.sourceforge.net"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://dozer.sourceforge.net
http://dozer.sourceforge.net/schema/beanmapping.xsd">
<configuration>
<custom-converters>
<converter type="somepackage.MyConverter" >
<class-a>somepackage.A</class-a>
<class-b>somepackage.B</class-b>
</converter>
</custom-converters>
</configuration>
</mappings>
这是CustomConverter:
public class MyConverter implements CustomConverter {
public Object convert(Object destination, Object source, Class destClass, Class sourceClass){
if (source == null) {
return null;
}
B dest = null;
if (source instanceof A) {
// check to see if the object already exists
if (destination == null) {
dest = new B();
} else {
dest = (B) destination;
}
//dest is your parentB instance
//setting indicatorB value in parentB
dest.setIndicatorB(((A) source).getIndicatorA());
//creating child instances of B
B child1B = new B();
B child2B = new B();
//setting indicatorB values to the child instances as well
child1B.setIndicatorB(((A) source).getIndicatorA());
child2B.setIndicatorB(((A) source).getIndicatorA());
//adding child Bs to parentB list
List<B> listOfBs = new ArrayList<B>();
listOfBs.add(child1B);
listOfBs.add(child2B);
dest.setListB(listOfBs);
return dest;
}
return null;
}
}
如果只有在运行时才知道childa的数量,则可以使用CustomConverter中的循环来创建相应数量的childb