BO(Business Object):业务对象,由 Service 层输出的封装业务逻辑的对象。进行对外接口的编写,往往需要返回BO(Entity中很多属性字段并不是需要的,BO只是选取需要的部分在Service进行返回),但是通过DAO层我们获取到的往往是Entity的形式,因此往往需要进行Entity到BO的转换,因此写了一个工具接口,完成这种转换:
public static<S extends Object,D extends Object> List<D> EntityList2BoList(List<S> source,Class<S> sClass,Class<D> dClass){
List<D> ds = new ArrayList<D>();
if(null == source){
return ds;
}
try {
for (S s : source) {
D d = dClass.newInstance();
BeanUtils.copyProperties(s, d);
ds.add(d);
}
}
catch (Exception e){
LOG.error("An error occurred while attempting to convert entity" +
"[" + sClass.getName() + "] to bo ["+ dClass.getName() +"], error infor :" + e.getMessage());
return new ArrayList<D>();
}
//LOG.info("Success to convert entity [" + sClass.getName() + "] to bo ["+ dClass.getName() +"]");
return ds;
}
DwUtil.EntityList2BoList(dwEmployeeEntities,AEntity.class,ABo.class);
其中BeanUtils的使用可以参考:
https://blog.csdn.net/Megustas_JJC/article/details/53525026