# easy-mapper **Repository Path**: itor/easy-mapper ## Basic Information - **Project Name**: easy-mapper - **Description**: 基于注解的easy-mapper - **Primary Language**: Java - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2019-04-02 - **Last Updated**: 2021-08-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # easy-mapper #### 介绍 # Easy-mapper [![Build Status](https://travis-ci.org/neoremind/easy-mapper.svg?branch=master)](https://travis-ci.org/neoremind/easy-mapper) [![Coverage Status](https://coveralls.io/repos/github/neoremind/easy-mapper/badge.svg?branch=master)](https://coveralls.io/github/neoremind/easy-mapper?branch=master) [![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.baidu.unbiz/easy-mapper/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.baidu.unbiz/easy-mapper) [![Hex.pm](https://img.shields.io/hexpm/l/plug.svg)](http://www.apache.org/licenses/LICENSE-2.0) Easy-mapper is a simple, light-weighted, high-performance java bean mapping framework. By leveraging Javassist, easy mapper can generate mapping byte-code at runtime and load them into JVM for later invocations to reduce some of the overhead. Easy-mapper not only provides a relatively high-performance mapping solution, but also enables the caller to do mapping in a more flexible and extensible way. Fluent interface style API and Java8 lambda API, these modern techniques can be fully used to customize your own mapping strategy. Here, easy-mapper uses by-reference field mapping strategy most of the time except for some immutable types like primitive, wrapper, String and BigDecimal, etc. When you don’t need to copy and clone field, by-reference mapping is capable to process your business logic and avoid the overhead of performance loss. On the other hand, easy-mapper respects immutability and do not mean to offense, just provide an alternative solution. For performance test result, please refer to the benchmark section. 中文手册[请点这里](http://neoremind.com/2016/08/easy-mapper-%E4%B8%80%E4%B8%AA%E7%81%B5%E6%B4%BB%E5%8F%AF%E6%89%A9%E5%B1%95%E7%9A%84%E9%AB%98%E6%80%A7%E8%83%BDbean-mapping%E7%B1%BB%E5%BA%93/)。 [GITHUB传送门](http://github.com/neoremind/easy-mapper) #### 说明 1. easy-mapper [作者](http://https://github.com/neoremind) 是我非常喜欢的程序员之一 2. easy-mapper 的源码非常简洁,注释非常清晰 3. easy-mapper 功能强大,但是有些地方还是不够完美 基于以上三个出发点,笔者修改了easy-mapper的源码,希望能够踩在巨人的肩膀上,让代码更加完美,更加方便。 #### 使用环境 JDK 1.8 + #### 新增功能 1. 支持注解 @CopyBean @CopyBeanProperty 2. 支持冗余字段映射 3. 封装统一copy工具类 BeanCopyUtil.java,增加支持 **List to List** 转换 4. 支持原值倍数扩大缩小及精度设置 #### 使用 1. 注解的使用 StudentEntity.java ``` public class StudentEntity { private String name; private Integer age; private Integer status; private String password; private Date createTime; //getter and setter } ``` StudentInfo.java ``` @CopyBean(sourceClass = StudentEntity.class,excludeFeilds = {"password"}) public class StudentInfo { private String name; private Integer age; private Integer status; @CopyBeanProperty(sourceField = "status", sourceFieldFormatType = FormatType.ENUM_FORMAT, enumFormatClazz = StatusEnum.class) private String statusName; private String password; private Date createTime; @CopyBeanProperty(sourceField = "createTime", sourceFieldFormatType = FormatType.DATE_TIME) private String createTimeStr; //getter and setter } FormatType.DATE_TIME 为时间格式化,默认时间格式 yyyy-MM-dd HH:mm:ss FormatType.ENUM_FORMAT 为枚举格式化 FormatType.NOT_FORMAT 默认值,不格式化 FormatType.SHRINK 缩小倍数 可设置 times(倍数) precision(精度) FormatType.MAGNIFY 扩大倍数 可设置 times(倍数) precision(精度) ``` StatusEnum.java ``` DETELETED(1, "已删除"), NOTDELETEED(2,"未删除"); public static String getDescribe(Object code) { for (StatusEnum ele : values()) { if(ele.statusCode.equals(code)){ return ele.getStatusName(); } } return null; } private Integer statusCode; private String statusName; //getter and setter ``` 如果使用了 @CopyBeanProperty 注解,且 **sourceFieldFormatType = FormatType.ENUM_FORMAT** ,注解属性 **enumFormatClazz** 枚举类中必须定义 **getDescribe(Object obj)** 方法,否则无法格式化对应的属性值。 ``` @Test public void testBeanToBean() { long startTime = System.currentTimeMillis(); StudentEntity s = new StudentEntity(); s.setAge(23); s.setCreateTime(new Date()); s.setPassword("adfasdf"); s.setStatus(1); s.setName("张三"); StudentInfo so = BeanCopyUtil.mapObject(s, StudentInfo.class); long endTime = System.currentTimeMillis(); System.out.println(" beanToBeantakes " + (endTime - startTime) +"ms"); } ``` 结果: ``` StudentInfo{ name='张三', age=23, status=1, statusName='已删除', password='null', createTime=Tue Apr 02 20:37:34 CST 2019, createTimeStr='2019-04-02 20:37:34' } ``` ``` @Test public void testListToList() { List studentins = Lists.newArrayList(); StudentEntity s = new StudentEntity(); s.setAge(23); s.setCreateTime(new Date()); s.setPassword("adfasdf"); s.setStatus(1); s.setName("张三"); for (int i = 0; i < 10000; i++) { s.setName("张三" + i); studentins.add(s); } long startTime = System.currentTimeMillis(); List sts = BeanCopyUtil.mapObjectList(studentins, StudentInfo.class); long endTime = System.currentTimeMillis(); System.out.println(" testListToList takes " + (endTime - startTime) +"ms"); } ``` 结果 ``` StudentInfo{ name='张三1', age=23, status=1, statusName='已删除', password='null', createTime=Tue Apr 02 20:37:34 CST 2019, createTimeStr='2019-04-02 20:37:34' },StudentInfo{ name='张三2', age=23, status=1, statusName='已删除', password='null', createTime=Tue Apr 02 20:37:34 CST 2019, createTimeStr='2019-04-02 20:37:34' }.... ```