實體類
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);
});
}