zoukankan      html  css  js  c++  java
  • springboot~ObjectMapper~dto到entity的自动赋值

    实体与Dto自动赋值

    在开发的过程中,实体之间相互赋值是很正常的事,但是我们一般的方法都通过set和get方法来进行的,如果要赋值的字段少那还行,但是需要赋值的字段超过10个,那就是个灾难,你会看到整屏代码中全是set和get方法。

    1. 两个实体属性字段几乎完全相同
    2. 两个字体有部分字段相同
    3. 源实体只有部分字段赋值,目标实体有完整的值

    第一种情况

    对于第1点来说,我们用到最多的就是entity和dto之间的转换了,这个我们可以使用Spring的工具类BeanUtils来解决,这里要注意的一点是,第一个参数是源,第二个参数是目标

    import org.springframework.beans.BeanUtils;
    BeanUtils.copyProperties(origin, target);
    

    第二种情况

    但是对于第2点来说,就没有那么简单了,再使用BeanUtils已经不能满足我们的需要了。
    我们可以使用jackson的ObjectMapper

    import com.fasterxml.jackson.databind.DeserializationFeature;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.fasterxml.jackson.databind.ObjectReader;
    import com.jd.fastjson.JSON;
    
    ObjectMapper objectMapper = new ObjectMapper();
    //配置该objectMapper在反序列化时,忽略目标对象没有的属性。凡是使用该objectMapper反序列化时,都会拥有该特性。
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    //读入需要更新的目标实体
    ObjectReader objectReader = objectMapper.readerForUpdating(target);
    //将源实体的值赋值到目标实体上
    objectReader.readValue(JSON.toJSONString(source));
    

    我们总结一下objectMapper的过滤参数:

     /*
     通过该方法对mapper对象进行设置,所有序列化的对象都将按改规则进行系列化
     Include.Include.ALWAYS 默认
     Include.NON_DEFAULT 属性为默认值不序列化
     Include.NON_EMPTY 属性为 空(“”)  或者为 NULL 都不序列化
     Include.NON_NULL 属性为NULL 不序列化
     */
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT);
        String outJson = objectMapper.writeValueAsString(productDetail);
    //上面代码里,outJson的值将会过滤掉只有默认值的属性
    

    第三种情况

    本情况主要对于从dto到entity转换过程中出现 ,比如一个put操作,前端可能只修改某几个属性,而在后端处理时也只希望处理这几个被赋值的属性,这时我们使用下面的方法:

      @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
      public HttpEntity update(@PathVariable int id, @RequestBody ProductDetail productDetail)
          throws IOException {
        ProductDetail existing = repository.findById(id).get();
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT);
        String outJson = objectMapper.writeValueAsString(productDetail);
        ObjectReader objectReader = objectMapper.readerForUpdating(existing);
        objectReader.readValue(outJson);
        repository.save(existing);
        return new ResponseEntity<>(existing, HttpStatus.ACCEPTED);
      }
    

    集合转集合

     String requestData="[]";
      List<ProductDetail> list = objectMapper.readValue(requestData,
            new TypeReference<List<ProductDetail>>() {
            });
        repository.saveAll(list);
    

    通过objectMapper的使用,确实让我们少写很多重复的代码。

  • 相关阅读:
    牛客 Wannafly 挑战赛26D 禁书目录 排列组合 概率期望
    UOJ#269. 【清华集训2016】如何优雅地求和
    斯特林数 学习笔记
    AtCoder Grand Contest 006 (AGC006) C
    Codeforces 1045D Interstellar battle 概率期望
    Codeforces 1045A Last chance 网络流,线段树,线段树优化建图
    Codeforces 1053C Putting Boxes Together 树状数组
    Codeforces 109D String Transformation 字符串 哈希 KMP
    AtCoder Grand Contest 1~10 做题小记
    AtCoder Grand Contest 002 (AGC002) F
  • 原文地址:https://www.cnblogs.com/lori/p/9400510.html
Copyright © 2011-2022 走看看