使用 mapstruct 将多个字段映射到 List
Map several fields to List with a mapstruct
例如,我有以下实体:
class Bank
{
String name;
String employee1;
String employee2;
}
还有一个 Dto 对象:
class BankDto
{
String name;
List<String> employeeList;
}
是否有正确的方法将 Bank 映射到 BankDto,以便将 employee1 和 employee2 字段添加到 employeeList 集合?
您可以通过在映射接口中添加后映射方法来解决您的问题:
@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);
}
}
如果你想让你的代码更动态,你也可以使用反射,你可以增加或减少员工字段的数量,映射器会自动为你做这件事:
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);
}
}
输出:
********BankDTO*********
Bank name : BNP
*****************
[Amine, SAMI, RAMI, KARIM, ZIAD]
你应该确保封装写getters setters ..
例如,我有以下实体:
class Bank
{
String name;
String employee1;
String employee2;
}
还有一个 Dto 对象:
class BankDto
{
String name;
List<String> employeeList;
}
是否有正确的方法将 Bank 映射到 BankDto,以便将 employee1 和 employee2 字段添加到 employeeList 集合?
您可以通过在映射接口中添加后映射方法来解决您的问题:
@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);
}
}
如果你想让你的代码更动态,你也可以使用反射,你可以增加或减少员工字段的数量,映射器会自动为你做这件事:
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);
}
}
输出:
********BankDTO*********
Bank name : BNP
*****************
[Amine, SAMI, RAMI, KARIM, ZIAD]
你应该确保封装写getters setters ..