dozer

Springboot整合Dozer深度复制

匿名 (未验证) 提交于 2019-12-03 00:18:01
Dozer Dozer是一种Java Bean到Java Bean的映射器,递归地将数据从一个对象复制到另一个对象,它是一个强大的,通用的,灵活的,可重用的和可配置的开源映射框架。 常用于: 代码层与层之间javabean转换, 如dao层PO转前端VO 分布式中, DAO层PO转DTO, DO 以及web层DTO转VO 注意的场景: 由于bean之间的深度复制, 在进行一些类似更新, 插入操作时尤其要注意最终接收到PO的一些关键字段 如ID 是否是真正需要的. 场景: 传入的DTO A为查出的DTO B复制后的, 这时候A里会有B的ID, 在插入A的时候很有可能造成主键冲突. 建议: 不用Dozer最好, Dozer带来的是性能开销.(这是不可能…) 某些特殊操作可以用切面控制特殊字段进行置空操作 pom.xml加入以下依赖 < dependency > < groupId > net.sf.dozer </ groupId > < artifactId > dozer-spring </ artifactId > < version > 5.5.1 </ version > </ dependency > < dependency > < groupId > net.sf.dozer </ groupId > < artifactId > dozer </ artifactId

dozer

匿名 (未验证) 提交于 2019-12-02 22:56:40
maven方式: < dependency > < groupId > net.sf.dozer </ groupId > < artifactId > dozer </ artifactId > < version > 5.4.0 </ version > </ dependency > NotSameAttributeA.java public class NotSameAttributeA { private long id; private String name; private Date date; // 省略getter/setter } NotSameAttributeB.java public class NotSameAttributeB { private long id; private String value ; private Date date; // 省略getter/setter } 这两个类存在属性名不完全相同的情况:name 和 value。 如果要映射的两个对象有完全相同的属性名,那么一切都很简单。 只需要直接使用Dozer的API即可: Mapper mapper = new DozerBeanMapper(); DestinationObject destObject = mapper. map (sourceObject,

Java to CSV file, SuperCSV Dozer: How to avoid looping for a nested object

空扰寡人 提交于 2019-12-02 15:01:37
问题 I have the below classes.. SurveyResponse.java import java.util.List; public class SurveyResponse { private int age; private Boolean consentGiven; private List<Answer> answers; public SurveyResponse() { } public SurveyResponse(final int age, final Boolean consentGiven, final List<Answer> answers) { this.age = age; this.consentGiven = consentGiven; this.answers = answers; } public int getAge() { return age; } public List<Answer> getAnswers() { return answers; } public Boolean getConsentGiven()

Java to CSV file, SuperCSV Dozer: How to avoid looping for a nested object

人走茶凉 提交于 2019-12-02 10:50:50
I have the below classes.. SurveyResponse.java import java.util.List; public class SurveyResponse { private int age; private Boolean consentGiven; private List<Answer> answers; public SurveyResponse() { } public SurveyResponse(final int age, final Boolean consentGiven, final List<Answer> answers) { this.age = age; this.consentGiven = consentGiven; this.answers = answers; } public int getAge() { return age; } public List<Answer> getAnswers() { return answers; } public Boolean getConsentGiven() { return consentGiven; } public void setAge(final int age) { this.age = age; } public void setAnswers

A classloader proplem related to spring-boot-devtools

我的梦境 提交于 2019-12-01 11:53:39
问题 Background: Spring boot project, add goods and goods price list Goods: List<GoodsPrice> pricelist; in controller first convert goodsForm to goods(by dozer), then save goods,after saving goods iterate goods price list to populate goodsId. goods.getPriceList().forEach(p -> p.setGoodsId(goods.getId())); When iterate goods price list, throw exception: java.lang.ClassCastException: com.foo.goods.model.GoodsPrice cannot be cast to com.foo.goods.model.GoodsPrice at com.foo.goods.service.GoodsService

使用dozer将DTO转化为PO

寵の児 提交于 2019-12-01 08:48:16
DTO,就是Data Transfer Object,数据传输对象。 在开发中,经常需要将DTO转化为PO,可以使用dozer。 Maven <dependency> <groupId>net.sf.dozer</groupId> <artifactId>dozer</artifactId> <version>5.5.1</version> </dependency> Utils 工具类BeanMapperUtils 。 import java.util.Collection; import java.util.List; import com.google.common.collect.Lists; import org.dozer.DozerBeanMapper; public class BeanMapperUtils { /** * 持有Dozer单例, 避免重复创建DozerMapper消耗资源. */ private static DozerBeanMapper dozer = new DozerBeanMapper(); /** * 基于Dozer转换对象的类型. */ public static <T> T map(Object source, Class<T> destinationClass) { return dozer.map(source,

Map a list of object to another list using Dozer's custom converters

那年仲夏 提交于 2019-11-30 23:15:01
What I am trying to do is to map a List of entities to a list of their String ids (more or less) using Dozer. Obviously, it implies Custom Converter. My first idea was to make a converter from MyEntity to a String, and then say to Dozer something like "Map every object of this collection using this converter". But I couldn't figure out how to do so. So my second idea was to make a converter form a list of entities to a list of string, directly. My problem on this idea is that I was strugling on something ridiculous which is to get the type of my list in the constructor, as below (which doesn't

A dozer map exception related to Spring boot devtools

瘦欲@ 提交于 2019-11-30 20:18:14
I have encountered a very strange exception, and I don't know how to find the reason. Business background: Add goods and meantime it's price list, a goods have 5 price for diff level user. In controller, first convert goodForm to goods by using dozer, then call goodsService to save goods. In goodsService after saving goods, traversal goods price list and populate goodsId to goods price, GoodsForm: @Mapping("priceList") List<GoodsPriceForm> goodsPriceFormList; Goods: List<GoodsPrice> priceList; Controller: Goods goods = BeanMapper.map(goodsForm, Goods.class); goodsService.saveGoods(adminId,

Map a list of object to another list using Dozer's custom converters

非 Y 不嫁゛ 提交于 2019-11-30 17:59:19
问题 What I am trying to do is to map a List of entities to a list of their String ids (more or less) using Dozer. Obviously, it implies Custom Converter. My first idea was to make a converter from MyEntity to a String, and then say to Dozer something like "Map every object of this collection using this converter". But I couldn't figure out how to do so. So my second idea was to make a converter form a list of entities to a list of string, directly. My problem on this idea is that I was strugling

How to use Dozer with Spring Boot?

拈花ヽ惹草 提交于 2019-11-30 12:06:49
I am working on a Spring Boot project. I just have annotation configuration. I want to include dozer to transform Entities to DTO and DTO to Entities. I see in the dozer website, they explain i have to add the following configuration in spring xml configuration file. Since i have not xml file but annotation configuration Java class, i don't know how to translate this into Java Configuration class. <bean id="org.dozer.Mapper" class="org.dozer.DozerBeanMapper"> <property name="mappingFiles"> <list> <value>dozer-global-configuration.xml</value> <value>dozer-bean-mappings.xml</value> <value>more