关于java:MapStruct在项目中封装实践带源码

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.

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理