![](/img/trans.png)
[英]How to update huge data in database using Spring Boot Batch Job
[英]How to update database with spring boot
我正在嘗試在我的 spring 啟動應用程序中更新我的數據庫(postgresql),其中包含一些我將從 angular 8 表單獲得的表單信息。 我怎樣才能完成下面的代碼呢?
PS:我無法訪問前端開發人員,因此無法訪問 angular 代碼。
我的實體:
@Entity
@Data
@Table(name = "person")
@AllArgsConstructor
@NoArgsConstructor
public class PersonEntity implements java.io.Serializable {
@Id
@Column(name = "id_person")
private int idPerson;
@Column(name = "name")
private String name;
@Column(name = "alive")
private Boolean alive;
}
我的映射 class:
@Data
public class PersonForm {
@NotBlank
private String idPerson;
@NotBlank
private String name;
private boolean alive;
}
我的倉庫:
@Repository
public interface IPersonRepository extends JpaRepository<PersonEntity, String> {
}
我的 Controller:
@RequestMapping(value = "person")
public class PersonController {
private final PersonService personService;
public PersonController(PersonService personService) {this.personService = personService;}
@PostMapping(value = "savePerson")
@ResponseBody
public ResponseEntity<PersonEntity> savePerson(@RequestBody final PersonForm form) {
return ?
}
}
我的服務:
@Transactional
@Service
public class PersonService {
@Autowired
private IPersonRepository personRepository;
public IPersonRepository(IPersonRepository personRepository) {
this.personRepository = personRepository;
}
也許這足以工作。
有其他方法可以做到這一點,這只是補充您的思路的示例。
如果您遇到編譯錯誤或其他錯誤,請告訴我:)
首先,您需要創建 object PersonEntity
並將其發送到您的服務。
@RequestMapping(value = "person")
public class PersonController {
private final PersonService personService;
public PersonController(PersonService personService) {this.personService = personService;}
@PostMapping(value = "savePerson")
@ResponseBody
public ResponseEntity<PersonEntity> savePerson(@RequestBody final PersonForm form) {
PersonEntity entity = new PersonEntity(form);
entity = personService.save(entity);
return ResponseEntity.ok(entity);
}
}
在您的服務中,您將從存儲庫實現中調用save
方法。
@Transactional
@Service
public class PersonService {
@Autowired
private IPersonRepository personRepository;
public IPersonRepository(IPersonRepository personRepository) {
this.personRepository = personRepository;
}
public PersonEntity save(PersonEntity entity) {
return personRepository.save(entity);
}
}
之后,您將在方法PersonController.savePerson
中創建ResponseEntity
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.