java对象到对象映射的任何工具?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1432764/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
any tool for java object to object mapping?
提问by ravinikam
I am trying to convert DO to DTO using java and looking for automated tool before start writing my own. I just wanted to know if there any free tool available for the same.
我正在尝试使用 java 将 DO 转换为 DTO 并在开始编写自己的工具之前寻找自动化工具。我只是想知道是否有任何可用的免费工具。
采纳答案by skaffman
You could try Dozer.
你可以试试推土机。
Dozer is a Java Bean to Java Bean mapper that recursively copies data from one object to another. Typically, these Java Beans will be of different complex types.
Dozer supports simple property mapping, complex type mapping, bi-directional mapping, implicit-explicit mapping, as well as recursive mapping. This includes mapping collection attributes that also need mapping at the element level.
Dozer 是 Java Bean 到 Java Bean 的映射器,它递归地将数据从一个对象复制到另一个对象。通常,这些 Java Bean 将具有不同的复杂类型。
Dozer 支持简单属性映射、复杂类型映射、双向映射、隐式-显式映射以及递归映射。这包括也需要在元素级别映射的映射集合属性。
回答by Pablojim
Use Apache commons beanutils:
使用 Apache commons beanutils:
static void copyProperties(Object dest, Object orig)
-Copy property values from the origin bean to the destination bean for all cases where the property names are the same.
static void copyProperties(Object dest, Object orig)
- 对于属性名称相同的所有情况,将属性值从源 bean 复制到目标 bean。
回答by Pascal Thivent
There are some libraries around there:
那里有一些图书馆:
Commons-BeanUtils: ConvertUtils -> Utility methods for converting String scalar values to objects of the specified Class, String arrays to arrays of the specified Class.
Commons-Lang: ArrayUtils -> Operations on arrays, primitive arrays (like int[]) and primitive wrapper arrays (like Integer[]).
Spring framework: Spring has an excellent support for PropertyEditors, that can also be used to transform Objects to/from Strings.
Dozer: Dozer is a powerful, yet simple Java Bean to Java Bean mapper that recursively copies data from one object to another. Typically, these Java Beans will be of different complex types.
ModelMapper: ModelMapper is an intelligent object mapping framework that automatically maps objects to each other. It uses a convention based approach to map objects while providing a simple refactoring safe API for handling specific use cases.
MapStruct: MapStruct is a compile-time code generator for bean mappings, resulting in fast (no usage of reflection or similar), dependency-less and type-safe mapping code at runtime.
Orika: Orika uses byte code generation to create fast mappers with minimal overhead.
Selma: Compile-time code-generator for mappings
JMapper: Bean mapper generation using Annotation, XML or API(seems dead, last updated 2 years ago)Smooks: The Smooks JavaBean Cartridge allows you to create and populate Java objects from your message data (i.e. bind data to) (suggested by superfilinin comments).(No longer under active development)Commons-Convert: Commons-Convert aims to provide a single library dedicated to the task of converting an object of one type to another. The first stage will focus on Object to String and String to Object conversions. (seems dead, last update 2010)Transmorph: Transmorph is a free java library used to convert a Java object of one type into an object of another type (with another signature, possibly parameterized).(seems dead, last update 2013)EZMorph: EZMorph is simple java library for transforming an Object to another Object. It supports transformations for primitives and Objects, for multidimensional arrays and transformations with DynaBeans(seems dead, last updated 2008)Morph: Morph is a Java framework that eases the internal interoperability of an application. As information flows through an application, it undergoes multiple transformations. Morph provides a standard way to implement these transformations.(seems dead, last update 2008)Lorentz: Lorentz is a generic object-to-object conversion framework. It provides a simple API to convert a Java objects of one type into an object of another type.(seems dead)OTOM: With OTOM, you can copy any data from any object to any other object. The possibilities are endless. Welcome to "Autumn".(seems dead)
Commons-BeanUtils: ConvertUtils -> 用于将字符串标量值转换为指定类的对象,将字符串数组转换为指定类的数组的实用方法。
Commons-Lang: ArrayUtils -> 对数组、原始数组(如 int[])和原始包装数组(如 Integer[])的操作。
Spring 框架:Spring 对 PropertyEditor 有很好的支持,它也可以用于将对象转换为/从字符串转换。
Dozer:Dozer 是一个强大而简单的 Java Bean 到 Java Bean 映射器,它递归地将数据从一个对象复制到另一个对象。通常,这些 Java Bean 将具有不同的复杂类型。
ModelMapper:ModelMapper 是一个智能对象映射框架,可以自动将对象相互映射。它使用基于约定的方法来映射对象,同时提供简单的重构安全 API 来处理特定用例。
MapStruct:MapStruct 是用于 bean 映射的编译时代码生成器,可在运行时生成快速(不使用反射或类似)、无依赖和类型安全的映射代码。
Orika:Orika 使用字节码生成来创建开销最小的快速映射器。
Selma:用于映射的编译时代码生成器
JMapper:使用 Annotation、XML 或 API 生成 Bean 映射器(似乎已死,上次更新于 2 年前)Smooks:Smooks JavaBean Cartridge 允许您从消息数据创建和填充 Java 对象(即绑定数据)(由superfilin在评论中建议)。(不再处于积极开发阶段)Commons-Convert:Commons-Convert 旨在提供一个专门用于将一种类型的对象转换为另一种类型的任务的库。第一阶段将重点关注对象到字符串和字符串到对象的转换。(似乎死了,最后更新 2010)Transmorph:Transmorph 是一个免费的 Java 库,用于将一种类型的 Java 对象转换为另一种类型的对象(带有另一个签名,可能是参数化的)。(似乎死了,最后更新 2013)EZMorph: EZMorph 是一个简单的 Java 库,用于将一个对象转换为另一个对象。它支持基元和对象的转换,多维数组和使用 DynaBeans 的转换(似乎已死,最后更新 2008 年)Morph:Morph 是一个 Java 框架,可简化应用程序的内部互操作性。当信息流经应用程序时,它会经历多次转换。Morph 提供了实现这些转换的标准方法。(似乎已经死了,最后更新 2008 年)Lorentz:Lorentz 是一个通用的对象到对象转换框架。它提供了一个简单的 API 来将一种类型的 Java 对象转换为另一种类型的对象。(好像死了)OTOM:使用 OTOM,您可以将任何数据从任何对象复制到任何其他对象。可能性是无止境。欢迎来到“秋天”。(好像死了)
回答by Dmitry
There is one more Java mapping engine/framework Nomin: http://nomin.sourceforge.net.
还有一个 Java 映射引擎/框架 Nomin:http: //nomin.sourceforge.net。
回答by Geoffrey Wiseman
I'm happy to add Moo as an option, although clearly I'm biased towards it: http://geoffreywiseman.github.com/Moo/
我很高兴将 Moo 添加为一个选项,尽管显然我偏向于它:http: //geoffreywiseman.github.com/Moo/
It's very easy to use for simple cases, reasonable capable for more complex cases, although there are still some areas where I can imagine enhancing it for even further complexities.
它很容易用于简单的情况,对于更复杂的情况来说是合理的,尽管我仍然可以想象在某些方面可以增强它以实现更复杂的情况。
回答by Tomas
Another one is Orika - https://github.com/orika-mapper/orika
另一个是 Orika - https://github.com/orika-mapper/orika
Orika is a Java Bean mapping framework that recursively copies (among other capabilities) data from one object to another. It can be very useful when developing multi-layered applications.
Orika focuses on automating as much as possible, while providing customization through configuration and extension where needed.
Orika enables the developer to :
- Map complex and deeply structured objects
- "Flatten" or "Expand" objects by mapping nested properties to top-level properties, and vice versa
- Create mappers on-the-fly, and apply customizations to control some or all of the mapping
- Create converters for complete control over the mapping of a specific set of objects anywhere in the object graph--by type, or even by specific property name
- Handle proxies or enhanced objects (like those of Hibernate, or the various mock frameworks)
- Apply bi-directional mapping with one configuration
- Map to instances of an appropriate concrete class for a target abstract class or interface
- Handle reverse mappings
- Handle complex conventions beyond JavaBean specs.
Orika uses byte code generation to create fast mappers with minimal overhead.
Orika 是一个 Java Bean 映射框架,它递归地将数据(以及其他功能)从一个对象复制到另一个对象。它在开发多层应用程序时非常有用。
Orika 专注于尽可能自动化,同时在需要时通过配置和扩展提供定制。
Orika 使开发人员能够:
- 映射复杂和深度结构化的对象
- 通过将嵌套属性映射到顶级属性来“展平”或“展开”对象,反之亦然
- 即时创建映射器,并应用自定义来控制部分或全部映射
- 创建转换器以完全控制对象图中任何位置的一组特定对象的映射——按类型,甚至按特定的属性名称
- 处理代理或增强对象(如 Hibernate 或各种模拟框架的对象)
- 通过一种配置应用双向映射
- 映射到目标抽象类或接口的适当具体类的实例
- 处理反向映射
- 处理超出 JavaBean 规范的复杂约定。
Orika 使用字节码生成来创建开销最小的快速映射器。
回答by Ivan
You can also try mapping framework based on Dozer, but with Excel mapping declaration. They've got some tools and additional cool features. Check at http://openl-tablets.sf.net/mapper
您也可以尝试基于 Dozer 的映射框架,但使用 Excel 映射声明。他们有一些工具和额外的很酷的功能。检查http://openl-tablets.sf.net/mapper
回答by Jonathan
ModelMapperis another library worth checking out. ModelMapper's design is different from other libraries in that it:
ModelMapper是另一个值得一试的库。ModelMapper 的设计与其他库的不同之处在于:
- Automatically maps object models by intelligently matching source and destination properties
- Provides a refactoring safe mapping API that uses actual code to map fields and methods rather than using strings
- Utilizes convention based configuration for simple handling of custom scenarios
- 通过智能匹配源和目标属性自动映射对象模型
- 提供重构安全映射 API,使用实际代码来映射字段和方法,而不是使用字符串
- 利用基于约定的配置来简单处理自定义场景
Check out the ModelMapper site for more info:
查看 ModelMapper 站点以获取更多信息:
回答by Alessandro
I suggest you try JMapper Framework.
我建议你试试JMapper 框架。
It is a Java bean to Java bean mapper, allows you to perform the passage of data dynamically with annotations and / or XML.
它是 Java bean 到 Java bean 的映射器,允许您使用注释和/或 XML 动态执行数据传递。
With JMapper you can:
使用 JMapper,您可以:
- Create and enrich target objects
- Apply a specific logic to the mapping
- Automatically manage the XML file
- Implement the 1 to N and N to 1 relationships
- Implement explicit conversions
- Apply inherited configurations
- 创建和丰富目标对象
- 将特定逻辑应用于映射
- 自动管理 XML 文件
- 实现 1 to N 和 N to 1 关系
- 实现显式转换
- 应用继承的配置