Memo Memo.
Creating a new user is the easiest to understand. Map the input user information and user Entity (DTO).
It looks like this in a legacy project using Struts of the project that is currently in. An image to rewrite from the received form information.
When not using modelMapper
@PostMapping("/new")
public String newUser(@Validated @ModelAttribute("userForm") UserForm form, BindingResult br) {
User user = User.builder()
.name(form.getName())
.email(form.getEmail())
.birthDay(form.getBirthDay())
.zip(form.getZip())
.address(form.getAddress())
.build();
With modelMapper
With ModelMapper
@PostMapping("/new")
public String newUser(@Validated @ModelAttribute("userForm") UserForm form, BindingResult br) {
ModelMapper modelMapper = new ModelMapper();
//Create a DTO from the input values
val inputUser = modelMapper.map(form, User.class);
Refreshing! !! To use it, the following description is required in the dependency configuration file such as build.gradle and pom.xml.
build.gradle
dependencies {
// modelmapper
compile "org.modelmapper:modelmapper:0.7.5"
}
Recommended Posts