简体   繁体   中英

Map several fields to List with a mapstruct

for example, I have following entity:

class Bank
{
String name;
String employee1;
String employee2;
}

And a Dto object:

class BankDto
{
String name;
List<String> employeeList;
}

Is there a proper way how to map Bank to BankDto, so employee1 and employee2 fields will be added to employeeList collection?

You can resolve your issue by adding an after mapping method to your mapping interface:

@Mapper
public abstract class Mapper {

    public abstract BankDto getBankDto(Bank bank);

    @AfterMapping
    public void setEmployees(Bank bank, @MappingTarget BankDto bankDto) {
        bankDto.setEmployeeList(new List<String>());
        bankDto.getEmployeeList().add(bank.employee1);
        bankDto.getEmployeeList().add(bank.employee2);
    }
}

You can also use reflection if you like to make your code more dynamic, you can increase or decrease the number of employee fields and the mapper will automatically do the job for you:

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

class Bank {
    String name;
    // you can add or remove employees here and the mapper still work as expected :
    String employee1;
    String employee2;
    String employee3;
    String employee4;
    String employee5;
}

class BankDto {
    String name;
    List<String> employeeList = new ArrayList<>();
}

class Mapper {
    static BankDto map(Bank bank) throws IllegalArgumentException, IllegalAccessException {
        BankDto dto = new BankDto();
        dto.name = bank.name;
        
        List<Field> fields = Stream.of(bank.getClass().getDeclaredFields()).filter(f -> f.getName().startsWith("emp"))
                .collect(Collectors.toList());
        for (Field f : fields) {
            if (!f.isAccessible()) {
                f.setAccessible(true);
                dto.employeeList.add((String) (f.get(bank)));
                f.setAccessible(false);
            }
            // System.out.println(f.get(bank)); //print fields with reflection
        }
        return dto;
    }
}

public class Main {
    public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException {
        Bank b = new Bank();
        b.name = "BNP";
        b.employee1 = "Amine";
        b.employee2 = "SAMI";
        b.employee3 = "RAMI";
        b.employee4 = "KARIM";
        b.employee5 = "ZIAD";

        BankDto dto = Mapper.map(b);
        System.out.println("********BankDTO*********");
        System.out.println("Bank name :   " + dto.name);
        System.out.println("*****************");
        System.out.println(dto.employeeList);
    }
}

Output:

********BankDTO*********
Bank name :   BNP
*****************
[Amine, SAMI, RAMI, KARIM, ZIAD]

You should ensure encapsulation write getters setters..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM