如果字段名称不同,如何将 POJO 映射到 DTO
问题描述:
我有一个 POJO 对象,它是从数据库映射的。其中的属性(列)与数据库表中的名称相同。但它需要一些改变,我将不得不使用 DTO。但问题是 DTO 具有数据库表中定义的不同名称的属性(因此我将数据库表映射到 POJO,然后将 POJO 映射到 DTO),因此在映射时我必须使用字段和字段映射(POJO 的一个字段和 DTO 对象的一个字段),这将需要 50 行代码(数据库表有 50 列)。有什么解决方案可以使用 DTO 直接映射到我的 POJO 吗?或者如果数据库表和 POJO/DTO 具有不同的列名,是否有办法将数据库表映射到 POJO/DTO?
例如
public class EmployeePOJO {
String EMP_ID;
String EMP_NAME;
String EMP_SALERY;
String EMP_DOB;
String EMP_CONTACT_NO;
String EMP_ADDRESS;
String EMP_BLOOD_GROUP;
String ASSIGNED_PROJECT;
String PROJECT_MANAGER;
String ROLE;
//Getters and setters
}
public class EmployeeDTO {
//String EMP_ID;
//String EMP_NAME;
String salery; //EMP_SALERY;
//String EMP_DOB;
String phoneNumber; //EMP_CONTACT_NO;
String address; //EMP_ADDRESS;
//String EMP_BLOOD_GROUP;
String currentProject; //ASSIGNED_PROJECT;
String projectManager; //PROJECT_MANAGER;
String role; //ROLE;
//getters and setters
}
Problem description:
I have a POJO object, which is mapped from database. Which having attributes(column) with same name as in database table. But it required some changes and I will have to use DTOs. But problem is that DTO having attributes with different names as defined in database table,(because of this I am mapping database table to POJO and then POJO to DTO) so at the time of mapping I have to use field and field mapping(One field of POJO and one field of DTO object), and that will take 50 lines of code( database table having 50 columns). Is there any solution to map directly to my POJO with DTO? Or is there a way to map database table to POJO/DTO if database table and POJO/DTO having diffrent column names?
For example
public class EmployeePOJO {
String EMP_ID;
String EMP_NAME;
String EMP_SALERY;
String EMP_DOB;
String EMP_CONTACT_NO;
String EMP_ADDRESS;
String EMP_BLOOD_GROUP;
String ASSIGNED_PROJECT;
String PROJECT_MANAGER;
String ROLE;
//Getters and setters
}
public class EmployeeDTO {
//String EMP_ID;
//String EMP_NAME;
String salery; //EMP_SALERY;
//String EMP_DOB;
String phoneNumber; //EMP_CONTACT_NO;
String address; //EMP_ADDRESS;
//String EMP_BLOOD_GROUP;
String currentProject; //ASSIGNED_PROJECT;
String projectManager; //PROJECT_MANAGER;
String role; //ROLE;
//getters and setters
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
http://sourceforge.net/projects/dozer/develop ? (不确定它是否符合您的需求,但请看一下)
http://dozer.sourceforge.net/
http://sourceforge.net/projects/dozer/develop ? (not sure it maps your needs,but take a look)
http://dozer.sourceforge.net/
我的 ModelMapper 是另一个值得一试的库。它提供了一个流畅的 API 来映射属性,而不是使用字符串引用或 XML。
查看 ModelMapper 网站了解更多信息:
http://modelmapper.org
My ModelMapper is another library worth checking out. It offers a fluent API to map properties as opposed to using string references or XML.
Check out the ModelMapper site for more info:
http://modelmapper.org
您可以添加
@JsonProperty("salery")
以将 EMP_SALERY 字段映射到字段顶部。它使用
import com.fasterxml.jackson.annotation.JsonProperty;
的导入,这在使用 ModelMapper 时对我有用。
如果您再次担心反序列化,您有两种选择。
另一种方法是使用别名
You can add
@JsonProperty("salery")
for mapping the EMP_SALERY field on the top of the field.which uses the import of
import com.fasterxml.jackson.annotation.JsonProperty;
This works for me when using the ModelMapper.
If again deserialization is your concern you have 2 options.
The other way is using Alias
我建议您尝试 JMapper 框架。
通过一些配置(使用注释或 xml),您就可以进行映射了
I suggest you try JMapper Framework.
With a little configuration (with annotations or xml) you're ready to map