Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring Boot:如何高效地更新对象?_Java_Spring_Spring Boot_Spring Mvc_Spring Data Jpa - Fatal编程技术网

Java Spring Boot:如何高效地更新对象?

Java Spring Boot:如何高效地更新对象?,java,spring,spring-boot,spring-mvc,spring-data-jpa,Java,Spring,Spring Boot,Spring Mvc,Spring Data Jpa,大家好,我是春天世界的新手。实际上,我想知道我们如何使用转换器来更新对象,而不是使用set和get逐个更新每个元素。现在,在我的控制器中,我: @PostMapping("/edit/{userId}") public Customer updateCustomer(@RequestBody Customer newCustomer, @PathVariable final String userId) { return customerService.u

大家好,我是春天世界的新手。实际上,我想知道我们如何使用转换器来更新对象,而不是使用set和get逐个更新每个元素。现在,在我的控制器中,我:

    @PostMapping("/edit/{userId}")
    public Customer updateCustomer(@RequestBody Customer newCustomer, @PathVariable final String userId)
    {
        return customerService.update(userId, newCustomer);
    }
这就是我更新客户对象的方式:

    @Override
    public Customer update(String id, Customer newCustomer) {
        Customer customer = customerRepository.findById(id).get();

        customer.setFirstName(newCustomer.getFirstName());
        customer.setLastName(newCustomer.getLastName());
        customer.setEmail(newCustomer.getEmail());

        return customerRepository.save(customer);
    }

我想使用一个转换器,而不是使用每个时间集和get

使用
ModelMapper
将一个模型映射到另一个模型。 首先定义一个可以将源数据映射到目标模型的函数。将此用作库,以便在需要时随时使用

  public static <T> void merge(T source, T target) {
    ModelMapper modelMapper = new ModelMapper();
    modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
    modelMapper.map(source, target);
  }
在pom.xml中为模型映射器添加依赖项

<dependency>
    <groupId>org.modelmapper</groupId>
    <artifactId>modelmapper</artifactId>
    <version>2.3.4</version>
</dependency>

org.modelmapper
模型映射器
2.3.4

使用
ModelMapper
将一个模型映射到另一个模型。 首先定义一个可以将源数据映射到目标模型的函数。将此用作库,以便在需要时随时使用

  public static <T> void merge(T source, T target) {
    ModelMapper modelMapper = new ModelMapper();
    modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
    modelMapper.map(source, target);
  }
在pom.xml中为模型映射器添加依赖项

<dependency>
    <groupId>org.modelmapper</groupId>
    <artifactId>modelmapper</artifactId>
    <version>2.3.4</version>
</dependency>

org.modelmapper
模型映射器
2.3.4

在更新实体id时,将其作为路径变量传递的方法并不正确。考虑一下:您有一个
@RequestBody
,为什么不在这个body中也包含id呢?为什么要为其指定路径变量

现在,如果您拥有完整的
客户
,其id来自主体,则不必对存储库进行任何调用,因为hibernate已根据其id和简单的

public Customer update(Customer newCustomer) {
      return customerRepository.save(newCustomer);
}
应该有用

Q:什么是持久状态

A:持久化实体已与数据库表行关联,并由当前运行的持久化上下文管理。(
customerRepository.findById()
只是询问数据库是否存在具有指定id的实体,并将其添加到持久状态。如果您有一个
@id
注释字段并已填充,则Hibernate管理所有此过程,换句话说:

   Customer customer = new Customer();
   customer.setId(1); 
与以下内容几乎相同:

   Customer customer = customerRepository.findById(1).get();
)

提示:无论如何,您不应该(如果您不知道)在控制器层中有一个模型。为什么?假设您的
客户
模型可以具有多个权限。一种可能的结构如下所示:

@Entity
public class Customer{
   //private fields here;

   @OneToMany(mappedBy="customer",--other configs here--)
   private List<Permission> permissions;
}
public class CustomerDTO
{
   private String firstName;
   private String lastName;
   private List<String> permissions;
}
您可以看到,
Customer
Permission
实体之间存在交叉引用,这将最终导致堆栈溢出异常(如果你不理解这一点,你可以考虑一个递归函数,它没有停止的条件,它被反复调用=>stack overflow。这里也发生了同样的事情)

你能做什么?创建一个所谓的
DTO
类,希望客户端接收该类而不是模型。你如何创建这个DTO?想想用户需要知道什么

1) 来自
权限
的“creationDate”是否是用户必需的字段?不是真的

2)
权限
中的“id”是否为用户必需的字段?在某些情况下是的,在另一些情况下不是

可能的
CustomerDTO
如下所示:

@Entity
public class Customer{
   //private fields here;

   @OneToMany(mappedBy="customer",--other configs here--)
   private List<Permission> permissions;
}
public class CustomerDTO
{
   private String firstName;
   private String lastName;
   private List<String> permissions;
}

在更新实体id时,将其作为路径变量传递的方法并不正确。考虑一下:您有一个
@RequestBody
,为什么不在这个body中也包含id呢?为什么要为其指定路径变量

现在,如果您拥有完整的
客户
,其id来自主体,则不必对存储库进行任何调用,因为hibernate已根据其id和简单的

public Customer update(Customer newCustomer) {
      return customerRepository.save(newCustomer);
}
应该有用

Q:什么是持久状态

A:持久化实体已与数据库表行关联,并由当前运行的持久化上下文管理。(
customerRepository.findById()
只是询问数据库是否存在具有指定id的实体,并将其添加到持久状态。如果您有一个
@id
注释字段并已填充,则Hibernate管理所有此过程,换句话说:

   Customer customer = new Customer();
   customer.setId(1); 
与以下内容几乎相同:

   Customer customer = customerRepository.findById(1).get();
)

提示:无论如何,您不应该(如果您不知道)在控制器层中有一个模型。为什么?假设您的
客户
模型可以具有多个权限。一种可能的结构如下所示:

@Entity
public class Customer{
   //private fields here;

   @OneToMany(mappedBy="customer",--other configs here--)
   private List<Permission> permissions;
}
public class CustomerDTO
{
   private String firstName;
   private String lastName;
   private List<String> permissions;
}
您可以看到,
Customer
Permission
实体之间存在交叉引用,这将最终导致堆栈溢出异常(如果你不理解这一点,你可以考虑一个递归函数,它没有停止的条件,它被反复调用=>stack overflow。这里也发生了同样的事情)

你能做什么?创建一个所谓的
DTO
类,希望客户端接收该类而不是模型。你如何创建这个DTO?想想用户需要知道什么

1) 来自
权限
的“creationDate”是否是用户必需的字段?不是真的

2)
权限
中的“id”是否为用户必需的字段?在某些情况下是的,在另一些情况下不是

可能的
CustomerDTO
如下所示:

@Entity
public class Customer{
   //private fields here;

   @OneToMany(mappedBy="customer",--other configs here--)
   private List<Permission> permissions;
}
public class CustomerDTO
{
   private String firstName;
   private String lastName;
   private List<String> permissions;
}

值得一提的是,将数据库对象用作端点请求主体并不是一种好的做法值得一提的是,将数据库对象用作端点请求主体并不是一种好的做法