我有以下DTO,Entity,Mapper类,它们在数据库中保持值不变。我在“日期”列的那些属性之一中面临问题。我输入了“ DD-MM-yyyy”格式的字符串。但是,它在mapper类中失败,称其为不可解析的日期。不知道为什么在编译时生成的类中没有默认的日期格式

数据库结构

java - 覆盖Mapper类或客户映射器或属性映射中的方法-Spring Boot-LMLPHP
DTO

public class CcarRepWfInstDTO implements Serializable {

private Long id;

private Long reportId;

private Long workflowInstanceId;

private String cobDate;

private String frequency;

private String runType;

private String reportVersion;

private String workflowStatus;

}


实体

@Data
@Entity(name = "CcarReportWorkflowInstance")
@Table(name = "CCAR_REPORT_WORKFLOW_INSTANCE")
public class CcarReportWorkflowInstance implements Serializable {
private static final long serialVersionUID = 1L;

@SequenceGenerator(name = "CCAR_REPORT_WORKFLOW_INSTANCESEQ", sequenceName = "SEQ_CCAR_REPORT_WF_INST_ID", allocationSize = 1)
@Id
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "CCAR_REPORT_WORKFLOW_INSTANCESEQ")
private Long id;

@Column(name = "WORKFLOW_INSTANCE_ID", nullable = false)
private Long workflowInstanceId;

@Column(name = "COB_DATE", nullable = false)
@Temporal(TemporalType.DATE)
private Date cobDate;

@Column(name = "FREQUENCY", nullable = false)
private String frequency;

@Column(name = "RUN_TYPE", nullable = false)
private String runType;

@Column(name = "REPORT_VERSION", nullable = false)
private String reportVersion;

@Column(name = "TERMINATION_STATUS", nullable = false)
private String terminationStatus;

@Version
@Column(name = "VERSION", columnDefinition = "integer DEFAULT 0", nullable = false)
private Long version = 0L;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "REPORT_ID", nullable = false, insertable = false, updatable = false)
private CcarReport ccarReport;

@Column(name = "REPORT_ID")
private Long reportId;


编译期间生成的Mapper Implementation类的代码片段失败,并抛出错误,指出无法解析的日期

    @Override
 public CcarReportWorkflowInstance ccarRepWfInstDTOToCcarRepWfInst(CcarRepWfInstDTO ccarRepWfInstDTO) {
    if ( ccarRepWfInstDTO == null ) {
        return null;
    }

    CcarReportWorkflowInstance ccarReportWorkflowInstance = new CcarReportWorkflowInstance();

    ccarReportWorkflowInstance.setId( ccarRepWfInstDTO.getId() );
    ccarReportWorkflowInstance.setWorkflowInstanceId( ccarRepWfInstDTO.getWorkflowInstanceId() );
    if ( ccarRepWfInstDTO.getCobDate() != null ) {
        try {
            ccarReportWorkflowInstance.setCobDate( new SimpleDateFormat().parse( ccarRepWfInstDTO.getCobDate() ) );
        }
        catch ( ParseException e ) {
            throw new RuntimeException( e );
        }
    }
    ccarReportWorkflowInstance.setFrequency( ccarRepWfInstDTO.getFrequency() );
    ccarReportWorkflowInstance.setRunType( ccarRepWfInstDTO.getRunType() );
    ccarReportWorkflowInstance.setReportVersion( ccarRepWfInstDTO.getReportVersion() );
    ccarReportWorkflowInstance.setReportId( ccarRepWfInstDTO.getReportId() );

    return ccarReportWorkflowInstance;
}


服务层中使用映射器类的代码失败

private void persistWfInst(CcarRepWfInstDTO ccarRepWfInstDTO) {

    try {
        ccarRepWfInstDTO.setWorkflowInstanceId(ccarRepWfInstDTO.getWorkflowInstanceId());
        CcarReportWorkflowInstance ccarReportWorkflowInstance = ccarRepWfInstMapper.ccarRepWfInstDTOToCcarRepWfInst(ccarRepWfInstDTO);
        ccarRepWfInstRepository.save(ccarReportWorkflowInstance);
    } catch (Exception e) {
        log.info("Exception while saving workflow instance information =" + e);
        throw new CustomParameterizedException(e.getMessage());
    }
}


服务层中的自定义代码,不使用使用显式定义的simpledate格式的mapper,它工作正常。由于我的整个应用程序都是围绕它设计的,因此请告知如何解决此问题并使用Spring引导方式堆栈。如果我不使用默认提供的映射器类和实现,那将不是很好。

private void persistWfInst(CcarRepWfInstDTO ccarRepWfInstDTO) {

    try {
        ccarRepWfInstDTO.setWorkflowInstanceId(ccarRepWfInstDTO.getWorkflowInstanceId());
        CcarReportWorkflowInstance ccarReportWorkflowInstance = new CcarReportWorkflowInstance();
        ccarReportWorkflowInstance.setId(ccarRepWfInstDTO.getId());
        ccarReportWorkflowInstance.setWorkflowInstanceId(ccarRepWfInstDTO.getWorkflowInstanceId());
        if (ccarRepWfInstDTO.getCobDate() != null) {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("DD-MM-yyyy");
                ccarReportWorkflowInstance.setCobDate(sdf.parse(ccarRepWfInstDTO.getCobDate()));
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        }
        ccarReportWorkflowInstance.setFrequency(ccarRepWfInstDTO.getFrequency());
        ccarReportWorkflowInstance.setRunType(ccarRepWfInstDTO.getRunType());
        ccarReportWorkflowInstance.setReportVersion(ccarRepWfInstDTO.getReportVersion());
        ccarReportWorkflowInstance.setReportId(ccarRepWfInstDTO.getReportId());
        ccarRepWfInstRepository.save(ccarReportWorkflowInstance);
    } catch (Exception e) {
        log.info("Exception while saving workflow instance information =" + e);
        throw new CustomParameterizedException(e.getMessage());
    }
}

最佳答案

映射逻辑错过了日期格式。可以在mapstruct中定义如下:

@Mapping(target = "cobDate", source = "cobDate", dateFormat = "DD-MM-yyyy")
CcarReportWorkflowInstance convert(CcarRepWfInstDTO ccarRepWfInstDTO);

关于java - 覆盖Mapper类或客户映射器或属性映射中的方法-Spring Boot,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38591714/

10-16 16:12