package com.eloancn.nback.common.utils;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* VO PO 转换帮助类
*
* @author nyy
* @param <S> PO类
* @param <T> VO类
*/
public class VoPoConvertUtil<S, T> {
protected Logger logger = LoggerFactory.getLogger(this.getClass());
private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public SimpleDateFormat getFormat() {
return format;
}
public void setFormat(SimpleDateFormat format) {
this.format = format;
}
private List<String> excludeFields = null;
/**
* 构造VO PO 转换帮助类
*
*/
public VoPoConvertUtil() {
}
/**
* 构造VO PO 转换帮助类
* @param excludeFields 排除字段集合
*/
public VoPoConvertUtil(List<String> excludeFields) {
this.excludeFields = excludeFields;
}
/**
* PO转VO
* @param source PO对象
* @param target VO对象
*/
public void convert(S source, T target) {
if (source == null || target == null) {
return;
}
convert(source, target, source.getClass(), target.getClass());
}
/**
* PO转VO 自定义时间类型属性样式
* @param source PO对象
* @param target VO对象
*/
public void convert(S source, T target,String dateFormat) {
if (source == null || target == null) {
return;
}
convert(source, target, source.getClass(), target.getClass(),dateFormat);
}
/**
* 自定义时间类型属性样式
* @param source
* @param target
* @param sc
* @param tc
* @param dateFormat 时间类型属性样式
* @author YYJ
* @date 2016年6月1日
*/
private void convert(S source, T target, Class<?> sc, Class<?> tc,String dateFormat) {
SimpleDateFormat fmt = new SimpleDateFormat(dateFormat);
Class<?> scs = sc.getSuperclass();
if (scs != null) {
convert(source, target, scs, tc.getSuperclass(), dateFormat);
}
for (Field field : tc.getDeclaredFields()) {
field.setAccessible(true);
if (notFlterField(field.getName())) {
try {
Field sourceField = null;
try {
sourceField = sc.getDeclaredField(field.getName());
} catch (Exception e) {
//vo对象中有的变量在po中不存在,不需要转换,不需要打印错误日志
}
if(sourceField != null){
sourceField.setAccessible(true);
Type type = sourceField.getGenericType();
Object value = sourceField.get(source);
if (value == null) {
continue;
} else if (type.equals(String.class)) {
field.set(target, value);
} else if (type.equals(BigDecimal.class)) {
field.set(target, String.valueOf(value));
} else if (type.equals(Date.class)) {
String ddd = fmt.format((Date) value);
field.set(target, ddd);
} else if (type.equals(Long.class) || type.equals(long.class)) {
field.set(target, String.valueOf(value));
} else if (type.equals(Double.class) || type.equals(double.class)) {
field.set(target, String.valueOf(value));
} else if (type.equals(Integer.class) || type.equals(int.class)) {
field.set(target, String.valueOf(value));
} else if (type.equals(Float.class) || type.equals(float.class)) {
field.set(target, String.valueOf(value));
} else if (type.equals(Byte.class) || type.equals(byte.class)) {
field.set(target, String.valueOf(value));
}
}
} catch (Exception e) {
logger.info("Po转VO发生了异常", e);
}
}
}
}
private void convert(S source, T target, Class<?> sc, Class<?> tc) {
Class<?> scs = sc.getSuperclass();
if (scs != null) {
convert(source, target, scs, tc.getSuperclass());
}
for (Field field : tc.getDeclaredFields()) {
field.setAccessible(true);
if (notFlterField(field.getName())) {
try {
Field sourceField = null;
try {
sourceField = sc.getDeclaredField(field.getName());
} catch (Exception e) {
//vo对象中有的变量在po中不存在,不需要转换,不需要打印错误日志
}
if(sourceField != null){
sourceField.setAccessible(true);
Type type = sourceField.getGenericType();
Object value = sourceField.get(source);
if (value == null) {
continue;
} else if (type.equals(String.class)) {
field.set(target, value);
} else if (type.equals(BigDecimal.class)) {
field.set(target, String.valueOf(value));
} else if (type.equals(Date.class)) {
field.set(target, format.format((Date) value));
} else if (type.equals(Long.class) || type.equals(long.class)) {
field.set(target, String.valueOf(value));
} else if (type.equals(Double.class) || type.equals(double.class)) {
field.set(target, String.valueOf(value));
} else if (type.equals(Integer.class) || type.equals(int.class)) {
field.set(target, String.valueOf(value));
} else if (type.equals(Float.class) || type.equals(float.class)) {
field.set(target, String.valueOf(value));
} else if (type.equals(Byte.class) || type.equals(byte.class)) {
field.set(target, String.valueOf(value));
}
}
} catch (Exception e) {
logger.info("Po转VO发生了异常", e);
}
}
}
}
/**
* VO 还原 PO
*
* @param source
* VO 对象
* @param target
* PO 对象
*/
public void revert(T source, S target) {
if (source == null || target == null) {
return;
}
revert(source, target, source.getClass(), target.getClass());
}
private void revert(T source, S target, Class<?> sc, Class<?> tc) {
Class<?> scs = sc.getSuperclass();
if (scs != null) {
revert(source, target, scs, tc.getSuperclass());
}
for (Field field : sc.getDeclaredFields()) {
field.setAccessible(true);
if (notFlterField(field.getName())) {
try {
Field targetField = tc.getDeclaredField(field.getName());
targetField.setAccessible(true);
Type type = targetField.getGenericType();
String value = null;
try {
value = (String) field.get(source);
} catch (Exception e) {
value = String.valueOf(field.get(source));
}
if (value == null || (type.equals(String.class) == false && StringUtils.isEmpty(value.trim()))) {
continue;
} else if (type.equals(String.class)) {
targetField.set(target, value);
} else {
if (type.equals(Long.class)) {
targetField.set(target, Long.valueOf(value));
} else if (type.equals(Double.class) || type.equals(double.class)) {
targetField.set(target, Double.valueOf(value));
} else if (type.equals(Integer.class) || type.equals(int.class)) {
targetField.set(target, Integer.valueOf(value));
} else if (type.equals(Float.class) || type.equals(float.class)) {
targetField.set(target, Float.valueOf(value));
} else if (type.equals(Byte.class) || type.equals(byte.class)) {
targetField.set(target, Byte.valueOf(value));
} else if (type.equals(Date.class)) {
if (", ".equals(value) == false) {
if (value.split(" ").length < 2) {
value = value + " 00:00:00";
}
targetField.set(target, format.parse(value));
}
} else if (type.equals(BigDecimal.class)) {
BigDecimal bigDecimal = new BigDecimal(value);
bigDecimal.setScale(4, BigDecimal.ROUND_HALF_UP);
targetField.set(target, bigDecimal);
}
}
} catch (Exception e) {
logger.info("Vo转Po发生了异常", e);
}
}
}
}
/**
* 不排除的字段
*
* @param fieldName
* @return boolean
* @exception
* @since 1.0.0
*/
private boolean notFlterField(String fieldName) {
return "serialVersionUID".equals(fieldName) == false
&& (excludeFields == null || !excludeFields.contains(fieldName));
}
}
相关推荐
自身关联的添加、检索及PO到VO得封装、转换
po与vo区别.docpo与vo区别.docpo与vo区别.doc
关于VO、PO的理解——java的(PO,VO,TO,BO,DAO,POJO)解释
简单的vopo转换功能及依赖jar
一款自动生成POVO 的工具,双击即可 运行,默认配置文件:lib/defaultconfig.properties
Java的几种对象(PO-VO-DAO-BO-POJO)解释
Java中 PO VO BO DTO DAO 和 POJO 关系图
介绍vo,bo,po的区别。看其他网站收集的资料。
java(PO,VO,BO,DAO,POJO)Explained Collection 详解
java的几种对象(PO_VO_DAO_BO_POJO)解释
首先 介绍Java的缩写,给一些入门Java的帮助,
JAVA 中 的 什 么 是 POJO、VO、PO、DO、DTO 都 是 什 么 ? 他 们 有 什 么 区 别 ?
NULL 博文链接:https://ewf-momo.iteye.com/blog/1738853
J2EE开发人员必须知道 Java Web开发中VO、PO、DTO、POJO代表含义。
J2EE基础知识之DTO,VO,PO,DO等定义J2EE基础知识之DTO,VO,PO,DO等定义J2EE基础知识之DTO,VO,PO,DO等定义
如果想生成未预置的代码,如vo对象、前端vue页面以及其他自定义代码等,需要做一些定制和扩展来实现。 从使用角度而言,官方资料介绍得比较简要,要实际使用,仍然需要进行具体的尝试和摸索,包括必要时看下源码...
本人以前搞不懂这些o的区别,特意查找资料总结了一下,希望也可以帮到其他人
主要介绍了java中PO、VO、BO、POJO、DAO、DTO、TO、QO、Bean、conn的理解,需要的朋友可以参考下