Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/onlyeat3/fastmapper

超快的对象和属性映射工具.Blazing fast object and attribute mapping tool
https://github.com/onlyeat3/fastmapper

Last synced: about 1 month ago
JSON representation

超快的对象和属性映射工具.Blazing fast object and attribute mapping tool

Awesome Lists containing this project

README

        

# fastmapper
对象映射工具
## 特性
- [x] Java 8+
- [x] 自定义类型转换方式
- [x] 支持多层级对象拷贝(实验特性)
- [x] 泛型支持
## 使用说明
添加依赖到pom.xml
```xml

io.github.onlyeat3
fastmapper
0.0.1

```

```java
//bean to map
Map map = FastMapper.map(foo, new ClassReference>() {});
```

```java
//map to bean
Foo foo = FastMapper.map(map, Foo.class);
```
```java
//bean to bean
Bar bar = FastMapper.map(foo, Bar.class);
```
```java
//属性拷贝(忽略空的属性)
FastMapper.populate(foo,bar);
```

```java
//类方式写一个自定义TypeMapper(推荐)
@Order(5)//指定优先级,数字越小越靠前,但非预置转换器的优先级不会超过预置的
public class BigIntegerToLongTypeMapper implements TypeMapper {
@Override
public boolean match(MappingInfo mappingInfo) {
return mappingInfo.getSourceType().getRawClass().equals(BigInteger.class) && mappingInfo.getTargetType().getRawClass().equals(Long.class);
}

@Override
public Long map(MappingInfo mappingInfo) {
return mappingInfo.getSource().longValue();
}
}
FastMapper.registerTypeMapper(new BigIntegerToLongTypeMapper())
//lambda方式写自定义TypeMapper,从SomeEnum转换到Long,这样写会另外注册一个从Long到SomeEnum的TypeMapper
FastMapper.registerTypeMapper(SomeEnum.class, Long.class, someEnum-> someEnum.getValue().longValue(), aLong -> SomeEnum.valueOf(aLong.intValue()))
```

```java
//to list
List list = FastMapper.map(fooList, new TypeReference>() {});
```

```java
//泛型
List list = FastMapper.map(fooList, new TypeReference>() {});
```

以上就是全部功能,按照已有需求提前预置了一些`TypeMapper`,这些`TypeMapper`无法满足需要时可以自己添加`TypeMapper`,也可以在`ISSUE`提需求,由我来添加。

`fastmapper`不建议把bean写的嵌套过多,最好是单层的,但为了对付一些无奈的场景,还是提供了简单的深层拷贝支持.不需要额外配置,直接使用即可。