在Spring Boot Applications中,在@Entity级别而不是@Service或@Controller级别使用ModelMapper的有效方法是什么?

时间:2019-01-08 18:06:52

标签: spring-boot jpa spring-data-jpa modelmapper

我必须在dao层使用实体进行dto转换。 我正在使用ModelMapper转换“实体-> Dto”和“ Dto->实体”。

下面是我的代码:

@Entity
@Table(name = "user")
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "user_id")
    private Long userId;

    @Column(name = "user_first_name", length = 45)
    private String userFirstName;

    @Column(name = "user_last_name", length = 45)
    private String userLastName;

    //SETTERS AND GETTERS

    public UserDto convertToDto() {
        ModelMapper modelMapper = new ModelMapper();
        return modelMapper.map(this, UserDto.class);
    }

}

public class UserDto {

    private Long userId;

    private String userFirstName;

    //SETTERS AND GETTERS

    public User convertToEntity() {
        ModelMapper modelMapper = new ModelMapper();
        return modelMapper.map(this, User.class);
    }

}

这是有效的方法吗?还是有什么方法可以使用ModelMapper?

0 个答案:

没有答案