瀏覽代碼

{struggle central}

Jacqueline Joson 5 年之前
父節點
當前提交
74f48f8aa0

+ 59
- 0
src/main/java/io/zipcoder/crudapp/Person.java 查看文件

@@ -0,0 +1,59 @@
1
+package io.zipcoder.crudapp;
2
+
3
+import javax.persistence.Entity;
4
+import javax.persistence.GeneratedValue;
5
+import javax.persistence.GenerationType;
6
+import javax.persistence.Id;
7
+
8
+@Entity
9
+public class Person {
10
+    @Id
11
+    @GeneratedValue(strategy = GenerationType.AUTO)
12
+    private long id;
13
+    private String firstName;
14
+    private String lastName;
15
+
16
+
17
+    public Person(String firstName, String lastName, long id) {
18
+        this.firstName = firstName;
19
+        this.lastName = lastName;
20
+        this.id = id;
21
+    }
22
+
23
+    public Person() {
24
+    }
25
+
26
+
27
+    public void setFirstName(String firstName) {
28
+        this.firstName = firstName;
29
+    }
30
+
31
+    public void setLastName(String lastName) {
32
+        this.lastName = lastName;
33
+    }
34
+
35
+    public void setId(long id) {
36
+        this.id = id;
37
+    }
38
+
39
+    public String getFirstName() {
40
+        return firstName;
41
+    }
42
+
43
+    public String getLastName() {
44
+        return lastName;
45
+    }
46
+
47
+    public long getId() {
48
+        return id;
49
+    }
50
+
51
+    @Override
52
+    public String toString() {
53
+        return "Person{" +
54
+                "firstName='" + firstName + '\'' +
55
+                ", lastName='" + lastName + '\'' +
56
+                ", id=" + id +
57
+                '}';
58
+    }
59
+}

+ 58
- 0
src/main/java/io/zipcoder/crudapp/PersonController.java 查看文件

@@ -0,0 +1,58 @@
1
+package io.zipcoder.crudapp;
2
+
3
+import org.springframework.http.HttpStatus;
4
+import org.springframework.http.ResponseEntity;
5
+import org.springframework.web.bind.annotation.*;
6
+
7
+@RestController
8
+public class PersonController {
9
+
10
+    PersonRepository personRepository;
11
+
12
+    public PersonController() {
13
+    }
14
+
15
+//
16
+//    public ResponseEntity<Person> createPerson(@RequestParam("person") Person person) {
17
+//        return new ResponseEntity<>(this.personRepository.save(person), HttpStatus.CREATED);
18
+//    }
19
+
20
+    @PostMapping("/person")
21
+    public ResponseEntity<Person> createPerson(@RequestBody Person person) {
22
+        Person person1 = new Person(person.getFirstName(), person.getLastName(), person.getId());
23
+        return new ResponseEntity<>(this.personRepository.save(person1), HttpStatus.CREATED);
24
+    }
25
+
26
+    @GetMapping("/people/{id}")
27
+    public ResponseEntity<Person> getPerson(@PathVariable Long id) {
28
+        Person person = this.personRepository.findOne(id);
29
+        return new ResponseEntity<>(person, HttpStatus.OK);
30
+    }
31
+
32
+    @GetMapping("/people")
33
+    public ResponseEntity<Iterable<Person>> getPersonList() {
34
+        return new ResponseEntity<>(this.personRepository.findAll(), HttpStatus.OK);
35
+    }
36
+
37
+    @PutMapping("/people/{id}")
38
+    public ResponseEntity<Person> updatePerson(@PathVariable Long id, @RequestBody Person person) {
39
+        Person person1 = this.personRepository.findOne(id);
40
+
41
+        person1.setFirstName(person.getFirstName());
42
+        person1.setLastName(person.getLastName());
43
+        return new ResponseEntity<>(person1, HttpStatus.OK);
44
+    }
45
+
46
+    @DeleteMapping("/people/{id}")
47
+    public ResponseEntity<?> deletePerson(@PathVariable Long id) {
48
+        this.personRepository.delete(personRepository.findOne(id));
49
+        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
50
+    }
51
+}
52
+
53
+
54
+
55
+
56
+
57
+
58
+

+ 6
- 0
src/main/java/io/zipcoder/crudapp/PersonRepository.java 查看文件

@@ -0,0 +1,6 @@
1
+package io.zipcoder.crudapp;
2
+
3
+import org.springframework.data.repository.CrudRepository;
4
+
5
+public interface PersonRepository extends CrudRepository <Person, Long> {
6
+}