Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.nerdbot.student.controller;
- import java.util.List;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestBody;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.bind.annotation.RequestParam;
- import org.springframework.web.bind.annotation.ResponseBody;
- import com.nerdbot.student.entity.Student;
- import com.nerdbot.student.entity.repo.StudentRepo;
- @Controller
- @RequestMapping("/")
- public class StudentController {
- @Autowired
- StudentRepo repo;
- @RequestMapping(value="student",method=RequestMethod.GET)
- public @ResponseBody Student getValueByid(@RequestParam(value="id", required=true) String id) {
- Student s = repo.findOne(Long.valueOf(id).longValue());
- return s;
- }
- @RequestMapping(value="allstudent",method=RequestMethod.GET)
- public @ResponseBody List<Student> getAllValue() {
- List<Student>s = repo.findAll();
- return s;
- }
- @RequestMapping(value="delete",method=RequestMethod.GET)
- public @ResponseBody String getdeleteById(@RequestParam(value="id", required=true) String id) {
- repo.delete(Long.valueOf(id).longValue());
- return id + "- deleted ";
- }
- @RequestMapping(value="add", method = RequestMethod.POST)
- public String add( @RequestBody Student input) {
- try {
- repo.save(input);
- } catch (Exception e) {
- System.err.println(e.getStackTrace());
- e.printStackTrace();
- }
- return "Saved";
- }
- @RequestMapping(value="deleteAll",method=RequestMethod.GET)
- public String getdeleteById() {
- repo.deleteAll();;
- return "All record deleted";
- }
- }
- package com.nerdbot.student.entity;
- import javax.persistence.CascadeType;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.GeneratedValue;
- import javax.persistence.GenerationType;
- import javax.persistence.Id;
- import javax.persistence.OneToOne;
- import javax.persistence.Table;
- @Entity
- @Table(name = "STUDENT")
- public class Student {
- @Id
- @Column(name = "id")
- @GeneratedValue(strategy = GenerationType.AUTO)
- private long id;
- @Column(name = "name")
- private String name;
- @Column(name = "age")
- private int age;
- 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 int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- }
- package com.nerdbot.student.entity.repo;
- import org.springframework.data.jpa.repository.JpaRepository;
- import com.nerdbot.student.entity.Student;
- public interface StudentRepo extends JpaRepository<Student, Long> {
- }
- spring.h2.console.enabled=true
- spring.h2.console.path=/h2
- # Datasource
- spring.datasource.url=jdbc:h2:file:~/spring-boot-h2-db
- spring.datasource.username=sa
- spring.datasource.password=
- spring.datasource.driver-class-name=org.h2.Driver
- spring.jpa.hibernate.ddl-auto=update
Add Comment
Please, Sign In to add comment