日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/3 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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.AddressType和com.blogspot.marxsoftware.PersonType的成本清單,以及重命名的自定義數據類dustin.examples.orikademo.Address和dustin.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 ,就可以輕松地從一個對象復制到另一個對象,并且兩個方向都在copyPersonTypeFromPerson和copyPersonFromPersonType方法中進行了描述。

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 映射非空字段

總結

以上是生活随笔為你收集整理的orika 映射非空字段_Orika:将JAXB对象映射到业务/域对象的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 亚洲熟妇无码一区二区三区导航 | 亚洲综合五月天婷婷丁香 | 青青草这里只有精品 | 中国美女一级看片 | 久久人人爽人人爽人人片av免费 | 免费成人美女在线观看 | 久久偷拍免费视频 | 在线v | 成年人在线视频 | 午夜777| 日本免费爱爱视频 | 在线免费91 | 国产一区二区在线视频观看 | 1024金沙人妻一区二区三区 | 国产黄色在线播放 | 亚洲成人77777| 男女裸体无遮挡做爰 | 狼人综合网 | 男女激情实录 | 婷婷丁香久久 | 爱情岛亚洲论坛入口福利 | 久久观看 | 中文字幕免费高清在线 | 欧美精品久久久久a | 人人爽人人爽人人片 | 欧美成年人视频 | 中文字幕91视频 | 少妇欧美激情一区二区三区 | 特黄1级潘金莲 | 丝袜一级片 | 中文字幕一区二区三区乱码人妻 | 男生脱女生衣服 | 欧美13p| 喷水视频在线观看 | 亚洲色图 美腿丝袜 | 国产黄色在线网站 | 影音先锋亚洲成aⅴ人在 | 久艹在线播放 | 国产成a人亚洲精v品无码 | 冲田杏梨一区二区三区 | 91免费版视频 | 亚洲黄色小说图片 | 亚洲一级影院 | 国产成人无码一区二区三区在线 | 亚洲 欧美 变态 另类 综合 | 成人免费观看网站 | 1024手机在线观看 | 欧美一本 | 日本欧美一区二区三区 | 国产av剧情一区 | 亚洲综合小说网 | 国产乱人伦app精品久久 | 国产视频久久久久 | 美女网站在线 | 久久精品国产99国产 | 国产美女视频91 | 国产精品免费视频一区二区 | 美女av免费在线观看 | va婷婷在线免费观看 | 亚洲一区二区三区麻豆 | 和美女啪啪 | 你懂得在线视频 | 少妇特黄一区二区三区 | 久久av一区 | 99久久婷婷国产综合精品青牛牛 | 欧美精品手机在线 | 精品一区二区毛片 | 国内av在线 | 日本久久高清视频 | 日韩欧美123 | 天天色综合天天 | 天天射日日干 | 成人性生交视频免费观看 | 欧美aaa视频 | 欧美不卡网 | 大胸喷奶水www视频妖精网站 | 亚洲一线二线在线观看 | 九色视频网站 | 嫩草视频在线看 | 日本不卡1| 久草视频免费在线 | 色噜噜狠狠一区二区三区牛牛影视 | 北京富婆泄欲对白 | 日本黄色片视频 | 嫩草嫩草嫩草嫩草嫩草嫩草 | 精品视频网 | 国产卡一卡二卡三 | 欧美久久免费 | 中文字幕韩日 | 色午夜视频 | 中文字幕日韩视频 | 99在线精品免费视频 | 午夜精品一区二区三区免费视频 | 欧美日韩毛片 | 国产成人超碰人人澡人人澡 | 中国毛片视频 | 在线看av的网址 | 欧美精品h| 激情xxx |