Spring通用方法

问题描述 投票:0回答:1

我有2个实体和2个Dto,它们继承自Item和ItemDto。我想创建将实体转移到Dto的通用Mapper。

物品类别:

 @MappedSuperclass 
public class Item {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long id;

        @NotNull
        private String title;

        @NotNull
        private String content;

        @NotNull
        private Date created;

        @ManyToOne
        private User user;

        // getters, setters, constructors }

Twitter类:

@NotNull
    private String twitterName;


    public Twitter(){

    }

    public Twitter(String title, String content, Date created, String twitterName, User user) {
        super(title,content,created, user);
        this.twitterName = twitterName;
    }

TwitterDto类:

public class TwitterDto extends ItemDto {

    public String getTwitterName() {
        return twitterName;
    }

    private String twitterName;

    public TwitterDto(Long id, String title, String content, Date created, Long userId, String twitterName1) {
        super(id, title, content, created, userId);
        this.twitterName = twitterName1;
    }

文章类别:

@NotNull
    private String articleUrl;

    public Article(){

    }

    public Article(String title, String content, Date created, String articleUrl, User user) {
        super(title,content,created, user);
        this.articleUrl = articleUrl;
    }

ArticleDto类:

public class ArticleDto extends ItemDto {

    private String articleUrl;

    public ArticleDto(Long id, String title, String content, Date created, Long userId, String articleUrl) {
        super(id, title, content, created, userId);
        this.articleUrl = articleUrl;
    }

    public String getArticleUrl() {
        return articleUrl;
    }

我尝试这样做一个Mapper类:

public class ItemMapper<T extends Item, T1 extends ItemDto> {

    private UserRepository userRepository;

    @Autowired
    public ItemMapper(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    public T dtoToEntity(T1 item){
        User user = userRepository.findById(item.getUserId())
                .orElseThrow(() ->
                        new ResponseStatusException(HttpStatus.BAD_REQUEST, "User with this id does not exist!"));

        if(item instanceof ArticleDto){
            return (T) new Article(item.getTitle(), item.getContent(),
                    item.getCreated(), ((ArticleDto) item).getArticleUrl() , user);
        }

        if(item instanceof TwitterDto){
            return (T) new Twitter(item.getTitle(), item.getContent(),
                    item.getCreated(), ((TwitterDto) item).getTwitterName(), user);
        }

        return ?????????
    }
}

但是我不知道它是否做得好以及返回什么。

java spring-boot generics generic-programming
1个回答
0
投票

为什么不使用现有的映射器而不是从头开始自己构建它?一个示例是DozerMapper,其中您需要做的就是:

Mapper mapper = DozerBeanMapperBuilder.buildDefault();
Twitter twitter = getTwitter();
TwitterDto twitterDto = mapper.map(twitter, TwitterDto.class);

它基于反射,因此易于使用。如果需要更好的性能,则应选择基于生成的代码的内容,例如mapstruct

© www.soinside.com 2019 - 2024. All rights reserved.