| | |
| | | package com.ruoyi.common.utils;
|
| | |
|
| | | import org.springframework.beans.BeanUtils;
|
| | |
|
| | | import java.util.List;
|
| | | import java.util.stream.Collectors;
|
| | |
|
| | | public class BeanCopyUtils {
|
| | |
|
| | | private BeanCopyUtils() {
|
| | | }
|
| | |
|
| | | /**练习
|
| | | public static Object copyBean(Object source, Class clazz) {
|
| | | //创建目标对象
|
| | | Object result = null;
|
| | | try {
|
| | | result = clazz.newInstance();
|
| | | //实现属性copy
|
| | | BeanUtils.copyProperties(source, result);
|
| | |
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | //返回结果
|
| | | return result;
|
| | | }*/
|
| | |
|
| | | public static <V> V copyBean(Object source,Class<V> clazz) {
|
| | | //创建目标对象
|
| | | V result = null;
|
| | | try {
|
| | | result = clazz.newInstance();
|
| | | //实现属性copy
|
| | | BeanUtils.copyProperties(source, result);
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | //返回结果
|
| | | return result;
|
| | | }
|
| | |
|
| | | /**练习
|
| | | * public static <V> List<V> copyBeanList(List<Object> list, Class<V> clazz){
|
| | | return list.stream()
|
| | | .map(o -> copyBean(o, clazz))
|
| | | .collect(Collectors.toList());
|
| | | }*/
|
| | |
|
| | | public static <O,V> List<V> copyBeanList(List<O> list, Class<V> clazz){
|
| | | return list.stream()
|
| | | .map(o -> copyBean(o, clazz))
|
| | | .collect(Collectors.toList());
|
| | | }
|
| | | }
|
| | | package com.ruoyi.common.utils; |
| | | |
| | | import org.springframework.beans.BeanUtils; |
| | | |
| | | import java.util.List; |
| | | import java.util.stream.Collectors; |
| | | |
| | | public class BeanCopyUtils { |
| | | |
| | | private BeanCopyUtils() { |
| | | } |
| | | |
| | | /**练习 |
| | | public static Object copyBean(Object source, Class clazz) { |
| | | //创建目标对象 |
| | | Object result = null; |
| | | try { |
| | | result = clazz.newInstance(); |
| | | //实现属性copy |
| | | BeanUtils.copyProperties(source, result); |
| | | |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | //返回结果 |
| | | return result; |
| | | }*/ |
| | | |
| | | public static <V> V copyBean(Object source,Class<V> clazz) { |
| | | //创建目标对象 |
| | | V result = null; |
| | | try { |
| | | result = clazz.newInstance(); |
| | | //实现属性copy |
| | | BeanUtils.copyProperties(source, result); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | //返回结果 |
| | | return result; |
| | | } |
| | | |
| | | /**练习 |
| | | * public static <V> List<V> copyBeanList(List<Object> list, Class<V> clazz){ |
| | | return list.stream() |
| | | .map(o -> copyBean(o, clazz)) |
| | | .collect(Collectors.toList()); |
| | | }*/ |
| | | |
| | | public static <O,V> List<V> copyBeanList(List<O> list, Class<V> clazz){ |
| | | return list.stream() |
| | | .map(o -> copyBean(o, clazz)) |
| | | .collect(Collectors.toList()); |
| | | } |
| | | } |