Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

In the previous section, we demonstrated the use of @Mapping annotation and expression attribute. In this article, we will use cases to introduce the use of @mappingTarget and extend “Mapping multiple objects to one object”.

@Mappings

You can configure multiple @mapping, for example

@Mappings({ @Mapping(source = "id", target = "carId"), @Mapping(source = "name", target = "carName"), @Mapping(source = "color", target = "carColor") })
Copy the code

@MappingTarget

To update an existing object, use an example:

1. Create the bmwcar. Java class

@NoArgsConstructor
@AllArgsConstructor
@Data
public class BMWCar {
    private String make;
    private int numberOfSeats;
    private CarType type;

    private String color;
    private String price;

}
Copy the code

2. Create the update method in mapper and view the implementation class

// Update the method
void updateBwmCar(Car car, @MappingTarget BMWCar bwmCar);

/ / implementation class
public void updateBwmCar(Car car, BMWCar bwmCar) {
	if(car ! =null) { bwmCar.setMake(car.getMake()); bwmCar.setNumberOfSeats(car.getNumberOfSeats()); bwmCar.setType(car.getType()); }}Copy the code

3. Client code

@Test
public void updateBwmCar(a) {
	Car car = new Car( "Morris".5, CarType.SEDAN );
	BMWCar bwmCar = new BMWCar("BWM".5, CarType.SPORTS, "RED"."50w");
	System.out.println("Car before update :"+car.toString());
	System.out.println("Before update BWMCar:"+bwmCar.toString());

	CarMapper.INSTANCE.updateBwmCar(car, bwmCar);

	System.out.println("Updated car:"+car.toString());
	System.out.println("Updated BWMCar:"+bwmCar.toString());
}
Copy the code

Execution Result:

Extension: Multiple objects map to one object

1. Prepare entity classesBenz4SMall.javaMall4S.java

@NoArgsConstructor
@AllArgsConstructor
@Data
public class Mall4S {

    private String address;

    private String mobile;

}

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Benz4SMall {

    private String address;
    private String mobile;
    private String make;
    private int numberOfSeats;
}
Copy the code

2. Mapper creates the transformation method and views the generated implementation classes

Benz4SMall mallCarToBenzMall(Car car, Mall4S mall4S);

/** * implementation class */
public Benz4SMall mallCarToBenzMall(Car car, Mall4S mall4S) {
	if (car == null && mall4S == null) {
		return null;
	} else {
		Benz4SMall benz4SMall = new Benz4SMall();
		if(car ! =null) {
			benz4SMall.setMake(car.getMake());
			benz4SMall.setNumberOfSeats(car.getNumberOfSeats());
		}

		if(mall4S ! =null) {
			benz4SMall.setAddress(mall4S.getAddress());
			benz4SMall.setMobile(mall4S.getMobile());
		}

		returnbenz4SMall; }}Copy the code

3. The client

@Test
public void mallCarToBenzMall(a) {
	Car car = new Car( "Morris".5, CarType.SEDAN );
	Mall4S mall4S = new Mall4S("Beijing"."135XXXX4503");
	Benz4SMall benz4SMall = CarMapper.INSTANCE.mallCarToBenzMall(car, mall4S);
	System.out.println(benz4SMall.toString());
}
Copy the code

The execution result

So much for using Mapstruct, let’s compare its advantages and disadvantages with other copying utility classes. If you have different opinions or better idea, welcome to contact AH Q, add AH Q can join the technical exchange group to participate in the discussion!