gpt4 book ai didi

java - 如何使用 Spring-data-JPA 使用 crudRepository 在 MySQL 数据库中附加多条记录?

转载 作者:行者123 更新时间:2023-11-29 06:00:44 25 4
gpt4 key购买 nike

我正在使用 POST 方法一条一条地插入新记录,但我期待一次推送多条记录。我的单插入代码如下:

@RequestMapping(path="/users")
public class MainController {
@Autowired
private UserRepository userRepository;

@PostMapping(path="/add", consumes="application/json", produces = "application/json")
@Transactional(propagation=Propagation.REQUIRED)
public @ResponseBody String addNewUser(@RequestBody UserFormDto userForm) {
User n = new User();
n.setName(userForm.getName());
n.setEmail(userForm.getEmail());
userRepository.save(n);
return "\n\t\t Saved";
}
}

实体类如下:任何帮助将不胜感激

package hello;
import javax.persistence.*;
import javax.persistence.Table;
import org.hibernate.validator.constraints.Email;

@SuppressWarnings("unused")
@Entity
public class User {

private String name;
private String email;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;

public User() {
}

public User(String email,String name) {
super();
this.name = name;
this.email = email;
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}
}

最佳答案

关于java - 如何使用 Spring-data-JPA 使用 crudRepository 在 MySQL 数据库中附加多条记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45166539/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com