Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BeanUtils.copyProperties() vs DozerBeanMapper.map()

I am using BeanUtils.copyProperties() for bean to dto mapping when I need to map all fields and field names are same. But I need not all field of source bean to map in destination dto, I used DozerBeanMapper.map() , because I haven't idea about to use BeanUtils in this situation.

So I think both methods having their own functionality, and there is no any similarity between both. Am I right? Please guide me.

like image 316
Pankaj Kumar Avatar asked Jun 23 '11 11:06

Pankaj Kumar


2 Answers

You might check out ModelMapper. It will intelligently map properties (fields/methods) even if the names are not exactly the same. Defining specific properties to be mapped or skipped is simple and uses real code instead of XML:

ModelMapper modelMapper = new ModelMapper();
modelMapper.addMappings(new PropertyMap<Order, OrderDTO>() {
  protected void configure() {
    map().setBillingStreet(source.getBillingStreetAddress());
    skip().setBillingCity(null);
  }
});

Check out the project homepage for more info:

http://modelmapper.org

like image 52
Jonathan Avatar answered Sep 16 '22 17:09

Jonathan


We considered mapstruct for our usecase. See a sample below:

@Mapper
public interface MyMapper {

    MyMapper INSTANCE = Mappers.getMapper(MyMapper.class);

    To to(From from);

}

Here is a performance comparison of MapStruct against Selma, Orika, ModelMapper, Dozer and manual mapping:

Manual mapping vs. Selma vs. MapStruct vs. Orika vs. ModelMapper vs. Dozer

Selma vs. MapStruct

like image 37
user2775185 Avatar answered Sep 16 '22 17:09

user2775185