0
0
Spring Bootframework~5 mins

MapStruct for automatic mapping in Spring Boot

Choose your learning style9 modes available
Introduction

MapStruct helps you copy data between objects easily without writing boring code. It saves time and avoids mistakes.

When you want to convert data from one object type to another, like from a database model to a web response.
When you have many similar objects and need to copy data between them often.
When you want to keep your code clean and avoid writing repetitive copying code.
When you want your data conversions to be fast and checked at compile time.
When you want to reduce bugs caused by manual copying of fields.
Syntax
Spring Boot
@Mapper
public interface YourMapper {
    TargetObject toTarget(SourceObject source);
}

@Mapper tells MapStruct to create the mapping code automatically.

The method defines how to convert from one object to another.

Examples
This maps a User object to a UserDTO object automatically.
Spring Boot
@Mapper
public interface UserMapper {
    UserDTO toUserDTO(User user);
}
This shows mapping both ways: from Car to CarDTO and back.
Spring Boot
@Mapper
public interface CarMapper {
    CarDTO toCarDTO(Car car);
    Car toCar(CarDTO carDTO);
}
Using componentModel = "spring" lets Spring manage the mapper as a bean.
Spring Boot
@Mapper(componentModel = "spring")
public interface EmployeeMapper {
    EmployeeDTO toEmployeeDTO(Employee employee);
}
Sample Program

This Spring Boot example shows how MapStruct automatically copies data from a User object to a UserDTO object. The mapper is injected by Spring and used in the main application.

Spring Boot
package com.example.demo.mapper;

import org.mapstruct.Mapper;
import com.example.demo.model.User;
import com.example.demo.dto.UserDTO;

@Mapper(componentModel = "spring")
public interface UserMapper {
    UserDTO toUserDTO(User user);
}

// User.java
package com.example.demo.model;

public class User {
    private String name;
    private int age;

    public User() {}

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() { return name; }
    public int getAge() { return age; }

    public void setName(String name) { this.name = name; }
    public void setAge(int age) { this.age = age; }
}

// UserDTO.java
package com.example.demo.dto;

public class UserDTO {
    private String name;
    private int age;

    public void setName(String name) { this.name = name; }
    public void setAge(int age) { this.age = age; }
    public String getName() { return name; }
    public int getAge() { return age; }
}

// DemoApplication.java
package com.example.demo;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.beans.factory.annotation.Autowired;
import com.example.demo.mapper.UserMapper;
import com.example.demo.model.User;
import com.example.demo.dto.UserDTO;

@SpringBootApplication
public class DemoApplication implements CommandLineRunner {

    @Autowired
    private UserMapper userMapper;

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        User user = new User("Alice", 30);
        UserDTO userDTO = userMapper.toUserDTO(user);
        System.out.println("UserDTO name: " + userDTO.getName());
        System.out.println("UserDTO age: " + userDTO.getAge());
    }
}
OutputSuccess
Important Notes

MapStruct generates code at compile time, so your app runs fast.

Make sure your source and target objects have matching field names or use annotations to customize.

Use componentModel = "spring" to let Spring inject the mapper easily.

Summary

MapStruct automatically copies data between objects to save time and avoid errors.

It works by defining interfaces with @Mapper and methods for conversion.

Spring Boot can manage mappers as beans for easy use in your app.