枚举互相映射

关于枚举互相映射问题

前景: 我们是maven模块化管理,存在api提供给其他服务使用,此时我们需要将返回model与服务层的model进行剥离。
举例子 sql返回的对象A,A中包含枚举EnumA, 然后covertTo API层的model B, b中包含枚举Benum
然后在转换的时候使用了mapStruct来自动生成,方便改造。

这里有片文章对mapStruct的使用介绍挺详细的。我使用的版本是1.4.1.final,
关于如何使用mapStruct

代码实现

pom

  <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
          <encoding>UTF-8</encoding>
          <annotationProcessorPaths>
            <!-- 此处,lombok依赖一定要放在,Mapstruct-processor依赖之前。否则,生成了maptruct的实现类,但该类只创建了对象,没有进行赋值 -->
            <path>
              <groupId>org.projectlombok</groupId>
              <artifactId>lombok</artifactId>
              <version>1.18.22</version>
            </path>
            <path>
              <groupId>org.mapstruct</groupId>
              <artifactId>mapstruct-processor</artifactId>
              <version>1.4.2.Final</version>
            </path>
          </annotationProcessorPaths>
        </configuration>
      </plugin>
import org.mapstruct.Mapper;
@Mapper(componentModel = "spring")
public interface EnumMapper {

    SourceTypeEnum toSourceTypeEnum(SourceType sourceType);

    SourceType toSourceType(SourceTypeEnum enums);

    GroupTypeEnum toGroupTypeEnum(GroupType groupType);

    GroupType toGroupType(GroupTypeEnum groupTypeEnum);

    UserTypeEnum toUserTypeEnum(UserType userType);

    UserType toUserType(UserTypeEnum userTypeEnum);
}

@RequiredArgsConstructor
@Getter
public enum SourceTypeEnum implements EnumBase<String> {
    LDAP("ldap"),
    INNER("内部系统"),
    WE_WORK("企业微信");

    SourceTypeEnum(String message){
        this.message = message;
        this.code = this.name();
    }
    @EnumValue
    private final String code;

    private final String message;
}

@Getter
public enum SourceType implements EnumBase<String> {
    LDAP("ldap"),
    INNER("内部系统"),
    WE_WORK("企业微信");

    SourceType(String message){
        this.message = message;
        this.code = this.name();
    }

    private final String code;

    private final String message;
}

在这里插入图片描述
以下为自动生成

package com.tarsocial.dag.auth.convert;

import com.tarsocial.dag.auth.enums.GroupTypeEnum;
import com.tarsocial.dag.auth.enums.SourceTypeEnum;
import com.tarsocial.dag.auth.enums.UserTypeEnum;
import com.tarsocial.dag.auth.model.enums.GroupType;
import com.tarsocial.dag.auth.model.enums.SourceType;
import com.tarsocial.dag.auth.model.enums.UserType;
import javax.annotation.Generated;
import org.springframework.stereotype.Component;

@Generated(
    value = "org.mapstruct.ap.MappingProcessor",
    date = "2023-06-07T15:43:21+0800",
    comments = "version: 1.4.2.Final, compiler: javac, environment: Java 17.0.6 (Amazon.com Inc.)"
)
@Component
public class EnumMapperImpl implements EnumMapper {

    @Override
    public SourceTypeEnum toSourceTypeEnum(SourceType sourceType) {
        if ( sourceType == null ) {
            return null;
        }

        SourceTypeEnum sourceTypeEnum;

        switch ( sourceType ) {
            case LDAP: sourceTypeEnum = SourceTypeEnum.LDAP;
            break;
            case INNER: sourceTypeEnum = SourceTypeEnum.INNER;
            break;
            case WE_WORK: sourceTypeEnum = SourceTypeEnum.WE_WORK;
            break;
            default: throw new IllegalArgumentException( "Unexpected enum constant: " + sourceType );
        }

        return sourceTypeEnum;
    }

    @Override
    public SourceType toSourceType(SourceTypeEnum enums) {
        if ( enums == null ) {
            return null;
        }

        SourceType sourceType;

        switch ( enums ) {
            case LDAP: sourceType = SourceType.LDAP;
            break;
            case INNER: sourceType = SourceType.INNER;
            break;
            case WE_WORK: sourceType = SourceType.WE_WORK;
            break;
            default: throw new IllegalArgumentException( "Unexpected enum constant: " + enums );
        }

        return sourceType;
    }

    @Override
    public GroupTypeEnum toGroupTypeEnum(GroupType groupType) {
        if ( groupType == null ) {
            return null;
        }

        GroupTypeEnum groupTypeEnum;

        switch ( groupType ) {
            case ROLE: groupTypeEnum = GroupTypeEnum.ROLE;
            break;
            case DEPARTMENT: groupTypeEnum = GroupTypeEnum.DEPARTMENT;
            break;
            default: throw new IllegalArgumentException( "Unexpected enum constant: " + groupType );
        }

        return groupTypeEnum;
    }

    @Override
    public GroupType toGroupType(GroupTypeEnum groupTypeEnum) {
        if ( groupTypeEnum == null ) {
            return null;
        }

        GroupType groupType;

        switch ( groupTypeEnum ) {
            case ROLE: groupType = GroupType.ROLE;
            break;
            case DEPARTMENT: groupType = GroupType.DEPARTMENT;
            break;
            default: throw new IllegalArgumentException( "Unexpected enum constant: " + groupTypeEnum );
        }

        return groupType;
    }

    @Override
    public UserTypeEnum toUserTypeEnum(UserType userType) {
        if ( userType == null ) {
            return null;
        }

        UserTypeEnum userTypeEnum;

        switch ( userType ) {
            case INNER_USER: userTypeEnum = UserTypeEnum.INNER_USER;
            break;
            case NORMAL_USER: userTypeEnum = UserTypeEnum.NORMAL_USER;
            break;
            case SERVICE_USER: userTypeEnum = UserTypeEnum.SERVICE_USER;
            break;
            default: throw new IllegalArgumentException( "Unexpected enum constant: " + userType );
        }

        return userTypeEnum;
    }

    @Override
    public UserType toUserType(UserTypeEnum userTypeEnum) {
        if ( userTypeEnum == null ) {
            return null;
        }

        UserType userType;

        switch ( userTypeEnum ) {
            case INNER_USER: userType = UserType.INNER_USER;
            break;
            case NORMAL_USER: userType = UserType.NORMAL_USER;
            break;
            case SERVICE_USER: userType = UserType.SERVICE_USER;
            break;
            default: throw new IllegalArgumentException( "Unexpected enum constant: " + userTypeEnum );
        }

        return userType;
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值