第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何將 MapStruct 用于不同的數(shù)據(jù)類型?

如何將 MapStruct 用于不同的數(shù)據(jù)類型?

浮云間 2023-05-10 14:03:44
我有兩種類型的數(shù)據(jù)要映射:注冊(cè)用戶Dto:public class SignUpUserDto {    private String firstName;    private String lastName;    private String username;    private String email;    private String password;    private String title;}注冊(cè)用戶:@Entitypublic class SignUpUser {    private Long id;    private String firstName;    private String lastName;    private String username;    private String email;    private String password;    private Title title;}標(biāo)題:public enum Title {    JUNIOR("junior"),    MIDDLE("middle"),    SENIOR("senior"),    MANAGER("manager");    private final String title;    Title(final String title) {        this.title = title;    }    public String toString() {        return this.title;    }}對(duì)于DTO title 成員是一個(gè)String。對(duì)于實(shí)體title 成員是Title。映射器應(yīng)該是什么樣子的?我應(yīng)該傳遞已經(jīng)在服務(wù)中轉(zhuǎn)換的標(biāo)題嗎?@Mapper(componentModel = "spring")public interface SignUpUserMapper {    SignUpUserMapper INSTANCE = Mappers.getMapper(SignUpUserMapper.class);    @Mapping(target = "title", expression = "title")    public SignUpUserDto signUpUserToSignUpUserDto(SignUpUser signUpUser, String title);    @Mapping(target = "title", source = "title")    public SignUpUser signUpUserDtoToSignUpUser(SignUpUserDto signUpUserDto, Title title);}或者我應(yīng)該在Mapper中進(jìn)行轉(zhuǎn)換?@Mapper(componentModel = "spring",  imports = Title.class)public interface SignUpUserMapper {    SignUpUserMapper INSTANCE = Mappers.getMapper(SignUpUserMapper.class);    @Mapping(target = "title", expression = "java(signUpUser.getTitle().toString())")    public SignUpUserDto signUpUserToSignUpUserDto(SignUpUser signUpUser);    @Mapping(target = "title", source = "java(new Title(signUpUserDto.getTitle()))")    public SignUpUser signUpUserDtoToSignUpUser(SignUpUserDto signUpUserDto);}
查看完整描述

2 回答

?
神不在的星期二

TA貢獻(xiàn)1963條經(jīng)驗(yàn) 獲得超6個(gè)贊

我應(yīng)該傳遞已經(jīng)在服務(wù)中轉(zhuǎn)換的標(biāo)題嗎?


你絕對(duì)不應(yīng)該這樣做。這是轉(zhuǎn)換器的工作,而不是服務(wù)的


嘗試以下方法:


1)枚舉類添加轉(zhuǎn)換方法


enum Title {

    ...


    public static Title fromString(String title) {

        if (title != null) {

            for (Title t : Title.values()) {

                if (t.toString().equals(title)) {

                    return t;

                }

            }

        }

        return null;

    }

}

2) Mapper 接口增加2個(gè)轉(zhuǎn)換方法(僅限Java 8+)


@Mapper(componentModel = "spring")

public interface SignUpUserMapper {

    SignUpUserDto signUpUserToSignUpUserDto(SignUpUser signUpUser);

    SignUpUser signUpUserDtoToSignUpUser(SignUpUserDto signUpUserDto);


    default String fromEnum(Title title) {

        return title == null ? null : title.toString();

    }


    default Title toEnum(String title) {

        return title == null ? null : Title.fromString(title);

    }

}


查看完整回答
反對(duì) 回復(fù) 2023-05-10
?
慕碼人8056858

TA貢獻(xiàn)1803條經(jīng)驗(yàn) 獲得超6個(gè)贊

像這樣使用第二個(gè)選項(xiàng):


 @Mapper(componentModel = "spring",  imports = Title.class)                     

public interface SignUpUserMapper {

SignUpUserMapper INSTANCE = Mappers.getMapper(SignUpUserMapper.class);

@Mapping(target = "title", expression = "java(signUpUser.getTitle().toString())")

public SignUpUserDto signUpUserToSignUpUserDto(SignUpUser signUpUser);

@Mapping(target = "title", source = "java(Title.valueOf(signUpUserDto.getTitle().toUpperCase()))")

public SignUpUser signUpUserDtoToSignUpUser(SignUpUserDto signUpUserDto);            

}


查看完整回答
反對(duì) 回復(fù) 2023-05-10
  • 2 回答
  • 0 關(guān)注
  • 218 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)