orika 映射非空字段_Orika:将JAXB对象映射到业务/域对象

orika 映射非空字段

这篇文章着眼于使用Orika将JAXB对象映射到业务域对象。 本月初, 我使用基于反射的Dozer讨论 了相同的映射用例 。 在本文中,我假设需要映射相同的示例类,但是它们将使用Orika而不是Dozer进行映射 。

Dozer和Orika旨在解决相同类型的问题:两个“数据”对象的自动映射,这些对象不共享公共继承,但表示相同的数据字段。 推土机使用反射来完成此操作,而Orika使用反射和字节码操作来完成此操作。 Orika的口号是“更简单,更轻便,更快的Java bean映射”。

Orika拥有版本2的Apache许可证,可以从https://github.com/orika-mapper/orika/archive/master.zip (源)或http://search.maven.org/#search下载。 | ga | 1 | orika (二进制)。 Orika对Javassist (用于字节码操作), SLF4J和paranamer (用于在运行时访问方法/构造函数参数名称)具有依赖性 。 这三个依赖项中的两个(JavaAssist和paranamer而不是SLF4J)捆绑在orika-core-1.4.4-deps-included.jar 。 如果依赖项已经可用,则可以使用更薄的orika-core-1.4.4.jar 。 就像这些JAR的名称所暗示的那样,在本文中,我使用Orika 1.4.4作为示例。

在我的“ 推土机:将JAXB对象映射到业务/域对象”一文中 ,我讨论了通常不希望将JAXB生成的类的实例用作业务或域对象的原因。 然后,我展示了JAXB生成的类和自定义数据类之间的“传统”映射方式,以便可以在业务域数据对象中的整个应用程序中传递数据。 在本文中,我将使用相同的方法,但是使用Orika进行映射,而不是执行自定义映射或使用Dozer进行映射。 为方便起见,我在此处列出了JAXB生成的类com.blogspot.marxsoftware.AddressTypecom.blogspot.marxsoftware.PersonType的成本清单,以及重命名的自定义数据类dustin.examples.orikademo.Addressdustin.examples.orikademo.Person

JAXB生成的AddressType.java

//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.4-2 
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
// Any modifications to this file will be lost upon recompilation of the source schema. 
// Generated on: 2013.12.03 at 11:44:32 PM MST 
//package com.blogspot.marxsoftware;import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlType;/*** <p>Java class for AddressType complex type.* * <p>The following schema fragment specifies the expected content contained within this class.* * <pre>* <complexType name="AddressType">*   <complexContent>*     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">*       <attribute name="streetAddress1" use="required" type="{http://www.w3.org/2001/XMLSchema}string" />*       <attribute name="streetAddress2" type="{http://www.w3.org/2001/XMLSchema}string" />*       <attribute name="city" use="required" type="{http://www.w3.org/2001/XMLSchema}string" />*       <attribute name="state" use="required" type="{http://www.w3.org/2001/XMLSchema}string" />*       <attribute name="zipcode" use="required" type="{http://www.w3.org/2001/XMLSchema}string" />*     </restriction>*   </complexContent>* </complexType>* </pre>* * */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "AddressType")
public class AddressType {@XmlAttribute(name = "streetAddress1", required = true)protected String streetAddress1;@XmlAttribute(name = "streetAddress2")protected String streetAddress2;@XmlAttribute(name = "city", required = true)protected String city;@XmlAttribute(name = "state", required = true)protected String state;@XmlAttribute(name = "zipcode", required = true)protected String zipcode;/*** Gets the value of the streetAddress1 property.* * @return*     possible object is*     {@link String }*     */public String getStreetAddress1() {return streetAddress1;}/*** Sets the value of the streetAddress1 property.* * @param value*     allowed object is*     {@link String }*     */public void setStreetAddress1(String value) {this.streetAddress1 = value;}/*** Gets the value of the streetAddress2 property.* * @return*     possible object is*     {@link String }*     */public String getStreetAddress2() {return streetAddress2;}/*** Sets the value of the streetAddress2 property.* * @param value*     allowed object is*     {@link String }*     */public void setStreetAddress2(String value) {this.streetAddress2 = value;}/*** Gets the value of the city property.* * @return*     possible object is*     {@link String }*     */public String getCity() {return city;}/*** Sets the value of the city property.* * @param value*     allowed object is*     {@link String }*     */public void setCity(String value) {this.city = value;}/*** Gets the value of the state property.* * @return*     possible object is*     {@link String }*     */public String getState() {return state;}/*** Sets the value of the state property.* * @param value*     allowed object is*     {@link String }*     */public void setState(String value) {this.state = value;}/*** Gets the value of the zipcode property.* * @return*     possible object is*     {@link String }*     */public String getZipcode() {return zipcode;}/*** Sets the value of the zipcode property.* * @param value*     allowed object is*     {@link String }*     */public void setZipcode(String value) {this.zipcode = value;}}

JAXB生成的PersonType.java

//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.4-2 
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
// Any modifications to this file will be lost upon recompilation of the source schema. 
// Generated on: 2013.12.03 at 11:44:32 PM MST 
//package com.blogspot.marxsoftware;import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;/*** <p>Java class for PersonType complex type.* * <p>The following schema fragment specifies the expected content contained within this class.* * <pre>* <complexType name="PersonType">*   <complexContent>*     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">*       <sequence>*         <element name="MailingAddress" type="{http://marxsoftware.blogspot.com/}AddressType"/>*         <element name="ResidentialAddress" type="{http://marxsoftware.blogspot.com/}AddressType" minOccurs="0"/>*       </sequence>*       <attribute name="firstName" type="{http://www.w3.org/2001/XMLSchema}string" />*       <attribute name="lastName" type="{http://www.w3.org/2001/XMLSchema}string" />*     </restriction>*   </complexContent>* </complexType>* </pre>* * */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "PersonType", propOrder = {"mailingAddress","residentialAddress"
})
public class PersonType {@XmlElement(name = "MailingAddress", required = true)protected AddressType mailingAddress;@XmlElement(name = "ResidentialAddress")protected AddressType residentialAddress;@XmlAttribute(name = "firstName")protected String firstName;@XmlAttribute(name = "lastName")protected String lastName;/*** Gets the value of the mailingAddress property.* * @return*     possible object is*     {@link AddressType }*     */public AddressType getMailingAddress() {return mailingAddress;}/*** Sets the value of the mailingAddress property.* * @param value*     allowed object is*     {@link AddressType }*     */public void setMailingAddress(AddressType value) {this.mailingAddress = value;}/*** Gets the value of the residentialAddress property.* * @return*     possible object is*     {@link AddressType }*     */public AddressType getResidentialAddress() {return residentialAddress;}/*** Sets the value of the residentialAddress property.* * @param value*     allowed object is*     {@link AddressType }*     */public void setResidentialAddress(AddressType value) {this.residentialAddress = value;}/*** Gets the value of the firstName property.* * @return*     possible object is*     {@link String }*     */public String getFirstName() {return firstName;}/*** Sets the value of the firstName property.* * @param value*     allowed object is*     {@link String }*     */public void setFirstName(String value) {this.firstName = value;}/*** Gets the value of the lastName property.* * @return*     possible object is*     {@link String }*     */public String getLastName() {return lastName;}/*** Sets the value of the lastName property.* * @param value*     allowed object is*     {@link String }*     */public void setLastName(String value) {this.lastName = value;}}

域/业务类Address.java

package dustin.examples.orikademo;import java.util.Objects;/*** Address class.* * @author Dustin*/
public class Address
{private String streetAddress1;private String streetAddress2;private String municipality;private String state;private String zipCode;public Address() {}public Address(final String newStreetAddress1,final String newStreetAddress2,final String newMunicipality,final String newState,final String newZipCode){this.streetAddress1 = newStreetAddress1;this.streetAddress2 = newStreetAddress2;this.municipality = newMunicipality;this.state = newState;this.zipCode = newZipCode;}public String getStreetAddress1(){return this.streetAddress1;}public void setStreetAddress1(String streetAddress1){this.streetAddress1 = streetAddress1;}public String getStreetAddress2(){return this.streetAddress2;}public void setStreetAddress2(String streetAddress2){this.streetAddress2 = streetAddress2;}public String getMunicipality(){return this.municipality;}public void setMunicipality(String municipality){this.municipality = municipality;}public String getState() {return this.state;}public void setState(String state){this.state = state;}public String getZipCode() {return this.zipCode;}public void setZipCode(String zipCode){this.zipCode = zipCode;}@Overridepublic int hashCode(){return Objects.hash(this.streetAddress1, this.streetAddress2, this.municipality,this.state, this.zipCode);}@Overridepublic boolean equals(Object obj){if (obj == null) {return false;}if (getClass() != obj.getClass()) {return false;}final Address other = (Address) obj;if (!Objects.equals(this.streetAddress1, other.streetAddress1)){return false;}if (!Objects.equals(this.streetAddress2, other.streetAddress2)){return false;}if (!Objects.equals(this.municipality, other.municipality)){return false;}if (!Objects.equals(this.state, other.state)){return false;}if (!Objects.equals(this.zipCode, other.zipCode)){return false;}return true;}@Overridepublic String toString(){return "Address{" + "streetAddress1=" + streetAddress1 + ", streetAddress2="+ streetAddress2 + ", municipality=" + municipality + ", state=" + state+ ", zipCode=" + zipCode + '}';}}

域/业务类Person.java

package dustin.examples.orikademo;import java.util.Objects;/*** Person class.* * @author Dustin*/
public class Person
{private String lastName;private String firstName;private Address mailingAddress;private Address residentialAddress;public Person() {}public Person(final String newLastName,final String newFirstName,final Address newResidentialAddress,final Address newMailingAddress){this.lastName = newLastName;this.firstName = newFirstName;this.residentialAddress = newResidentialAddress;this.mailingAddress = newMailingAddress;}public String getLastName(){return this.lastName;}public void setLastName(String lastName) {this.lastName = lastName;}public String getFirstName(){return this.firstName;}public void setFirstName(String firstName){this.firstName = firstName;}public Address getMailingAddress(){return this.mailingAddress;}public void setMailingAddress(Address mailingAddress){this.mailingAddress = mailingAddress;}public Address getResidentialAddress(){return this.residentialAddress;}public void setResidentialAddress(Address residentialAddress){this.residentialAddress = residentialAddress;}@Overridepublic int hashCode(){int hash = 3;hash = 19 * hash + Objects.hashCode(this.lastName);hash = 19 * hash + Objects.hashCode(this.firstName);hash = 19 * hash + Objects.hashCode(this.mailingAddress);hash = 19 * hash + Objects.hashCode(this.residentialAddress);return hash;}@Overridepublic boolean equals(Object obj){if (obj == null){return false;}if (getClass() != obj.getClass()){return false;}final Person other = (Person) obj;if (!Objects.equals(this.lastName, other.lastName)){return false;}if (!Objects.equals(this.firstName, other.firstName)){return false;}if (!Objects.equals(this.mailingAddress, other.mailingAddress)){return false;}if (!Objects.equals(this.residentialAddress, other.residentialAddress)){return false;}return true;}@Overridepublic String toString() {return  "Person{" + "lastName=" + lastName + ", firstName=" + firstName+ ", mailingAddress=" + mailingAddress + ", residentialAddress="+ residentialAddress + '}';}}

与Dozer一样,要映射的类需要具有无参数的构造函数以及“ set”和“ get”方法以支持双向转换,而无需任何特殊的附加配置。 另外,与Dozer一样,Orika会自动映射同名字段,并易于配置异常的映射(名称不匹配的字段)。 下一个代码清单(针对我称为OrikaPersonConverter的类)演示了OrikaPersonConverter MapperFactory的实例化和配置,以默认情况下映射大多数字段,并通过显式映射来映射名称彼此不同的字段 (“市政”和“城市”)组态。 一旦配置了MapperFactory ,就可以轻松地从一个对象复制到另一个对象,并且两个方向都在copyPersonTypeFromPersoncopyPersonFromPersonType方法中进行了描述。

OrikaPersonConverter

package dustin.examples.orikademo;import com.blogspot.marxsoftware.AddressType;
import com.blogspot.marxsoftware.PersonType;
import ma.glasnost.orika.MapperFacade;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.impl.DefaultMapperFactory;/*** Convert between instances of {@link com.blogspot.marxsoftware.PersonType}* and {@link dustin.examples.orikademo.Person}.* * @author Dustin*/
public class OrikaPersonConverter
{/** Orika Mapper Facade. */private final static MapperFacade mapper;static{final MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();mapperFactory.classMap(Address.class, AddressType.class).field("municipality", "city").byDefault().register();mapper = mapperFactory.getMapperFacade();}/** No-arguments constructor. */public OrikaPersonConverter() {}/*** Provide an instance of {@link com.blogspot.marxsoftware.PersonType}* that corresponds with provided {@link dustin.examples.orikademo.Person} as* mapped by Dozer Mapper.* * @param person Instance of {@link dustin.examples.orikademo.Person} from which*    {@link com.blogspot.marxsoftware.PersonType} will be extracted.* @return Instance of {@link com.blogspot.marxsoftware.PersonType} that*    is based on provided {@link dustin.examples.orikademo.Person} instance.*/public PersonType copyPersonTypeFromPerson(final Person person){PersonType personType = mapper.map(person, PersonType.class);return personType;}/*** Provide an instance of {@link dustin.examples.orikademo.Person} that corresponds* with the provided {@link com.blogspot.marxsoftware.PersonType} as * mapped by Dozer Mapper.* * @param personType Instance of {@link com.blogspot.marxsoftware.PersonType}*    from which {@link dustin.examples.orikademo.Person} will be extracted.* @return Instance of {@link dustin.examples.orikademo.Person} that is based on the*    provided {@link com.blogspot.marxsoftware.PersonType}.*/public Person copyPersonFromPersonType(final PersonType personType){Person person = mapper.map(personType, Person.class);return person;}
}

与Dozer的情况一样,两个类之间的映射是双向的,因此只需进行一次映射,并将应用于从一个对象到另一个对象的复制。

结论

像推土机一样,Orika提供的自定义性和灵活性比本文中演示的要好得多。 但是,对于相对简单的映射(在使用JAXB生成的对象的应用程序中很常见),Orika非常易于使用。 《 Orika用户指南》是了解更多有关Orika的好资源。

参考: Orika:来自JCG合作伙伴 Dustin Marx在Inspired by Actual Events博客上将JAXB对象映射到业务/域对象 。

翻译自: https://www.javacodegeeks.com/2013/12/orika-mapping-jaxb-objects-to-businessdomain-objects.html

orika 映射非空字段

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/344880.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【渝粤教育】广东开放大学 应用创意写作 形成性考核 (54)

选择题 题目&#xff1a;《四库全书》将图书分为 &#xff08; &#xff09;四部。 题目&#xff1a;“五经”指 &#xff08; &#xff09;。 题目&#xff1a;汉代 &#xff08; &#xff09;在《论六家要旨》中&#xff0c;重点对春秋战国时诸子百家中的儒、墨、名、法、道、…

【渝粤教育】广东开放大学 教育心理学 形成性考核 (42)

选择题 题目&#xff1a;认知过程不包括以下哪种要素&#xff08;&#xff09; 题目&#xff1a;下列说法中关于动机的说法中正确的是&#xff08;&#xff09; 题目&#xff1a;关于感觉和知觉下列说法中错误的是 &#xff08; &#xff09; 题目&#xff1a;下列关于意识和无…

使用Maven将文件上传和下载到S3

多年来&#xff0c;我已经看到许多团队以许多不同的方式使用Maven。 Maven可用于许多ci / cd任务&#xff0c;而无需使用额外的管道代码&#xff0c;或者可用于在运行某些测试之前准备开发环境。 通常&#xff0c;它是一种方便的工具&#xff0c;在Java团队中广泛使用&#xf…

html怎么把一段文字设置为连接到下一个网页的按钮,网页设计三合一模拟试题(一)...

网页设计三合一模拟试题一、选择题&#xff1a;(20小题&#xff0c;每题2分&#xff0c;共40分)1、以下超链接到电子邮件的正确格式是&#xff1a;()A. maiil to://abchttp://www.doczj.com/doc/e97fe8f9b9f67c1cfad6195f312b3169a451eaf7.htmlB. mail to: abchttp://www.doczj…

【渝粤教育】广东开放大学 演讲与口才 形成性考核 (1)

题库查询系统 选择题 题目&#xff1a;口才是人际交流的润滑剂。 答案&#xff1a; A、对 题目&#xff1a;说话的水平决定了沟通的成败。 答案&#xff1a; A、对 题目&#xff1a;能说话≠有口才。 答案&#xff1a; A、对 题目&#xff1a;会说话≠有口才。 答案&#xff1a…

怎么删除计算机管理员用户密码,小编手把手教你Win10系统如何删除管理员账户密码...

一位用户反馈自己在windows10正式版系统电脑中设置了管理员账户密码&#xff0c;之后每次开机都需要输入密码才可以登录&#xff0c;感觉十分麻烦。那么&#xff0c;Win10系统下该如何删除管理员账户密码&#xff1f;接下来&#xff0c;系统之家小编就为大家分享下具体操作方法…

【渝粤教育】广东开放大学 质量认证认可 形成性考核 (31)

题库查询系统 选择题 题目&#xff1a; 以下哪个认证标志成为世界上第一个受法律保护的认证标志。&#xff08;&#xff09; 正确答案&#xff1a; 答案&#xff1a; A、 题目&#xff1a; &#xff08;&#xff09;以下哪个标志是国家认监委的标志? 正确答案&#xff1a; 答…

【渝粤题库】广东开放大学 机械设计 形成性考核

选择题 题目&#xff1a;当四杆机构处于死点位置时&#xff0c;机构的压力角为。 答案&#xff1a; A、为90 B、与构件尺寸有关 C、为0 题目&#xff1a;( )能把转动运动转换成往复直线运动&#xff0c;也可以把往复直线运动转换成转动运动。 答案&#xff1a; A、曲柄摇杆…

Hibernate事实:始终检查Criteria API SQL查询

Criteria API对于动态构建查询非常有用&#xff0c;但这是我使用它的唯一用例。 每当您有一个带有N个过滤器且可以以任意M个组合到达的UI时&#xff0c;就应该有一个API动态地构造查询&#xff0c;因为连接字符串始终是我所不愿使用的路径。 问题是&#xff0c;您是否知道您的…

计算机教室 使用计划,计算机室教学计划

着时代进步与社会的快速发展,专业的计算机人才成为社会所稀缺的资源,因而计算机教学也逐渐受到学校的关注和重视&#xff0c;他&#xff0c;如何做好计算机教学工作呢?下面是小编分享给大家的&#xff0c;希望对大家有帮助。一、班级学生情况分析1、基本情况&#xff1a;计算机…

【渝粤题库】广东开放大学 跨境电商搜索引擎优化 形成性考核

选择题 题目&#xff1a;搜索引擎除了覆盖范围广&#xff0c;还有什么优势 &#xff08; &#xff09; 答案&#xff1a; A、使用方便 B、操作简单 C、收入较多 D、针对性强 题目&#xff1a;以下哪一个不属于国外经常使用搜索引擎&#xff08; &#xff09; 答案&#xf…

以Spring方式构建企业Java应用程序

我认为可以肯定地说&#xff0c; Java EE在Java开发人员中享有很高的声誉。 尽管多年来确实在各个方面都有所改善&#xff0c;甚至将其改名为Eclipse Foundation成为Jakarta EE &#xff0c;但其苦味仍然相当浓厚。 另一方面&#xff0c;我们拥有Spring框架 &#xff08;或者为…

【渝粤题库】陕西师范大学180113 学前儿童艺术教育作业

一、简答题 1.简述学前儿童歌唱活动&#xff08;歌词方面&#xff09;如何选材&#xff1f; 2.简述罗恩菲尔德的儿童画发展阶段。 3.简述傣族民间舞的风格特点&#xff1f; 4.学前儿童美术教育的任务是什么&#xff1f; 5. 音乐的基本特征是什么&#xff1f; 6.柯思修泰纳划分的…

jq设置html的fontsize,Jquery 设置字体大小(font-size)与行高(line-height)

Jquery 设置字体大小(font-size)与行高(line-height)var cssfontSize$(".txt_container").css(font-size);var csslineHeight$(".txt_container").css(line-height);var unitcssfontSize.slice(-2);var fontSizeparseFloat(cssfontSize);var lineHeightpar…

【渝粤题库】陕西师范大学200431综合英语(一)作业(高起专、高起本)

《综合英语&#xff08;一&#xff09;》作业 I. Multiple Choice. Choose the appropriate explanation of the underlined words. Mr. Brown is the principal of a British school. A. main B. headmaster C. lawThe doctor thought the patient was really bullheaded. A.…

【渝粤题库】陕西师范大学201001 教育管理学(高起本)作业

《教育管理学》作业 一、单项选择题 1、管理的二重性是指( ) A&#xff0e;科学性与艺术性 B&#xff0e;开放性与封闭性 C&#xff0e;政治性与非政治性 D&#xff0e;规范性与灵活性 2、管理总是要经历一个过程&#xff0c;尽管不同管理情境中经历的过程不完全相同&#xff0…

计算机网络发展第二阶段 兴起于,计算机辅助开始于计算机发展第几阶段

计算机辅助开始于计算机发展第4阶段。其计算机辅助管理发展的四个阶段分别如下&#xff1a;1、单项数据处理阶段&#xff1a;这是计算机应用于管理的初级阶段&#xff0c;主要用于处理工资计算、统计报表、发放凭证等部分的事务工作&#xff0c;而原始数据的收集以及对输出结果…

【渝粤题库】陕西师范大学202011 微观经济学 作业 (专升本、高起本)

《微观经济学》作业 一、填空题&#xff1a; &#xff11;&#xff0e;实证经济学要回答的是 的问题&#xff0c;规范经济学要回答的是 的问题。 &#xff12;&#xff0e;需求变动引起均衡价格 变动&#xff0c;供给变动引起均衡价格 变动。 &#xff13;&#xff0e;边际收益…

jaxb 映射 空字段_推土机:将JAXB对象映射到业务/域对象

jaxb 映射 空字段Dozer是开放源代码&#xff08; Apache 2许可 &#xff09;“ Java Bean到Java Bean映射器&#xff0c;可将数据从一个对象递归复制到另一个对象”。 正如从其主页上的描述所描述的那样&#xff0c;它用于映射两个JavaBeans实例&#xff0c;以在实例之间进行自…

【渝粤题库】陕西师范大学202881 电子商务概论

《电子商务概论》作业 一、判断题 1、CPU中运算器的主要功能是完成对数据的算术运算、逻辑运算和逻辑判断等操作。&#xff08; &#xff09; 2、与传统的支付方式相比&#xff0c;电子支付具有方便、快捷、高效、经济、安全的优势。&#xff08; &#xff09; 3、在面向终端的…