orika java bean 转换工具试用
orika 是一个很不错的bean 转换工具包,以下是一个简单的使用
参考代码
- MyUser.java
/**
* Auto-generated: 2020-08-31 10:16:39
*
* @author bejson.com (i@bejson.com)
* @website http://www.bejson.com/java2pojo/
*/
public class MyUser {
@Override
public String toString() {
return "MyUser{" +
"url='" + url + '\'' +
", object=" + object +
'}';
}
private String url;
private Object2 object;
public void setUrl(String url) {
this.url = url;
}
public String getUrl() {
return url;
}
public void setObject(Object2 object) {
this.object = object;
}
public Object2 getObject() {
return object;
}
}
- Myuser2.java
/**
* Auto-generated: 2020-08-31 10:16:39
*
* @author bejson.com (i@bejson.com)
* @website http://www.bejson.com/java2pojo/
*/
public class MyUser2 {
@Override
public String toString() {
return "MyUser2{" +
"url='" + url2 + '\'' +
", object=" + object2 +
'}';
}
public String getUrl2() {
return url2;
}
public void setUrl2(String url2) {
this.url2 = url2;
}
public Object2 getObject2() {
return object2;
}
public void setObject2(Object2 object2) {
this.object2 = object2;
}
private String url2;
private Object2 object2;
}
- bean 转换
public static void appdemo(){
MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
mapperFactory.classMap(MyUser.class,MyUser2.class)
.field("url","url2")
.field("object","object2")
.byDefault().register();
MapperFacade mapper = mapperFactory.getMapperFacade();
MyUser myUser =new MyUser();
Object2 object2 =new Object2();
object2.setUrl("ddd");
myUser.setObject(object2);
myUser.setUrl("deee");
MyUser2 myUser2 = mapper.map(myUser,MyUser2.class);
System.out.println(myUser2);
}
pom.xml
<dependency>
<groupId>ma.glasnost.orika</groupId>
<artifactId>orika-core</artifactId>
<version>1.5.4</version><!-- or latest version -->
</dependency>
说明
orika 的功能还是很强大的,很值得使用下