java jpa save方法輔助工具類

java jpa save方法更新數據工具類

  • 因爲jpa沒有單獨更新某些字段的方法,save方法可以更新某條數據,但是有一個缺陷,就是它會將爲空的字段也更新上去.
    如果有一個工具類可以將一個實體類非空的數據映射到jpa的findOne數據上,再進行save,就會方便很多.如下:

import com.alibaba.druid.util.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import java.beans.PropertyDescriptor;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

public class UpdateUtil {
    /**
     * 所有爲空值的屬性都不copy
     *
     * @param source
     * @param target
     */
    public static void copyNullProperties(Object source, Object target) {
        BeanUtils.copyProperties(source, target, getNullField(source));
    }

    /**
     * 獲取屬性中爲空的字段
     *
     * @param target
     * @return
     */
    private static String[] getNullField(Object target) {
        BeanWrapper beanWrapper = new BeanWrapperImpl(target);
        PropertyDescriptor[] propertyDescriptors = beanWrapper.getPropertyDescriptors();
        Set<String> notNullFieldSet = new HashSet<>();
        if (propertyDescriptors.length > 0) {
            for (PropertyDescriptor p : propertyDescriptors) {
                String name = p.getName();
                Object value = beanWrapper.getPropertyValue(name);
                if (Objects.isNull(value)) {
                    notNullFieldSet.add(name);
                }
            }
        }
        String[] notNullField = new String[notNullFieldSet.size()];
        return notNullFieldSet.toArray(notNullField);
    }

    public static void main(String[] args) {
        IdNameVo newvo=  new IdNameVo();
        newvo.setId("1");
        newvo.setName("更新後的實體類");
        newvo.setLevel(1);
        newvo.setParentId("0");
        IdNameVo old=  new IdNameVo();
        old.setId("1");
        old.setName("更新前的實體類");
        old.setLevel(2);
        old.setParentId("1");
        old.setIdx(4);
        old.setCode("123");
        System.out.println(old);
        UpdateUtil.copyNullProperties(newvo,old);
        System.out.println(old);
		//然後直接jpa的save(vo2)就可以了
    }
}

輸出的結果爲:

IdNameVo(id=1, name=更新前的實體類, parentId=1, level=2, idx=4, children=[],code=123, coupon=null, couponId=null, promotionId=null,
promotionType=null, searchType=null)
IdNameVo(id=1, name=更新後的實體類, parentId=0, level=1, idx=4, children=[], code=123, coupon=null, couponId=null, promotionId=null,
promotionType=null, searchType=null)

發佈了10 篇原創文章 · 獲贊 4 · 訪問量 1831
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章