Hola, tenemos la solución a lo que necesitas, desplázate y la hallarás aquí.
Ejemplo: cómo realizar operaciones de crud en spring boot
packagecom.example.demo.com.example.demo.resource;importjava.util.List;importjava.util.Optional;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.web.bind.annotation.DeleteMapping;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.PathVariable;importorg.springframework.web.bind.annotation.PostMapping;importorg.springframework.web.bind.annotation.RequestBody;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestMethod;importorg.springframework.web.bind.annotation.RequestParam;importorg.springframework.web.bind.annotation.ResponseBody;importorg.springframework.web.bind.annotation.RestController;importcom.example.demo.com.example.demo.model.Student;importcom.example.demo.com.example.demo.repository.StudentRep;@RestController@RequestMapping(value ="/webapi")publicclassStudentResource@AutowiredprivateStudentRep studentRep;@RequestMapping(method =RequestMethod.GET, value ="/getstudents")publicList<Student>getStudents()return studentRep.findAll();@RequestMapping(method =RequestMethod.POST, value ="/addstudent")publicStringaddStudent(@RequestBodyStudent student)
studentRep.save(student);return"Added Student : "+student.getName();@RequestMapping(method =RequestMethod.GET,value ="/getstudents/id")publicOptional<Student>getStudent(@PathVariableString id)return studentRep.findById(id);@RequestMapping(method =RequestMethod.POST, value ="/deletestudent/id")publicStringdeleteStudent(@PathVariableString id)
studentRep.deleteById(id);return"Record Deleted :"+ id;@RequestMapping(method =RequestMethod.PUT, value ="/updatestudent")publicStringupdateStudent(@RequestBodyStudent student)
studentRep.findById(student.getId());if(studentRep.existsById(student.getId()))
studentRep.save(student);return"Record updated";elsereturn"No record";
Aquí puedes ver las reseñas y valoraciones de los usuarios
No se te olvide dar recomendación a esta reseña si te ayudó.
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)