springboot jpa 动态更新数据库里的对象属性

本文介绍了一种使用Spring Boot JPA实现实体类属性动态更新的方法,通过BeanUtils和自定义CommonUtils类来忽略null值属性,确保数据库更新的准确性。同时,针对原有方法在属性设为null时的不足,提出改进方案,避免重要字段被意外清除。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

   service 层代码:

    import org.springframework.beans.BeanUtils;  
    import javax.transaction.Transactional;


    @Transactional  
    public User update( User user ){  
        User userInDB = findById( user.getId() ); 
        BeanUtils.copyProperties( user, userInDB, CommonUtils.getNullPropertyNames(user) );  


        return userDAO.save( userInDB );  // 注意 save 的参数是 userInDB,而不是 user
    } 

 

// BeanUtils.copyProperties( sourceBean, targetBean, ignoreProperties ) 方法的作用是:Copy the property values of the given source bean into the given target bean, ignoring the given "ignoreProperties".

------------------------

    import org.springframework.beans.BeanWrapper;
    import org.springframework.beans.BeanWrapperImpl;

    public class CommonUtils {

        // get Null Property Names, return String[]  挑出 bean 里空的属性,返回属性名的数组
        public static String[] getNullPropertyNames (Object source) {  
            final BeanWrapper src = new BeanWrapperImpl(source);  
            java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();  
      
            Set<String> emptyNames = new HashSet<String>();  
            for(java.beans.PropertyDescriptor pd : pds) {  
                Object srcValue = src.getPropertyValue(pd.getName());  
                if (srcValue == null) {
                    emptyNames.add(pd.getName());
                }
            }  
            String[] result = new String[emptyNames.size()];  
            return emptyNames.toArray(result);  
        }  
    }

用以上方法可以实现对象属性动态更新进数据库

 

2019-2-1  今天发现上面这种网上找来的实现 jpa 动态更新实体类属性的方法存在问题:如果页面里把一个属性修改为 null,提交到后台进行修改,用这种方法会漏掉改为 null 的属性。
所以需要修改写法,把数据库里没有传到前台的属性(本例子中是 createDate 和 status)查出来,设置到前台传回到后台的对象里,避免更新操作后,这类字段变为 null。

修改后写法为:
   service 层代码:

    @Transactional  
    public User update(User user){  
        user.setUpdateDate(new Date());    
        
        // 把数据库里没有传到前台的属性获取出来,设置到前台传回来的 user 对象里
        User userInDB = findById(user.getId());
        user.setCreateDate(userInDB.getCreateDate());
        user.setStatus(userInDB.getStatus());

        return userDAO.save(user);  // user 对象只要包含 id,就能实现更新效果
    }

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值