Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the conversion between VO and DTO in java

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Today, I would like to share with you the relevant knowledge of how to achieve the conversion between VO and DTO in java. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.

I. background

1. There are four types of entity classes in the domain model: VO, DTO, DO, and PO

Second, explain in detail

1.VO (View Object), the view object, is used in the presentation layer to encapsulate all the data of a given page (or component).

2.DTO (Data Transfer Object), data transfer object, this concept comes from the design pattern of J2EE, the original purpose is to provide coarse-grained data entities for EJB distributed applications, in order to reduce the number of distributed calls, so as to improve the performance of distributed calls and reduce the network load, but here, I generally refer to the data transfer objects used between the presentation layer and the service layer.

3.DO (Domain Object), domain object, is a tangible or intangible business entity abstracted from the real world.

4.PO (PersistentObject), the persistence object, forms an one-to-one mapping with the data structure of the persistence layer (usually a relational database). If the persistence layer is a relational database, then each field (or several) in the data table corresponds to one (or several) attributes of PO.

5. For example:

The backend returns a front-end object, which can be * VO.java.

The dao entity object generated by mybatis, which can be * PO.java

Typically, PO converts DO and then converts DTO to provide services for external invocation

Be careful

PO generally automatically generates and maps table fields one by one

DO is generally the same as many DO fields, but many, such as type fields, are enumerated, and in some projects, DO is a class ending with Model.

DTO, such as webservice interface (which provides external services), the returned result objects are all DTO. The definition of attributes in it is more elaborate, easy to understand and easy to expand. Whether the relationship between DTO and DTO is inclusive or horizontal must be clearly defined when designing, otherwise it will be a pit in the later stage.

VO is relatively simple, and the front end shows how to define what is needed.

III. Conversion between entity objects

A standardized project

The code is full of VO, From, DTO, DO and other pojo classes. The functions of these classes are different, but the attribute values in the classes are often exactly the same. When there are a lot of attributes, the set is indeed very tiring and time-consuming. In fact, the powerful Spring has already prepared intimate tools for us to deal with the previous 30 lines of code.

IV. Specific code examples of the first method

1. Import dependency

Org.springframework spring-beans 5.1.6.RELEASE

two。 A DTO (interface definition objects usually use DTO)

Public class TestDemoDTO {private String name; private Map citys; private Date gmtStart; private Boolean flag; private Integer age; private List types; public String getName () {return name;} public void setName (String name) {this.name = name;} public Map getCitys () {return citys;} public void setCitys (Map citys) {this.citys = citys } public Date getGmtStart () {return gmtStart;} public void setGmtStart (Date gmtStart) {this.gmtStart = gmtStart;} public Boolean getFlag () {return flag;} public void setFlag (Boolean flag) {this.flag = flag;} public Integer getAge () {return age;} public void setAge (Integer age) {this.age = age } public List getTypes () {return types;} public void setTypes (List types) {this.types = types } @ Override public String toString () {return "TestDemoDTO {" + "name='" + name +''+ ", citys=" + citys + ", gmtStart=" + gmtStart + ", flag=" + flag + ", age=" + age + " Types= "+ types +'}' }}

3. A From (the front-end input parameter defines From)

Public class TestDemoFrom {private String name; private Map citys; private Date gmtStart; private Boolean flag; private Integer age; private List types; public String getName () {return name;} public void setName (String name) {this.name = name;} public Map getCitys () {return citys;} public void setCitys (Map citys) {this.citys = citys } public Date getGmtStart () {return gmtStart;} public void setGmtStart (Date gmtStart) {this.gmtStart = gmtStart;} public Boolean getFlag () {return flag;} public void setFlag (Boolean flag) {this.flag = flag;} public Integer getAge () {return age;} public void setAge (Integer age) {this.age = age } public List getTypes () {return types;} public void setTypes (List types) {this.types = types } @ Override public String toString () {return "TestDemoFrom {" + "name='" + name +''+ ", citys=" + citys + ", gmtStart=" + gmtStart + ", flag=" + flag + ", age=" + age + " Types= "+ types +'}' }}

4. Test class

Public class TestDemo {public static void main (String [] args) {TestDemoFrom testDemoFrom = new TestDemoFrom (); HashMap map = Maps.newHashMap (); map.put ("001"," Beijing "); map.put (" 002", "Shanghai"); map.put ("003"," Guangzhou "); testDemoFrom.setCitys (map); testDemoFrom.setFlag (true) TestDemoFrom.setGmtStart (new Date ()); testDemoFrom.setName (promise); testDemoFrom.setAge (18); testDemoFrom.setTypes (Lists.newArrayList ("I love", "baby")); System.out.printf (testDemoFrom.toString () + "\ n"); / / you can start the conversion below, just one line of code TestDemoDTO testDemoDTO = new TestDemoDTO (); BeanUtils.copyProperties (testDemoFrom,testDemoDTO) System.out.println (testDemoDTO.toString ());}}

5. Result

TestDemoFrom {name=' promise', citys= {001 = Beijing, 002 = Shanghai, 003 = Guangzhou}, gmtStart=Fri May 10 22:37:53 CST 2019, flag=true, age=18, types= [I love, baby]}

TestDemoDTO {name=' promise', citys= {001 = Beijing, 002 = Shanghai, 003 = Guangzhou}, gmtStart=Fri May 10 22:37:53 CST 2019, flag=true, age=18, types= [I love, baby]}

6. Be careful

Pay attention to assignment failure

If the attribute name is different, the value cannot be assigned.

If the type is different, the same name will not be assigned.

5. Specific code examples of the second method (using dozer)

1. Import dependency

Net.sf.dozer dozer 5.4.0

two。 Test class

Public class TestDemoDozer {@ Test public void test () {DozerBeanMapper mapper = new DozerBeanMapper (); TestDemoFrom testDemoFrom = new TestDemoFrom (); HashMap map = Maps.newHashMap (); map.put ("001"," Beijing "); map.put (" 002", "Shanghai"); map.put ("003"," Guangzhou "); testDemoFrom.setCitys (map); testDemoFrom.setFlag (true) TestDemoFrom.setGmtStart (new Date ()); testDemoFrom.setName (promise); testDemoFrom.setAge (18); testDemoFrom.setTypes (Lists.newArrayList ("I love", "baby"); System.out.printf (testDemoFrom.toString () + "\ n"); / / convert TestDemoDTO convert = mapper.map (testDemoFrom,TestDemoDTO.class); System.out.println (convert);}}

3. Result

TestDemoFrom {name=' promise', citys= {001 = Beijing, 002 = Shanghai, 003 = Guangzhou}, gmtStart=Sat May 11 00:30:02 CST 2019, flag=true, age=18, types= [I love, baby]}

TestDemoDTO {name=' promise', citys= {001 = Beijing, 002 = Shanghai, 003 = Guangzhou}, gmtStart=Sat May 11 00:30:02 CST 2019, flag=true, age=18, types= [I love, baby]}

These are all the contents of this article entitled "how to achieve the conversion between VO and DTO in java". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report