java带List实体的集合转换

 

实体类

public class PageResult<T>{
int page;
int limit;
Long count;
String code;
string msg;
List<T> data;
T example;
public PageResult() {
}
}

 

 

转换的utils

/**
*list集合转换
* @param resultT 转换前数据
*@param resultw 转换后的实体类
@param classW 实体类
@param <T>
@param <W>
*throws Exception*
*/
public static <T,W> void copyConvert(PageResult<T> resultT,PageResult<W> resultw,class<W> classW) throws Exception 3
BeanUtils.copyProperties(resultT,resultw);
List<W> list=new ArrayList<>();
copyConvertIncluseList(resultT.getData(),list,classW); resultW.setData(list); }
/** *list集合转换,不支持实体类里面还有list * @param t 源集合 *@param w转换后的集合 @param classW 实体类 @param <T> @param <W> *throws Exception* */ public static <T,W> void copyConvert(List<T> t,List<W> w,class<W> classW) throws Exception { if(t==null){ w=null; return;
} t.stream().forEach(m
->{ w dto = null; try { dto = classw.newInstance(); } catch (InstantiationException e){ throw new RuntimeException(e); } catch (IllegalAccessException e){ throw new RuntimeException(e); } Beanutils.copyProperties(m,dto); w.add(dto); }); }
/**
*list集合转换,不支持实体类里面还有list
* @param t 源集合
*@param w转换后的集合
@param classW 实体类
@param <T>
@param <W>
*throws Exception*
*/
public static <T,W> void copyConvertIncludeList(List<T> t,List<W> w,class<W> classW) throws Exception {
if(t==null){
w=null; return;
} t.stream().forEach(m->{ String str=JSON.toJSONString(m); W w1=JSONObject.parseObject(str,classW) w.add(w1); }); }
 

 

posted @ 2023-11-14 15:58  ☆♂安♀★  阅读(123)  评论(0编辑  收藏  举报