import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
 
// 定义实体类
public class Source {
    private String name;
    private int age;
    // 省略其他属性和方法
}
 
public class Target {
    private String fullName;
    private int age;
    // 省略其他属性和方法
}
 
// 定义映射器接口
@Mapper
public interface SourceTargetMapper {
    SourceTargetMapper INSTANCE = Mappers.getMapper(SourceTargetMapper.class);
 
    @Mapping(source = "name", target = "fullName")
    Target sourceToTarget(Source source);
 
    default Source targetToSource(Target target) {
        // 这里可以实现反向映射,如果不需要可以省略
        return null;
    }
}
 
// 在Spring Boot应用中使用
public class Application {
    public static void main(String[] args) {
        Source source = new Source();
        source.setName("John");
        source.setAge(30);
 
        Target target = SourceTargetMapper.INSTANCE.sourceToTarget(source);
        // 使用target对象
    }
}这个例子展示了如何在Spring Boot项目中使用MapStruct来简化对象映射的过程。首先定义了两个简单的实体类Source和Target。然后创建了一个映射器接口SourceTargetMapper,使用@Mapping注解指定了字段的映射关系。最后在应用的主类中演示了如何使用这个映射器将一个Source对象转换为Target对象。