Saltar al contenido

operación de crud simple en el ejemplo de código de arranque de primavera (servicio y controlador y jpa)

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

Eres capaz de añadir valor a nuestra información tributando tu veteranía en las crónicas.

¡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 *