Guest User

Untitled

a guest
Dec 10th, 2017
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. package com.nerdbot.student.controller;
  2.  
  3. import java.util.List;
  4.  
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Controller;
  7. import org.springframework.web.bind.annotation.RequestBody;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.bind.annotation.RequestMethod;
  10. import org.springframework.web.bind.annotation.RequestParam;
  11. import org.springframework.web.bind.annotation.ResponseBody;
  12.  
  13. import com.nerdbot.student.entity.Student;
  14. import com.nerdbot.student.entity.repo.StudentRepo;
  15.  
  16. @Controller
  17. @RequestMapping("/")
  18. public class StudentController {
  19.  
  20.  
  21. @Autowired
  22. StudentRepo repo;
  23. @RequestMapping(value="student",method=RequestMethod.GET)
  24. public @ResponseBody Student getValueByid(@RequestParam(value="id", required=true) String id) {
  25. Student s = repo.findOne(Long.valueOf(id).longValue());
  26. return s;
  27. }
  28.  
  29. @RequestMapping(value="allstudent",method=RequestMethod.GET)
  30. public @ResponseBody List<Student> getAllValue() {
  31. List<Student>s = repo.findAll();
  32. return s;
  33. }
  34.  
  35. @RequestMapping(value="delete",method=RequestMethod.GET)
  36. public @ResponseBody String getdeleteById(@RequestParam(value="id", required=true) String id) {
  37. repo.delete(Long.valueOf(id).longValue());
  38. return id + "- deleted ";
  39. }
  40.  
  41. @RequestMapping(value="add", method = RequestMethod.POST)
  42. public String add( @RequestBody Student input) {
  43.  
  44. try {
  45. repo.save(input);
  46.  
  47. } catch (Exception e) {
  48. System.err.println(e.getStackTrace());
  49. e.printStackTrace();
  50. }
  51.  
  52. return "Saved";
  53.  
  54. }
  55.  
  56.  
  57. @RequestMapping(value="deleteAll",method=RequestMethod.GET)
  58. public String getdeleteById() {
  59. repo.deleteAll();;
  60. return "All record deleted";
  61. }
  62.  
  63.  
  64.  
  65.  
  66. }
  67.  
  68. package com.nerdbot.student.entity;
  69.  
  70. import javax.persistence.CascadeType;
  71. import javax.persistence.Column;
  72. import javax.persistence.Entity;
  73. import javax.persistence.GeneratedValue;
  74. import javax.persistence.GenerationType;
  75. import javax.persistence.Id;
  76. import javax.persistence.OneToOne;
  77. import javax.persistence.Table;
  78.  
  79. @Entity
  80. @Table(name = "STUDENT")
  81. public class Student {
  82.  
  83. @Id
  84. @Column(name = "id")
  85. @GeneratedValue(strategy = GenerationType.AUTO)
  86. private long id;
  87.  
  88. @Column(name = "name")
  89. private String name;
  90.  
  91. @Column(name = "age")
  92. private int age;
  93.  
  94.  
  95.  
  96. public long getId() {
  97. return id;
  98. }
  99.  
  100. public void setId(long id) {
  101. this.id = id;
  102. }
  103.  
  104. public String getName() {
  105. return name;
  106. }
  107.  
  108. public void setName(String name) {
  109. this.name = name;
  110. }
  111.  
  112. public int getAge() {
  113. return age;
  114. }
  115.  
  116. public void setAge(int age) {
  117. this.age = age;
  118. }
  119.  
  120.  
  121. }
  122.  
  123. package com.nerdbot.student.entity.repo;
  124.  
  125. import org.springframework.data.jpa.repository.JpaRepository;
  126.  
  127. import com.nerdbot.student.entity.Student;
  128.  
  129. public interface StudentRepo extends JpaRepository<Student, Long> {
  130.  
  131. }
  132.  
  133. spring.h2.console.enabled=true
  134. spring.h2.console.path=/h2
  135. # Datasource
  136. spring.datasource.url=jdbc:h2:file:~/spring-boot-h2-db
  137. spring.datasource.username=sa
  138. spring.datasource.password=
  139. spring.datasource.driver-class-name=org.h2.Driver
  140. spring.jpa.hibernate.ddl-auto=update
Add Comment
Please, Sign In to add comment