https://segmentfault.com/a/1190000040583959 Some students paid attention to the source code of 161bc51a3aa0c5, so I separated it and published it on github. If it is convenient, give it a star! ! !
Code repository: https://github.com/DHBin/mapstruct-helper
mapstruct-helper
Simplify the use of mapstruct, inspired by Spring Ioc.
Instructions
<dependency>
<groupId>cn.dhbin</groupId>
<artifactId>mapstruct-helper-core</artifactId>
<version>1.0.0</version>
</dependency>
example
// 第一步:定义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 configuration
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()
default allocation
// 默认不支持待复制Bean的子类
MapperConfig.defaultConfig("scanPackage").build()
Expand
The default is to use cn.dhbin.mapstruct.helper.core.BeanConvertMapper
as a template to generate Mapper, but considering compatibility issues, custom templates are supported.
For example, the original template in the project is as follows:
public interface MyBeanConvertMapper<SOURCE, TARGET> {
/**
* source to target
*
* @param source source
* @return target
*/
TARGET convert(SOURCE source);
}
Compatibility is achieved through the following configuration:
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());
Convert/copy attributes
public static<T> T convert(Object source,Class<T> targetClass);
public static<T> T convert(Object source,T target);
Integration with Spring
rely
<dependency>
<groupId>cn.dhbin</groupId>
<artifactId>mapstruct-helper-starter</artifactId>
<version>1.0.0</version>
</dependency>
use
// 第一步:定义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.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。