Saltar al contenido

ejemplo de código de aplicación spring boot crud

Posterior a de una larga selección de información pudimos resolver esta duda que tienen ciertos de nuestros usuarios. Te ofrecemos la solución y esperamos resultarte de mucha apoyo.

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";

Si para ti ha sido de utilidad nuestro artículo, sería de mucha ayuda si lo compartes con otros desarrolladores de esta forma contrubuyes a dar difusión a este contenido.

¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)



Utiliza Nuestro Buscador

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *