亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Mapstruct對象插入數(shù)據(jù)庫某個字段總是為空的bug詳解

 更新時間:2022年07月25日 11:33:22   作者:女友在高考  
這篇文章主要為大家介紹了在一次需求開發(fā)Mapstruct中對象插入數(shù)據(jù)庫某個字段總是為空的bug問題詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

前言

在一次需求的開發(fā)過程中,發(fā)現(xiàn)一個對象插入數(shù)據(jù)庫時某個字段總是為空。

版本:lombok:1.18.24、mapstruct:1.5.2.Final

簡化后的代碼如下:

@Autowired
    private PersonService personService;
    public void test1(){
        Person person = personService.findById(1L);
        PersonDto personDto = PersonMapper.INSTANCE.personToPersonDto(person);
        personService.insert(personDto);
    }

這么簡單的邏輯按理說不會出幺蛾子啊,我先排查了數(shù)據(jù)庫里person id=1的記錄發(fā)現(xiàn)值是有的啊,然后又排查了我的insert方法,也是沒問題的。

經(jīng)過一段時間的排查,才發(fā)現(xiàn)是

PersonDto personDto = PersonMapper.INSTANCE.personToPersonDto(person);

這行代碼的問題。證據(jù)截圖如下:

前面的時候addTeacherNum還有值,轉(zhuǎn)化后怎么又沒值了呢?

大家看到這里肯定猜測是不是我屬性名不對,或者屬性類型不對。我甚至還刪除了之后用復(fù)制的方式來保證沒有手敲敲錯的情況。

完全是一模一樣的屬性啊。

我們知道m(xù)apstruct是編譯時通過我們的PersonMapper接口來實現(xiàn)實現(xiàn)類,實現(xiàn)類里是setter、getter方法來實現(xiàn)的。于是我打開了PersonMapper的實現(xiàn)類準備一探究竟:

public class PersonMapperImpl implements PersonMapper {
    public PersonMapperImpl() {
    }
    public PersonDto personToPersonDto(Person person) {
        if (person == null) {
            return null;
        } else {
            PersonDtoBuilder personDto = PersonDto.builder();
            personDto.name(person.getName());
            return personDto.build();
        }
    }
}

竟然沒有對我這個屬性addTeacherNum進行賦值。這讓我百思不得其解。只能去看看源碼,試圖找出原因。

如何調(diào)試Maven插件

前面我們提到mapstruct是在代碼編譯的時候就開始生成代碼了,于是我們需要對maven編譯期進行調(diào)試。方法如下:

  • maven debug命令
mvnDebug clean compile
  • idea遠程debug

新建一個remote,然后修改端口為8000,然后在執(zhí)行maven命令的同時,啟動這個remote即可。

源碼解析

斷點應(yīng)該打在哪里呢?

我們查看mapstruct的結(jié)構(gòu),一般先從配置的文件入手

找到了這個MappingProcessor類,我們可以看到這里面有個process方法,里面又調(diào)用了如下的這個方法:

private void processMapperTypeElement(ProcessorContext context, TypeElement mapperTypeElement) {
    Object model = null;
    for ( ModelElementProcessor<?, ?> processor : getProcessors() ) {
        try {
            model = process( context, processor, mapperTypeElement, model );
        }
        catch ( AnnotationProcessingException e ) {
           //省略
        }
    }
}

這段代碼其實就是調(diào)用getProcessors()方法拿到多個processor,然后遍歷調(diào)用。而這個getProcessors()就是從配置文件里通過SPI的方式加載對象。

這里面我們重點關(guān)注這個Processor:MapperCreationProcessor。它的process方法如下:

@Override
public Mapper process(ProcessorContext context, TypeElement mapperTypeElement, List<SourceMethod> sourceModel) {
    this.elementUtils = context.getElementUtils();
    this.typeUtils = context.getTypeUtils();
    this.messager =
        new MapperAnnotatedFormattingMessenger( context.getMessager(), mapperTypeElement, context.getTypeUtils() );
    this.options = context.getOptions();
    this.versionInformation = context.getVersionInformation();
    this.typeFactory = context.getTypeFactory();
    this.accessorNaming = context.getAccessorNaming();
    MapperOptions mapperOptions = MapperOptions.getInstanceOn( mapperTypeElement, context.getOptions() );
    List<MapperReference> mapperReferences = initReferencedMappers( mapperTypeElement, mapperOptions );
    MappingBuilderContext ctx = new MappingBuilderContext(
        typeFactory,
        elementUtils,
        typeUtils,
        messager,
        accessorNaming,
        context.getEnumMappingStrategy(),
        context.getEnumTransformationStrategies(),
        options,
        new MappingResolverImpl(
            messager,
            elementUtils,
            typeUtils,
            typeFactory,
            new ArrayList<>( sourceModel ),
            mapperReferences,
            options.isVerbose()
        ),
        mapperTypeElement,
        //sourceModel is passed only to fetch the after/before mapping methods in lifecycleCallbackFactory;
        //Consider removing those methods directly into MappingBuilderContext.
        Collections.unmodifiableList( sourceModel ),
        mapperReferences
    );
    this.mappingContext = ctx;
    return getMapper( mapperTypeElement, mapperOptions, sourceModel );
}

getMapper里面有一段這個方法引起我的注意:

List<MappingMethod> mappingMethods = getMappingMethods( mapperOptions, methods );

猜測這段就是獲取要寫入的set、get方法。 于是一路跟蹤:

發(fā)現(xiàn)mapstruct里面把方法分為了下面四類,而我的addTeacherNum屬性通過lombok生成的方法methodType被分到了ADDER里面。

而在生成我的Mapper實現(xiàn)類的時候它會只過濾setter方法。

List<Accessor> candidates = new ArrayList<>( getSetters() );

至此真相大白。

以上就是Mapstruct中對象插入數(shù)據(jù)庫某個字段總是為空的bug詳解的詳細內(nèi)容,更多關(guān)于Mapstruct插入數(shù)據(jù)字段為空的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論