Bläddra i källkod

Added handy mysql repository for Person(s).

Kr Younger 6 år sedan
förälder
incheckning
79064ccd57

+ 5
- 0
pom.xml Visa fil

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

+ 41
- 0
src/main/java/com/example/throwupthursday/PersonController.java Visa fil

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 Visa fil

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 Visa fil

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

+ 1
- 0
src/main/java/com/example/throwupthursday/config/BookLoader.java Visa fil

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

+ 1
- 0
src/main/java/com/example/throwupthursday/config/PersonLoader.java Visa fil

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

+ 1
- 0
src/main/java/com/example/throwupthursday/daos/Book.java Visa fil

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

+ 1
- 0
src/main/java/com/example/throwupthursday/daos/BookDAO.java Visa fil

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

+ 2
- 0
src/main/java/com/example/throwupthursday/daos/Person.java Visa fil

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

+ 2
- 0
src/main/java/com/example/throwupthursday/daos/PersonDAO.java Visa fil

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

+ 4
- 0
src/main/resources/application.properties Visa fil

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