gpt4 book ai didi

java - Dozer Mapper 未映射组成的嵌套字段

转载 作者:行者123 更新时间:2023-11-30 08:44:17 27 4
gpt4 key购买 nike

正在使用的工具/框架:

  • JDK 1.7
  • Spring 3.2.5.RELEASE
  • 推土机 5.5.1

类:BaseModel.java

package com.demo.model;
public class BaseModel {
protected Long createdBy;
protected Timestamp createdTimestamp;
protected Long updatedBy;
protected Timestamp updatedTimestamp;
protected Long revision;
// ... Getters/Setters
}

类:Author.java

public class Author {
private BaseModel baseModel;
private Long id;
private String firstName;
private String lastName;

// Getter of baseModel
public BaseModel getBaseModel() {
return baseModel == null ? new BaseModel() : baseModel;
}
// ... Other Getters/Setters
}

类:BasePojo.java

package com.demo.web.pojo;
public class BasePojo {
protected Long createdBy;
protected Timestamp createdTimestamp;
protected Long updatedBy;
protected Timestamp updatedTimestamp;
protected Long revision;
// ... Getters/Setters
}

类:AuthorPojo.java

package com.demo.web.pojo;
public class AuthorPojo extends BasePojo {
private Long id;
private String firstName;
private String lastName;
// Getters/Setters
}

类:SpringConfig.java

@Configuration
@EnableAspectJAutoProxy(proxyTargetClass = true)
@ComponentScan("com.demo")
public class SpringConfig {

}

类:DemoDozerMapper.java

@Component("demoDozerMapper")
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
public class DemoDozerMapper extends DozerBeanMapper {
@PostConstruct
public void init() {
logger.info("PostConstruct called....");
this.initMappings();
}

private void initMappings() {
final BeanMappingBuilder authorMappingBuilder = new BeanMappingBuilder() {
@Override
protected void configure() {
mapping(Author.class, com.demo.web.pojo.Author.class,
TypeMappingOptions.mapId("authorMap"),
TypeMappingOptions.mapNull(true), TypeMappingOptions.oneWay())
.fields("baseModel.createdBy", "createdBy")
.fields("baseModel.createdTimestamp", "createdTimestamp")
.fields("baseModel.updatedBy", "updatedBy")
.fields("baseModel.updatedTimestamp", "updatedTimestamp");
}
};

addMapping(authorMappingBuilder);
}
}

类:Demo.java

public class Demo {
public static void main(String[] args) {
final ApplicationContext springAppContext = new AnnotationConfigApplicationContext(SpringConfig.class);
final Mapper dozerMapper = springAppContext.getBean("demoDozerMapper", DemoDozerMapper.class);

final Author authorModel = new Author();
authorModel.getBaseModel().setCreatedBy(Long.valueOf(1L));
authorModel.getBaseModel().setCreatedTimestamp(new Timestamp(System.currentTimeMillis()));
authorModel.setFirstName("First");
authorModel.setLastName("Last");
authorModel.setId(Long.valueOf(21101L));

final com.demo.web.pojo.Author author = new com.demo.web.pojo.Author();
dozerMapper.map(authorModel, author);
System.out.println("Author Pojo: " + author);
}
}

输出

Author Pojo: AuthorPojo {id=21101, firstName=First, lastName=Last, createdBy=null, createdTimestamp=null, updatedBy=null, updatedTimestamp=null, revision=null}

字段 createdTimestampcreatedBy 没有从模型映射到 pojo。我做错了什么吗?有人可以帮忙吗?

最佳答案

我在上面的评论后又看了一眼。除了更改在 authorModel 上设置 baseModel 的方式外,您还需要确保调用映射器时传递与您定义的映射相对应的 id DemoDozerMapper.java(即 authorMap)- 参见 here有关 Dozer 中基于上下文的映射的详细信息。

修改后的 Demo.java 应该是这样的:

类:Demo.java

public class Demo {
public static void main(String[] args) {
final ApplicationContext springAppContext = new AnnotationConfigApplicationContext(SpringConfig.class);
final Mapper dozerMapper = springAppContext.getBean("demoDozerMapper", DemoDozerMapper.class);

final Author authorModel = new Author();

// Ensure base model is set
BaseModel baseModel = new BaseModel();
baseModel.setCreatedBy(Long.valueOf(1L));
baseModel.setCreatedTimestamp(new Timestamp(System.currentTimeMillis()));
authorModel.setBaseModel(baseModel);

authorModel.setFirstName("First");
authorModel.setLastName("Last");
authorModel.setId(Long.valueOf(21101L));

final AuthorPojo author = new AuthorPojo();

// Select the appropriate case to use
dozerMapper.map(authorModel, author, "authorMap");
System.out.println("Author Pojo: " + author);
}
}

修改后的输出

Author Pojo: AuthorPojo [id=21101, firstName=First, lastName=Last, createdBy=1, createdTimestamp=2015-11-26 10:07:31.501, updatedBy=null, updatedTimestamp=null, revision=null]

关于java - Dozer Mapper 未映射组成的嵌套字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33853347/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com