本文主要是介绍【Dozer】数据映射之bean映射,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
项目研发过程中,避免不了和第三方系统交互,那么就会出现对同一字段,命名方式不同了,这个是比较让人头疼的事情,所以,今天就学习一下数据映射插件Dozer。
直接上代码:
1、maven引入dozer jar包:
<dependency><groupId>com.github.dozermapper</groupId><artifactId>dozer-core</artifactId><version>6.4.0</version>
</dependency>
2、java代码:
package cn.gtmap.estateplat.olcommon.util;import com.github.dozermapper.core.DozerBeanMapperBuilder;
import com.github.dozermapper.core.Mapper;
import com.github.dozermapper.core.Mapping;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Component;import java.util.List;/*** @author <a href="mailto:934804229@qq.com">Tastill</a>* @version 2019/9/3 11:24* @description DozerUtils*/
@Component
public class DozerUtils {/*** @description Here's demo* @param* @return* @version 2.0, 2019/9/3 14:05* @author <a href="mailto:934804229@qq.com">Tastill</a>*/public static void main(String[] args){Mapper mapper = DozerBeanMapperBuilder.buildDefault();A a = new A();a.setStudentName("张三");a.setAge(12);B b = mapper.map(a, B.class);System.out.println(b);}static class A{@Mapping("name")private String studentName;private Integer age;public String getStudentName() {return studentName;}public void setStudentName(String studentName) {this.studentName = studentName;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}@Overridepublic String toString() {return "A{" +"studentName='" + studentName + '\'' +", age=" + age +'}';}}static class B{private String name;private Integer age;public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}@Overridepublic String toString() {return "B{" +"name='" + name + '\'' +", age=" + age +'}';}}
}
这篇关于【Dozer】数据映射之bean映射的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!