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 integrate Dozer with Springboot to achieve Deep replication

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how Springboot integrates Dozer to achieve deep replication. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Dozer

Dozer is a Java Bean to Java Bean mapper that recursively copies data from one object to another. It is a powerful, general, flexible, reusable and configurable open source mapping framework.

Commonly used for:

Javabean conversion between code layers, such as dao layer PO to front-end VO

In distributed, DAO layer PO to DTO, DO and web layer DTO to VO

Scenarios to note:

Due to the deep replication between bean, when doing some similar updates, we should pay special attention to whether some key fields of PO, such as ID, are really needed. Scenario: the incoming DTO An is copied by the checked DTO B. at this time, there will be B's ID in A, which is likely to cause primary key conflicts when inserting A.

Recommendations:

It's best not to use Dozer. Dozer brings performance overhead. This is impossible.)

Some special operations can be empty by using section control special fields.

SpringBoot Integration Dozerjar dependency introduction

Pom.xml adds the following dependencies

Net.sf.dozer dozer-spring 5.5.1 net.sf.dozer dozer 5.5.1dozer configuration xml introduction

Create a new dozer folder under the resource folder, and create a new bean-mappings.xml, global-configuration.xml

Bean-mappings.xml

Global-configuration.xml

JavaConfig of yyyy-MM-dd HH:mm:ss true false java.lang.Enum java.lang.Integer Dozer

Configuration for SpringBoot to find DozerBeanMapperFactoryBean

Create a new DozerMapperConfig.java

Public class DozerMapperConfig {@ Bean public DozerBeanMapperFactoryBean dozerBeanMapperFactoryBean (@ Value ("classpath*:dozer/*.xml") Resource [] resources) throws Exception {final DozerBeanMapperFactoryBean dozerBeanMapperFactoryBean = new DozerBeanMapperFactoryBean (); dozerBeanMapperFactoryBean.setMappingFiles (resources); return dozerBeanMapperFactoryBean;}} format factory

It is recommended to create a new package to specifically place Dozer tools.

API definition: new IGenerator.java API

Public interface IGenerator {/ * @ Description: deep replication and type conversion of a single object, vo/domain, po * @ param s data object * @ param clz replication target type * @ return * @ author banjuer@outlook.com * @ Time May 9, 2018 3:53:24 / T convert (S, Class clz) / * * @ Description: deep copy result set (ResultSet is a custom paged result set) * @ param s data object ResultSet convert (s, Class clz); / * * @ Description: list deep replication * @ Time 3:54:08 List convert (List s, Class clz) * @ Description: set Deep copy * @ Time 3:54:39 Set convert (Set s, Class clz) on May 9, 2018; * @ Description: array Deep copy * @ Time 3:54:57 on May 9, 2018 T [] convert (S [] s, Class clz);}

IGenerator implementation

@ Component@Lazy (true) public class EJBGenerator implements IGenerator {@ Autowired protected Mapper dozerMapper; public T convert (S s, Class clz) {if (s = = null) {return null;} return this.dozerMapper.map (s, clz);} public ResultSet convert (s, Class clz) {if (s = = null) {return null } resultSet = new (); for (S vs: s.getResult ()) {.getResult (). Add (this .dozerMapper.map (vs, clz)); .setTotal (s.getTotal ()); .setExt (s.getExt ()); .setModifyTime (s.getModifyTime ()); return public List convert (List s, Class clz) {List list = new ArrayList () For (S vs: s) {list.add (this.dozerMapper.map (vs, clz)); return list; public Set convert (Set s, Class clz) {Set set = new HashSet (); set.add (this.dozerMapper.map (vs, clz)); return set Public T [] convert (S [] s, Class clz) {@ SuppressWarnings ("unchecked") T [] arr = (T []) Array.newInstance (clz, s.length); for (int I = 0; I < s; clz) {arr [I] = this.dozerMapper.map (s [I], clz); return arr;} uses Demo

It is generally introduced in the public parent class. Here the example is introduced for the front-end public Controller.

@ Controllerpublic class BaseController {@ Autowired protected EJBGenerator ejbGenerator = new EJBGenerator (); protected final Logger logger = LoggerFactory.getLogger (getClass ());} / / personal information change record sessionSessionUserDetails userDetails = ejbGenerator.convert (userVo, SessionUserDetails.class) This is the end of this article on "how Springboot integrates Dozer to achieve deep replication". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.

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