Connor Dunnigan 5 年之前
父節點
當前提交
8abbfc112b

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

@@ -0,0 +1,43 @@
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
+
11
+    @Id
12
+    @GeneratedValue(strategy = GenerationType.AUTO)
13
+    private Integer id;
14
+    private String firstName;
15
+    private String lastName;
16
+
17
+    public Person() {
18
+    }
19
+
20
+    public Integer getId() {
21
+        return id;
22
+    }
23
+
24
+    public void setId(Integer id) {
25
+        this.id = id;
26
+    }
27
+
28
+    public String getFirstName() {
29
+        return firstName;
30
+    }
31
+
32
+    public void setFirstName(String firstName) {
33
+        this.firstName = firstName;
34
+    }
35
+
36
+    public String getLastName() {
37
+        return lastName;
38
+    }
39
+
40
+    public void setLastName(String lastName) {
41
+        this.lastName = lastName;
42
+    }
43
+}

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

@@ -0,0 +1,40 @@
1
+package io.zipcoder.crudapp;
2
+
3
+import org.springframework.beans.factory.annotation.Autowired;
4
+import org.springframework.web.bind.annotation.*;
5
+
6
+@RestController
7
+public class PersonController {
8
+    @Autowired
9
+    private PersonRepository repository;
10
+
11
+    @PostMapping("/people")
12
+    Person createPerson(@RequestBody Person p) {
13
+        repository.save(p);
14
+        return p;
15
+    }
16
+
17
+    @GetMapping("/people")
18
+    Iterable<Person> getPersonList() {
19
+        return repository.findAll();
20
+    }
21
+
22
+    @GetMapping("/people/{id}")
23
+    Person getPerson(@PathVariable int id) {
24
+        return repository.findOne(id);
25
+    }
26
+
27
+    @PutMapping("/people/{id}")
28
+    Person updatePerson(@PathVariable int id, @RequestBody Person p) {
29
+        Person personToBeUpdated = getPerson(id);
30
+        personToBeUpdated.setFirstName(p.getFirstName());
31
+        personToBeUpdated.setLastName(p.getLastName());
32
+        repository.save(personToBeUpdated);
33
+        return personToBeUpdated;
34
+    }
35
+
36
+    @DeleteMapping("/people/{id}")
37
+    void DeletePerson(@PathVariable int id) {
38
+        repository.delete(id);
39
+    }
40
+}

+ 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, Integer> {
6
+}