类复制基类,service 可以继承该类,实现业务层与数据层之间的转换
* 两种继承方式调用相关转换方法
* 1、继承时传入泛型具体的类型<B, P>
* 2、继承时不带具体类型,即泛型擦除
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.List;
/**
* @ClassName: BaseConverter
* @Description: bo 和 po 转换类
* @Author TanXiongZhan
* @Date 2019/6/18
*/
public abstract class BaseConverter<B, P> {
private final static Logger logger = LoggerFactory.getLogger(BaseConverter.class);
private Class<B> bClass;
private Class<P> pClass;
public BaseConverter() {
try {
ParameterizedType superClass = (ParameterizedType) getClass().getGenericSuperclass();
bClass = (Class<B>) superClass.getActualTypeArguments()[0];
pClass = (Class<P>) superClass.getActualTypeArguments()[0];
} catch (Exception e) {
}
}
/* 泛型擦除时调用的方法 */
/**
* 泛型擦除时调用
*
* 列表转换【无类型继承】
* 数据库数据-->业务数据
*
* @param pos
* @return
*/
protected List<B> po2BOList(List<P> pos, Class<B> bClass) {
this.bClass = bClass;
List<B> bos = new ArrayList<>();
if (pos instanceof List) {
for (P poo : pos) {
bos.add(po2BO(poo));
}
}
return bos;
}
/**
* 泛型擦除时调用
*
* 列表转换
* 业务数据-->数据库数据
*
* @param bos
* @return
*/
protected List<?> bo2POList(List<B> bos, Class<P> pClass) {
this.pClass = pClass;
List<P> poss = new ArrayList<>();
if (bos instanceof List) {
for (B boo : bos) {
poss.add(bo2PO(boo));
}
}
return poss;
}
/**
* page转换
* 数据库数据-->业务数据
*
* @param pageList
* @return
*/
protected PageList<B> po2BOPage(PageList<P> pageList, Class<B> bClass) {
this.bClass = bClass;
PageList<B> pageListB = new PageList();
copySameProperties(pageList, pageListB);
pageListB.setList(po2BOList(pageList.getList()));
return pageListB;
}
/* 具体类型继承调用的方法 */
/**
* page转换
* 数据库数据-->业务数据
*
* @param pageList
* @return
*/
protected PageList<B> po2BOPage(PageList<P> pageList) {
PageList<B> pageListB = new PageList();
copySameProperties(pageList, pageListB);
pageListB.setList(po2BOList(pageList.getList()));
return pageListB;
}
/**
* 列表转换
* 数据库数据-->业务数据
*
* @param pos
* @return
*/
protected List<B> po2BOList(List<P> pos) {
List<B> bos = new ArrayList<>();
if (pos instanceof List) {
for (P poo : pos) {
bos.add(po2BO(poo));
}
}
return bos;
}
/**
* 列表转换
* 业务数据-->数据库数据
*
* @param bos
* @return
*/
protected List<P> bo2POList(List<B> bos) {
List<P> poss = new ArrayList<>();
if (bos instanceof List) {
for (B boo : bos) {
poss.add(bo2PO(boo));
}
}
return poss;
}
/**
* 业务数据-->数据库数据
* bo转po
*
* @param bo
*/
protected P bo2PO(B bo) {
P result = null;
if (bo != null) {
try {
result = pClass.newInstance();
copySameProperties(bo, result);
copyDiffPropertiesFromBO2PO(bo, result);
} catch (InstantiationException e) {
logger.info("对象属性值复制出错:原数据为{" + bo + "}, 目标数据为{" + pClass + "}。");
e.printStackTrace();
} catch (IllegalAccessException e) {
logger.info("对象属性值复制出错:原数据为{" + bo + "}, 目标数据为{" + pClass + "}。");
e.printStackTrace();
}
}
return result;
}
/**
* 数据库数据-->转业务数据
* po 转 bo
* @param po
* @return
*/
protected B po2BO(P po) {
B result = null;
if (po != null) {
try {
result = bClass.newInstance();
copySameProperties(po, result);
copyDiffPropertiesFromPO2BO(po, result);
} catch (InstantiationException e) {
logger.info("对象属性值复制出错:原数据为{" + po + "}, 目标数据为{" + bClass + "}。");
e.printStackTrace();
} catch (IllegalAccessException e) {
logger.info("对象属性值复制出错:原数据为{" + po + "}, 目标数据为{" + pClass + "}。");
e.printStackTrace();
}
}
return result;
}
/* 基础方法 */
/**
* po转bo
*
* @param bo
* @param po
*/
protected void po2BO(P po, B bo) {
copySameProperties(po, bo);
copyDiffPropertiesFromPO2BO(po, bo);
}
/**
* bo转 po
*
* @param bo
* @param po
*/
protected void bo2PO(B bo, P po) {
copySameProperties(bo, po);
copyDiffPropertiesFromBO2PO(bo, po);
}
/**
* 同名属性复制
*
* @param target 目标对象
* @param source 来源对象
*/
protected void copySameProperties(Object source, Object target) {
try {
BeanUtils.copyProperties(source, target);
} catch (Exception e) {
logger.info("对象属性值复制出错:原数据为{" + source + "}, 目标数据为{" + target + "}。");
}
}
/**
* BO非同名属性复制到PO属性
*
* @param target 域对象
* @param source 值对象
*/
public abstract void copyDiffPropertiesFromBO2PO(B source, P target);
/**
* PO非同名属性复制到BO属性
*
* @param target 值对象
* @param source 域对象
*/
public abstract void copyDiffPropertiesFromPO2BO(P source, B target);
}
详细例子:
链接:点击打开
提取码:cqcl
-
-
-
- 完毕