Procházet zdrojové kódy

Added handy mysql repository for Person(s).

Kr Younger před 6 roky
rodič
revize
79064ccd57

+ 5
- 0
pom.xml Zobrazit soubor

@@ -30,6 +30,10 @@
30 30
 			<artifactId>spring-boot-starter-data-jpa</artifactId>
31 31
 		</dependency>
32 32
 		<dependency>
33
+			<groupId>mysql</groupId>
34
+			<artifactId>mysql-connector-java</artifactId>
35
+		</dependency>
36
+		<dependency>
33 37
 			<groupId>org.springframework.boot</groupId>
34 38
 			<artifactId>spring-boot-starter-data-rest</artifactId>
35 39
 		</dependency>
@@ -69,3 +73,4 @@
69 73
 
70 74
 
71 75
 </project>
76
+

+ 41
- 0
src/main/java/com/example/throwupthursday/PersonController.java Zobrazit soubor

@@ -0,0 +1,41 @@
1
+package com.example.throwupthursday;
2
+
3
+
4
+import org.springframework.beans.factory.annotation.Autowired;
5
+import org.springframework.stereotype.Controller;
6
+import org.springframework.web.bind.annotation.RequestMapping;
7
+import org.springframework.web.bind.annotation.GetMapping;
8
+import org.springframework.web.bind.annotation.RequestParam;
9
+import org.springframework.web.bind.annotation.ResponseBody;
10
+
11
+import com.example.throwupthursday.daos.Person;
12
+import com.example.throwupthursday.PersonRepository;
13
+
14
+@Controller    // This means that this class is a Controller
15
+@RequestMapping(path="/people") // This means URL's start with /demo (after Application path)
16
+public class PersonController {
17
+    @Autowired // This means to get the bean called userRepository
18
+    // Which is auto-generated by Spring, we will use it to handle the data
19
+    private PersonRepository personRepository;
20
+
21
+    @GetMapping(path="/add") // Map ONLY GET Requests
22
+    public @ResponseBody String addNewUser (@RequestParam String name
23
+            , @RequestParam Integer age) {
24
+        // @ResponseBody means the returned String is the response, not a view name
25
+        // @RequestParam means it is a parameter from the GET or POST request
26
+
27
+        Person n = new Person();
28
+        n.setName(name);
29
+        n.setAge(age);
30
+        personRepository.save(n);
31
+        return "Saved";
32
+    }
33
+
34
+    @GetMapping(path="/all")
35
+    public @ResponseBody Iterable<Person> getAllUsers() {
36
+        // This returns a JSON or XML with the users
37
+        return personRepository.findAll();
38
+    }
39
+}
40
+
41
+// $ curl 'localhost:8080/people/add?name=George&age=28'

+ 11
- 0
src/main/java/com/example/throwupthursday/PersonRepository.java Zobrazit soubor

@@ -0,0 +1,11 @@
1
+package com.example.throwupthursday;
2
+import org.springframework.data.repository.CrudRepository;
3
+
4
+import com.example.throwupthursday.daos.Person;
5
+
6
+// This will be AUTO IMPLEMENTED by Spring into a Bean called userRepository
7
+// CRUD refers Create, Read, Update, Delete
8
+
9
+public interface PersonRepository extends CrudRepository<Person, Long> {
10
+
11
+}

+ 2
- 0
src/main/java/com/example/throwupthursday/ThrowupThursdayApplication.java Zobrazit soubor

@@ -3,6 +3,8 @@ package com.example.throwupthursday;
3 3
 import org.springframework.boot.SpringApplication;
4 4
 import org.springframework.boot.autoconfigure.SpringBootApplication;
5 5
 
6
+//
7
+
6 8
 @SpringBootApplication
7 9
 public class ThrowupThursdayApplication {
8 10
 

+ 1
- 0
src/main/java/com/example/throwupthursday/config/BookLoader.java Zobrazit soubor

@@ -7,6 +7,7 @@ import org.springframework.boot.ApplicationArguments;
7 7
 import org.springframework.boot.ApplicationRunner;
8 8
 import org.springframework.stereotype.Component;
9 9
 
10
+
10 11
 @Component
11 12
 public class BookLoader implements ApplicationRunner {
12 13
     private BookDAO book;

+ 1
- 0
src/main/java/com/example/throwupthursday/config/PersonLoader.java Zobrazit soubor

@@ -7,6 +7,7 @@ import org.springframework.boot.ApplicationArguments;
7 7
 import org.springframework.boot.ApplicationRunner;
8 8
 import org.springframework.stereotype.Component;
9 9
 
10
+
10 11
 @Component
11 12
 public class PersonLoader implements ApplicationRunner {
12 13
     private PersonDAO person;

+ 1
- 0
src/main/java/com/example/throwupthursday/daos/Book.java Zobrazit soubor

@@ -15,6 +15,7 @@ import javax.persistence.GenerationType;
15 15
 //            "isbn10":"0871404427",
16 16
 //            "isbn13":"9780871404428"
17 17
 
18
+
18 19
 @Entity
19 20
 public class Book {
20 21
     @javax.persistence.Id

+ 1
- 0
src/main/java/com/example/throwupthursday/daos/BookDAO.java Zobrazit soubor

@@ -4,6 +4,7 @@ import org.springframework.data.repository.PagingAndSortingRepository;
4 4
 import org.springframework.data.rest.core.annotation.RepositoryRestResource;
5 5
 import org.springframework.stereotype.Repository;
6 6
 
7
+
7 8
 @Repository
8 9
 @RepositoryRestResource(collectionResourceRel = "books", path = "books")
9 10
 public interface BookDAO extends PagingAndSortingRepository<Book, Long> {

+ 2
- 0
src/main/java/com/example/throwupthursday/daos/Person.java Zobrazit soubor

@@ -5,6 +5,8 @@ import org.springframework.data.annotation.Id;
5 5
 import javax.persistence.Entity;
6 6
 import javax.persistence.*;
7 7
 
8
+// POJO
9
+
8 10
 @Entity
9 11
 public class Person {
10 12
     @javax.persistence.Id

+ 2
- 0
src/main/java/com/example/throwupthursday/daos/PersonDAO.java Zobrazit soubor

@@ -4,6 +4,8 @@ import org.springframework.data.repository.PagingAndSortingRepository;
4 4
 import org.springframework.data.rest.core.annotation.RepositoryRestResource;
5 5
 import org.springframework.stereotype.Repository;
6 6
 
7
+// DAO Person
8
+
7 9
 @Repository
8 10
 @RepositoryRestResource(collectionResourceRel = "persons", path = "persons")
9 11
 public interface PersonDAO extends PagingAndSortingRepository<Person, Long> {

+ 4
- 0
src/main/resources/application.properties Zobrazit soubor

@@ -0,0 +1,4 @@
1
+spring.jpa.hibernate.ddl-auto=create
2
+spring.datasource.url=jdbc:mysql://localhost:3306/throwup
3
+spring.datasource.username=kristofer
4
+spring.datasource.password=kristofer