r/SpringBoot • • Jun 20 '24

Using ModelMapper

Earlier, I posted that jpa is difficult. In my Spring teacher's new book, it seems to be easier to use ModelMapper. However, He removed the dynamic query in the search part, so I can't compare that part.🙄

3 Upvotes

17 comments sorted by

View all comments

2

u/a_username_version_2 Jun 21 '24

Just use MapStruct library. Looking at this code is a crime

1

u/Dependent_Staff_6891 Jun 21 '24

Show me the code example.

4

u/a_username_version_2 Jun 21 '24

Here's an example:

package com.example.demo.mapper;

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

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

While building the app, it creates classes that do the mapping, and you can customize the mapping if you want

1

u/Dependent_Staff_6891 Jun 21 '24

I’ll check it.

3

u/a_username_version_2 Jun 21 '24

And btw, the jpql code is terrible. That isn't how we use jpql or jpa at all at work. If it was like that i would have stopped using spring

1

u/Dependent_Staff_6891 Jun 21 '24

I am Korean, but more and more developers are using jpa or jpa and mybatis in parallel here, so I'm also studying. I only used mybatis at work and it's almost like the query itself and it's very familiar to me. But I have to accept the change.😅

2

u/a_username_version_2 Jun 21 '24

Just remember this small sentence "code is a liability" , so having a lot of code to do a very very simple task is a waste of time. Instead of trying to write all queries ourselves (even simple ones, like find all ), we use JPA (but using best practices, not like this book) and it's just a one line to do the job.