MapStruct在專案中封裝實踐-帶原始碼

DH鑌發表於2021-12-18
https://segmentfault.com/a/11... 這個文章有同學留意問有沒有原始碼,所以分離了出來,釋出到github上,方便的話,給個star!!!

程式碼倉庫:https://github.com/DHBin/maps...

mapstruct-helper

簡化mapstruct使用,靈感來源Spring Ioc。

使用方法


<dependency>
    <groupId>cn.dhbin</groupId>
    <artifactId>mapstruct-helper-core</artifactId>
    <version>1.0.0</version>
</dependency>

例子

// 第一步:定義Mapper,繼承cn.dhbin.mapstruct.helper.core.BeanConvertMapper
@Mapper
public interface FooToBooMapper extends BeanConvertMapper<FooBean, BooBean> {
}

// 第二步:掃描裝載Mapper(只需要配置一次)
BeanConvertMappers.config(MapperConfig.defaultConfig("package").build());

FooBean fooBean=new FooBean();
fooBean.setName("xxx");

// 使用
assertEquals("xxx",BeanConvertMappers.convert(fooBean,BooBean.class).getName());
assertEquals("xxx",BeanConvertMappers.convert(fooBean,new BooBean()).getName());

API

Mapper配置

cn.dhbin.mapstruct.helper.core.MapperConfig
BeanConvertMappers.config(
        MapperConfig.builder()
        // 是否支援待複製Bean的子類
        .supportSubclass(true)
        // mapper掃描器
        .mapperDefinitionScanner(new DefaultMapperDefinitionScanner("scanPackage"))
        // 轉換方法
        .convertFunction((mapper,source)->{
        return((BeanConvertMapper)mapper).to(source);
        })
        .build()

預設配置

// 預設不支援待複製Bean的子類
MapperConfig.defaultConfig("scanPackage").build()

擴充套件

預設是使用cn.dhbin.mapstruct.helper.core.BeanConvertMapper作為模板生成Mapper,但考慮到相容性問題,支援自定義模板。

比如專案中原來的模板如下:

public interface MyBeanConvertMapper<SOURCE, TARGET> {

    /**
     * source to target
     *
     * @param source source
     * @return target
     */
    TARGET convert(SOURCE source);

}

通過以下配置實現相容:

BeanConvertMappers.config(MapperConfig.builder()
        .supportSubclass(false)
        .mapperDefinitionScanner(new AbstractPackageMapperDefinitionScanner<MyBeanConvertMapper>("package") {

            @Override
            public Class<MyBeanConvertMapper> getMapperClass() {
                return MyBeanConvertMapper.class;
            }
        })
        .convertFunction((mapper, source) -> {
            return ((MyBeanConvertMapper) mapper).convert(source);
        })
        .build());

轉換/複製屬性

public static<T> T convert(Object source,Class<T> targetClass);
public static<T> T convert(Object source,T target);

與Spring整合

依賴

<dependency>
    <groupId>cn.dhbin</groupId>
    <artifactId>mapstruct-helper-starter</artifactId>
    <version>1.0.0</version>
</dependency>

使用

// 第一步:定義Mapper,繼承cn.dhbin.mapstruct.helper.core.BeanConvertMapper
@Mapper(componentModel = "spring")
public interface FooToBooMapper extends BeanConvertMapper<FooBean, BooBean> {
}


FooBean fooBean = new FooBean();
fooBean.setName("xxx");

// 直接使用BeanConvertMappers
assertEquals("xxx",BeanConvertMappers.convert(fooBean,BooBean.class).getName());
assertEquals("xxx",BeanConvertMappers.convert(fooBean,new BooBean()).getName());

// 擴充套件:繼承cn.dhbin.mapstruct.helper.core.MapperConfig注入到Ioc中

LICENSE

Copyright 2021 the original author or authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

相關文章