Java 映射:Selma 与 MapStruct
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34786737/
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
Java mapping: Selma vs MapStruct
提问by HymanDev
Currently there are two main popular Java Object to Object mapping frameworks that supersede Dozer (http://dozer.sourceforge.net/documentation/mappings.html), they are:
目前有两个主要流行的 Java Object to Object 映射框架取代 Dozer ( http://dozer.sourceforge.net/documentation/mappings.html),它们是:
- Selma - http://www.selma-java.org/
- MapStruct - http://mapstruct.org/
- 塞尔玛 - http://www.selma-java.org/
- MapStruct - http://mapstruct.org/
With the exception of this page (http://vytas.io/blog/java/java-object-to-object-mapping-which-framework-to-choose-part-2/) I haven't been able to find much online regarding which framework is better than the other, or under what circumstances they are better. Wondering if anyone you can shed some light on this. In terms of functionality based on the documents, they seem to be doing the same thing.
除了这个页面(http://vytas.io/blog/java/java-object-to-object-mapping-which-framework-to-choose-part-2/)我没能找到关于哪个框架比另一个更好,或者在什么情况下它们更好的网上很多。想知道是否有人可以对此有所了解。在基于文档的功能方面,它们似乎在做同样的事情。
回答by Gunnar
(Original author of MapStruct here, so naturally I am biased)
(MapStruct的原作者在这里,所以自然是有偏见的)
Indeed, both projects are based on the same general idea of generating mapping code at compile time; I recommend you MapStruct for the following reasons:
实际上,这两个项目都基于在编译时生成映射代码的相同总体思路;我推荐你 MapStruct 的原因如下:
- Proven and stable codebase: MapStruct is the older of the two, coming up with the idea of mapping generation originally. It has been enhanced and polished over quite a long time, based on real-world feedback from usage in many different projects; We released the stable 1.0 Final last year
- Larger developer and user community as per the number of committers (MapStruct, Selma) and user questions (MapStruct, Selma)
- Feature-rich (Some things supported in MapStruct I didn't find (to the same extend) in the Selma docs):
- Many built-in type conversions, including advanced support for JAXB types such as
JAXBElement
- Support for default values and constants
- Mapping customizations through inline expressions
- Sharing configurationsacross mappers
- Nicely integrates with CDI and JSR 330(in addition to Spring)
- Many built-in type conversions, including advanced support for JAXB types such as
- Eclipse plug-inavaible: Still work in progress, but its quickfixes and auto-completions are already very helpful when designing mapper interfaces
- IntelliJ plug-in: helps when editing mapper interfaces via auto-completion, go to referenced properties, refactoring support etc.
- 经过验证且稳定的代码库:MapStruct 是两者中较旧的,最初提出了映射生成的想法。基于在许多不同项目中使用的实际反馈,它经过了很长时间的增强和完善;我们去年发布了稳定版 1.0 Final
- 根据提交者(MapStruct,Selma)和用户问题(MapStruct,Selma)的数量,更大的开发人员和用户社区
- 功能丰富(我在 Selma 文档中没有找到(同样扩展)MapStruct 中支持的某些内容):
- Eclipse 插件可用:仍在开发中,但它的快速修复和自动完成在设计映射器界面时已经非常有用
- IntelliJ 插件:帮助通过自动完成编辑映射器界面、转到引用的属性、重构支持等。
回答by slemesle
(Original author of Selma so slight different point of view)
(原作者塞尔玛所以观点略有不同)
Selma and MapStruct does the same job with some differences. First it appears that Selma generated code is just a bit faster than MapStruct (http://javaetmoi.com/wp-content/uploads/2015/09/2015-09-mapping-objet-objet2.png). The 0.13 release number does not really reflects the maturity of code Selma is stable and robust it is in use in production for 2 years.
Selma 和 MapStruct 做同样的工作,但有一些不同。首先看起来 Selma 生成的代码比 MapStruct ( http://javaetmoi.com/wp-content/uploads/2015/09/2015-09-mapping-objet-objet2.png)快一点。0.13 版本号并没有真正反映代码的成熟度 Selma 是稳定和健壮的,它在生产中使用了 2 年。
The main idea behind Selma is to prohibit magic conversion and just automate all mappings without any side effects. When mapping appears to be too complex, the developer should handle it by himself using custom mappings or interceptor.
Selma 背后的主要思想是禁止魔法转换,只是自动化所有映射,没有任何副作用。当映射看起来过于复杂时,开发人员应该使用自定义映射或拦截器自己处理。
The footprint of Selma is built to be as small as possible we only depend on a JavaWriter and the JDK.
Selma 的足迹被构建得尽可能小,我们只依赖于 JavaWriter 和 JDK。
Selma tries to only use static compiled generated code without any reflection at runtime or pseudo-code written in string fields.
Selma 尝试只使用静态编译生成的代码,在运行时没有任何反射或在字符串字段中编写的伪代码。
You can use composition to build a chain of mappers and inside a single mapper you can have global configuration that can be overwritten on a per method basis.
您可以使用组合来构建映射器链,并且在单个映射器中您可以拥有可以在每个方法的基础上覆盖的全局配置。
Compiler messages are built to give developer early feedback, tips to solve the issue and learn the API.
编译器消息旨在为开发人员提供早期反馈、解决问题和学习 API 的技巧。
At the end for sure MapStruct is more feature rich but Selma gives developer all the tools needed for complex mapping with the responsibility of writing the business logic. You could also find one of the 2 APIs nicer than the other from a user perspective so best thing to do is to try both and choose the one you feel more comfortable with. It won't be time consuming.
最后肯定 MapStruct 功能更丰富,但 Selma 为开发人员提供了复杂映射所需的所有工具,并负责编写业务逻辑。从用户的角度来看,您还可以发现这 2 个 API 中的一个比另一个更好,所以最好的办法是尝试这两个 API,然后选择您觉得更舒服的一个。不会很费时间。