Seth 5 年前
父节点
当前提交
59cb25cb7f

+ 9
- 1
src/main/java/io/zipcoder/crudapp/Person.java 查看文件

1
 package io.zipcoder.crudapp;
1
 package io.zipcoder.crudapp;
2
 
2
 
3
+import javax.persistence.Entity;
4
+import javax.persistence.GeneratedValue;
5
+import javax.persistence.GenerationType;
6
+import javax.persistence.Id;
7
+
8
+@Entity
3
 public class Person {
9
 public class Person {
4
     private String firstName;
10
     private String firstName;
5
     private String lastName;
11
     private String lastName;
6
-    private int id;
12
+    @Id
13
+    @GeneratedValue(strategy = GenerationType.AUTO)
14
+    private Integer id;
7
 
15
 
8
     public Person() {
16
     public Person() {
9
 
17
 

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

1
 package io.zipcoder.crudapp;
1
 package io.zipcoder.crudapp;
2
 
2
 
3
+import org.springframework.beans.factory.annotation.Autowired;
3
 import org.springframework.web.bind.annotation.*;
4
 import org.springframework.web.bind.annotation.*;
4
 
5
 
5
 import java.util.ArrayList;
6
 import java.util.ArrayList;
11
 public class PersonController {
12
 public class PersonController {
12
     private List<Person> personList = new ArrayList<>();
13
     private List<Person> personList = new ArrayList<>();
13
 
14
 
15
+    @Autowired
16
+    PersonRepository personRepository;
17
+
14
     public PersonController(){
18
     public PersonController(){
15
 
19
 
16
     }
20
     }
17
 
21
 
18
     @RequestMapping(value = "/people", method = POST)
22
     @RequestMapping(value = "/people", method = POST)
19
     public Person createPerson(@RequestBody Person p){
23
     public Person createPerson(@RequestBody Person p){
20
-        return new Person();
24
+
25
+       return personRepository.save(p);
21
     }
26
     }
22
 
27
 
23
     @RequestMapping(value = "/people/{id}", method = GET)
28
     @RequestMapping(value = "/people/{id}", method = GET)
24
-    public Person getPerson(@PathVariable int id){
25
-        for(Person person: personList){
26
-            if(person.getId() == id){
27
-                return person;
28
-            }
29
-        }
30
-        return null;
29
+    public void getPerson(@PathVariable int id){
30
+        personRepository.findById(id);
31
     }
31
     }
32
 
32
 
33
     @RequestMapping(value = "/people", method = GET)
33
     @RequestMapping(value = "/people", method = GET)
34
-    public List<Person> getPersonList(){
35
-        return this.personList;
34
+    public void getPersonList(){
35
+        personRepository.findAll();
36
     }
36
     }
37
 
37
 
38
     @RequestMapping(value = "/people", method = PUT)
38
     @RequestMapping(value = "/people", method = PUT)
39
     public Person updatePerson(@RequestBody Person p){
39
     public Person updatePerson(@RequestBody Person p){
40
-        return new Person();
40
+        return personRepository.save(p);
41
     }
41
     }
42
 
42
 
43
     @RequestMapping(value = "/people/{id}", method = DELETE)
43
     @RequestMapping(value = "/people/{id}", method = DELETE)
44
     public void deletePerson(@PathVariable int id) {
44
     public void deletePerson(@PathVariable int id) {
45
-        personList.remove(id);
45
+        personRepository.deleteById(id);
46
     }
46
     }
47
 }
47
 }

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

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