Bean实体类转换
程序员文章站
2022-05-24 09:16:53
...
Bean实体类转换
对象copy工具有很多,像dozer,orika等等。项目中对接搜索商品接口,原先是对接的是第三方平台的选单网,后面发现搜索类别实在太少了,只有一种like模糊搜索,后来打算用维易接口,搜索种类相对丰富。两者的商品实体类属性字段都各有差异,所以就通过从维易获取的结果转成选单的,使用了类型装换。搞了个demo,做个记录,方便以后查看。
1.BaseDto
package com.slx.outer.bean;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
abstract class BaseDto<T> {
public BaseDto() {
}
public BaseDto(T t) {
}
abstract public T convertTo();
public void setStatus(String status){
throw new NotImplementedException();
}
public void check() {
}
@Override
public String toString() {
return GsonBuilderUtil.buildGson().toJson(this);
}
}
2.GsonBuilderUtil
package com.slx.outer.bean;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class GsonBuilderUtil {
public static Gson buildGson() {
return new GsonBuilder()
.disableHtmlEscaping()
.enableComplexMapKeySerialization()
.setDateFormat("yyyy-MM-dd HH:mm:ss")
.create();
}
}
3.UserDO
package com.slx.outer.bean;
import java.io.Serializable;
/**
* @Author: slx
* @Date: 2019/6/15 20:32
*/
public class UserDO extends BaseDto<UserDTO> implements Serializable {
private String userName;
private String passWord;
private String userType;
private String mobile;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public String getUserType() {
return userType;
}
public void setUserType(String userType) {
this.userType = userType;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
@Override
public UserDTO convertTo() {
UserDTO userDTO = new UserDTO();
userDTO.setUserName(this.userName);
userDTO.setUserType(this.userType);
userDTO.setMobile(this.mobile);
return userDTO;
}
@Override
public String toString() {
return "UserDO{" +
"userName='" + userName + '\'' +
", passWord='" + passWord + '\'' +
", userType='" + userType + '\'' +
", mobile='" + mobile + '\'' +
'}';
}
}
4、UserDTO
package com.slx.outer.bean;
/**
* @Author: slx
* @Date: 2019/6/15 20:33
*/
public class UserDTO {
private String userName;
private String userType;
private String mobile;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserType() {
return userType;
}
public void setUserType(String userType) {
this.userType = userType;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
@Override
public String toString() {
return "UserDTO{" +
"userName='" + userName + '\'' +
", userType='" + userType + '\'' +
", mobile='" + mobile + '\'' +
'}';
}
}
5.测试
package com.slx.outer.bean;
import org.junit.Test;
/**
* @Author: slx
* @Date: 2019/6/15 20:38
*/
public class UserTest {
@Test
public void Test() {
UserDO userDO = new UserDO();
userDO.setUserName("shuPush");
userDO.setPassWord("123456");
userDO.setUserType("VIP");
userDO.setMobile("15866666666");
UserDTO userDTO = userDO.convertTo();
System.out.println(userDO.toString());
System.out.println(userDTO.toString());
}
}
结果:
UserDO{userName='shuPush', passWord='123456', userType='VIP', mobile='15866666666'}
UserDTO{userName='shuPush', userType='VIP', mobile='15866666666'}