Skip to content

Instantly share code, notes, and snippets.

@codethereforam
Created August 26, 2024 06:26
Show Gist options
  • Select an option

  • Save codethereforam/c8bc31c244ef3c8c5491ca3869346710 to your computer and use it in GitHub Desktop.

Select an option

Save codethereforam/c8bc31c244ef3c8c5491ca3869346710 to your computer and use it in GitHub Desktop.
use spring BeanCopier copy beans
/**
* 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