• java基于BeanUtils拷贝非空属性工具类


    问题:

    写SQL时难免会有一些需求要进行多张表关联才能查出来,但是进行多变查询的话,有又会造成查询效率严重下降,此时我们就需要拆分sql,拆分完可以显著提升查询效率,但此时我们每个sql查询的只是一部分数据,最后还需要将他们拼装到一起,传统的BeanUtil提供的copyProperties方法会将源数据的属性统统复制给目标对象这样不利于我们拼装sql,我们需要的只是非空的部分

    思路:

    copyProperties 方法的第三参数String... ignoreProperties(要忽略的属性),有了此种实现,我们就可以在数据拷贝之前拿到源数据的null值然后忽略就可以了详细请看getNullPropertyNames方法

    有了这种还不算完,因为在拼装时还有很多情况查询的结果是一个集合,那么这种情况一般来说需要根据某一个字段来进行匹配,于是有了copyListPropertiesIgnoreNull方法,原理就是循环两个集合,首先循环source时通过反射获取到要匹配的字段,将其作为map的key,而这个对象就作为value,随后循环target利用反射获取到要匹配的字段从map中获取到前面放入的对象(map可以高效的索引到value),于是就可以利用前面封装好的copyPropertiesIgnoreNull方法拷贝非空的属性

    1. /**
    2. * 属性复制(不复制空属性)
    3. *
    4. * @author zzt
    5. * @date 2022/08/20
    6. */
    7. public class BeanNotNullUtil {
    8. /**
    9. * 获取到对象中属性为null的属性名
    10. *
    11. * @param source
    12. * @return
    13. */
    14. private static String[] getNullPropertyNames(Object source) {
    15. final BeanWrapper src = new BeanWrapperImpl(source);
    16. PropertyDescriptor[] pds = src.getPropertyDescriptors();
    17. Set emptyNames = new HashSet<>();
    18. for (PropertyDescriptor pd : pds) {
    19. Object srcValue = src.getPropertyValue(pd.getName());
    20. if (ObjectUtils.isEmpty(srcValue)) {
    21. emptyNames.add(pd.getName());
    22. }
    23. }
    24. String[] result = new String[emptyNames.size()];
    25. return emptyNames.toArray(result);
    26. }
    27. /**
    28. * 拷贝非空对象属性值
    29. *
    30. * @param source
    31. * @param target
    32. */
    33. public static void copyPropertiesIgnoreNull(Object source, Object target) {
    34. BeanUtils.copyProperties(source, target, getNullPropertyNames(source));
    35. }
    36. /**
    37. * 集合拷贝非空数据
    38. *
    39. * @param source 数据源
    40. * @param target 目标
    41. * @param propertyName 要匹配的属性名,例如两个集合使用id进行匹配拷贝 propertyName: "id"
    42. */
    43. public static void copyListPropertiesIgnoreNull(List source, List target, String propertyName) {
    44. if (CollectionUtils.isEmpty(source)) {
    45. throw new NullPointerException("copyListPropertiesIgnoreNull source源数据为空!");
    46. }
    47. Map map = new HashMap<>(source.size());
    48. source.forEach(s -> {
    49. final BeanWrapper sourceBean = new BeanWrapperImpl(s);
    50. Object value = sourceBean.getPropertyValue(propertyName);
    51. if (value == null) {
    52. throw new NullPointerException("copyListPropertiesIgnoreNull获取参数异常");
    53. }
    54. map.put(value, s);
    55. });
    56. target.forEach(s -> {
    57. final BeanWrapper targetBean = new BeanWrapperImpl(s);
    58. Object value = targetBean.getPropertyValue(propertyName);
    59. if (value == null) {
    60. throw new NullPointerException("copyListPropertiesIgnoreNull获取参数异常");
    61. }
    62. Object o = map.get(value);
    63. copyPropertiesIgnoreNull(o, s);
    64. });
    65. }
    66. }

    测试:

    1. public static void main(String[] args) {
    2. List vos1 = new ArrayList<>();
    3. vos1.add(new UserVo().setName("张三")
    4. .setId("1"));
    5. vos1.add(new UserVo().setId("2")
    6. .setName("李四"));
    7. List vos2 = new ArrayList<>();
    8. vos2.add(new UserVo().setId("1")
    9. .setPassword("123456"));
    10. vos2.add(new UserVo().setId("2")
    11. .setPassword("654321"));
    12. copyListPropertiesIgnoreNull(vos2, vos1,"id");
    13. System.out.println(vos1);
    14. //[UserVo(id=1, name=张三, password=123456), UserVo(id=2, name=李四, password=654321)]
    15. }

  • 相关阅读:
    php mysql摄影网_图片分享网站
    linux学习实操计划0101-linux卸载软件
    星岛专栏|从Web3发展看金融与科技的融合之道
    华为机考入门python3--(14)牛客14-字符串排序
    【SpringBoot项目】SpringBoot项目-瑞吉外卖【day01】
    Go 基本数据类型和 string 类型介绍
    使用CSS变量实现主题定制真的很简单
    使用 Spring Data Redis 访问 Redis 的 Spring Boot 应用
    Redis缓存的高并发问题
    摄影工作室标配,智云五倍F100棒灯快速塑造专业风格
  • 原文地址:https://blog.csdn.net/winerpro/article/details/126449815