Seth 5 yıl önce
ebeveyn
işleme
59cb25cb7f

+ 9
- 1
src/main/java/io/zipcoder/crudapp/Person.java Dosyayı Görüntüle

@@ -1,9 +1,17 @@
1 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 9
 public class Person {
4 10
     private String firstName;
5 11
     private String lastName;
6
-    private int id;
12
+    @Id
13
+    @GeneratedValue(strategy = GenerationType.AUTO)
14
+    private Integer id;
7 15
 
8 16
     public Person() {
9 17
 

+ 12
- 12
src/main/java/io/zipcoder/crudapp/PersonController.java Dosyayı Görüntüle

@@ -1,5 +1,6 @@
1 1
 package io.zipcoder.crudapp;
2 2
 
3
+import org.springframework.beans.factory.annotation.Autowired;
3 4
 import org.springframework.web.bind.annotation.*;
4 5
 
5 6
 import java.util.ArrayList;
@@ -11,37 +12,36 @@ import static org.springframework.web.bind.annotation.RequestMethod.*;
11 12
 public class PersonController {
12 13
     private List<Person> personList = new ArrayList<>();
13 14
 
15
+    @Autowired
16
+    PersonRepository personRepository;
17
+
14 18
     public PersonController(){
15 19
 
16 20
     }
17 21
 
18 22
     @RequestMapping(value = "/people", method = POST)
19 23
     public Person createPerson(@RequestBody Person p){
20
-        return new Person();
24
+
25
+       return personRepository.save(p);
21 26
     }
22 27
 
23 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 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 38
     @RequestMapping(value = "/people", method = PUT)
39 39
     public Person updatePerson(@RequestBody Person p){
40
-        return new Person();
40
+        return personRepository.save(p);
41 41
     }
42 42
 
43 43
     @RequestMapping(value = "/people/{id}", method = DELETE)
44 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 Dosyayı Görüntüle

@@ -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
+}