BeanTransferUtils.java
1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package org.nafmii.utils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* @author by zhangbr
* @date 2020/3/26.
*/
public class BeanTransferUtils {
public static <T> T transfer(Object text, Class<T> clazz) {
T t;
try {
t = clazz.newInstance();
org.springframework.beans.BeanUtils.copyProperties(text, t);
return t;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static <T> List<T> transferList(List<?> list, Class<T> clazz) {
if(CollectionUtils.isEmpty(list)){
return Collections.emptyList();
}
List<T> result =new ArrayList<>();
for(Object each : list) {
result.add(transfer(each,clazz));
}
return result;
}
public static <T> PagedResult<T> toPagedResult(List<T> list, Page page) {
PagedResult<T> result = new PagedResult<>();
if (list != null && !list.isEmpty()){
result.setList(list);
if (page != null){
result.setTotal(page.getTotal());
}else {
result.setTotal(list.size());
}
}
return result;
}
public static <T> PagedResult<T> toPagedResult(List<?> list, Class<T> clazz, Page page) {
PagedResult<T> result = new PagedResult<>();
if (page != null){
result.setTotal(page.getTotal());
}else {
if (list != null){
result.setTotal(list.size());
}
}
if (list != null && !list.isEmpty()){
result.setList(BeanTransferUtils.transferList(list, clazz));
}
return result;
}
}