Created
August 26, 2024 06:26
-
-
Save codethereforam/c8bc31c244ef3c8c5491ca3869346710 to your computer and use it in GitHub Desktop.
use spring BeanCopier copy beans
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * BeanHelper | |
| * | |
| * @author yanganyu | |
| */ | |
| @UtilityClass | |
| public class BeanHelper { | |
| public static <S, T> T copy(BeanCopier beanCopier, S source, Supplier<T> targetConstructor) { | |
| T target = targetConstructor.get(); | |
| beanCopier.copy(source, target, null); | |
| return target; | |
| } | |
| public static <S, T> List<T> copy(BeanCopier beanCopier, List<S> sources, Supplier<T> targetConstructor) { | |
| List<T> targetRecords = new ArrayList<>(); | |
| for (S source : sources) { | |
| T target = targetConstructor.get(); | |
| beanCopier.copy(source, target, null); | |
| targetRecords.add(target); | |
| } | |
| return targetRecords; | |
| } | |
| public static <S, T> Page<T> copyPage(BeanCopier beanCopier, Page<S> sourcePage, Supplier<T> targetConstructor) { | |
| Page<T> targetPage = new Page<>(sourcePage.getCurrent(), sourcePage.getSize(), sourcePage.getTotal()); | |
| targetPage.setRecords(BeanHelper.copy(beanCopier, sourcePage.getRecords(), targetConstructor)); | |
| return targetPage; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment