Skip to content

Commit abb2e52

Browse files
committed
Tuturial 5: Toegang tot database vanuit de applicatie
We hebben de database structuur gegeven en gevuld met testdata. We zijn nu klaar om deze data te gaan gebruiken in de applicatie. In deze tutorial kijken we hoe we data uit de H2 database kunnen opvragen en deze kunnen gebruiken in onze API. https://tweakers.net/advertorials/onlinejavaacademy2/tutorial5/
1 parent 366fd7e commit abb2e52

File tree

7 files changed

+76
-70
lines changed

7 files changed

+76
-70
lines changed
Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,27 @@
11
package tutorial.api.controllers;
22

3-
import org.springframework.web.bind.annotation.PathVariable;
4-
import org.springframework.web.bind.annotation.RequestMapping;
5-
import org.springframework.web.bind.annotation.RequestMethod;
6-
import org.springframework.web.bind.annotation.RestController;
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.web.bind.annotation.*;
75
import tutorial.persistence.models.Product;
6+
import tutorial.persistence.repositories.ProductRepository;
87

98
import java.math.BigDecimal;
10-
import java.util.ArrayList;
119
import java.util.List;
1210

1311
@RestController
1412
public class ProductController extends ApiController {
1513

14+
@Autowired
15+
ProductRepository productRepository;
16+
1617
@RequestMapping(value="/products", method=RequestMethod.GET)
1718
public List<Product> showAll() {
18-
List<Product> products = new ArrayList<>();
19-
20-
products.add(new Product(1, "Macbook Air", new BigDecimal("999.95")));
21-
products.add(new Product(2, "Microsoft Surface 3", new BigDecimal("449")));
22-
products.add(new Product(3, "Chromebook Pixel C", new BigDecimal("595.50"))
23-
);
24-
25-
return products;
19+
return (List<Product>) productRepository.findAll();
2620
}
2721

2822
@RequestMapping(value="/products/{productId}", method=RequestMethod.GET)
2923
public Product show(@PathVariable("productId") long productId) {
30-
return new Product(productId, "Macbook Air", new BigDecimal("999.95"));
24+
return productRepository.findOne(productId);
3125
}
3226

33-
}
27+
}
Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,29 @@
11
package tutorial.api.controllers;
22

3+
import org.springframework.beans.factory.annotation.Autowired;
34
import org.springframework.web.bind.annotation.PathVariable;
45
import org.springframework.web.bind.annotation.RequestMapping;
56
import org.springframework.web.bind.annotation.RequestMethod;
67
import org.springframework.web.bind.annotation.RestController;
7-
import tutorial.persistence.models.Product;
88
import tutorial.persistence.models.ProductList;
9+
import tutorial.persistence.repositories.ProductListRepository;
910

10-
import java.math.BigDecimal;
11-
import java.util.ArrayList;
1211
import java.util.List;
1312

1413
@RestController
1514
public class ProductListController extends ApiController {
1615

16+
@Autowired
17+
ProductListRepository productListRepository;
18+
1719
@RequestMapping(value="/lists", method= RequestMethod.GET)
1820
public List<ProductList> showAll() {
19-
List<ProductList> productLists = new ArrayList<>();
20-
21-
for (int i = 1; i < 4; i++) {
22-
ProductList productList = new ProductList(i, "Tech Goodies (" + i + ")"
23-
);
24-
25-
List<Product> products = new ArrayList<>();
26-
products.add(new Product(1, "Macbook Air", new BigDecimal("999.95")));
27-
products.add(new Product(2, "Microsoft Surface 3", new BigDecimal("449"
28-
)));
29-
productList.setProducts(products);
30-
31-
productLists.add(productList);
32-
}
33-
34-
return productLists;
21+
return (List<ProductList>) productListRepository.findAll();
3522
}
3623

3724
@RequestMapping(value="/lists/{productListId}", method=RequestMethod.GET)
3825
public ProductList show(@PathVariable("productListId") long productListId) {
39-
ProductList productList = new ProductList(productListId, "Tech Goodies (" + productListId + ")");
40-
41-
List<Product> products = new ArrayList<>();
42-
products.add(new Product(1, "Macbook Air", new BigDecimal("999.95")));
43-
products.add(new Product(2, "Microsoft Surface 3", new BigDecimal("449")));
44-
productList.setProducts(products);
45-
46-
return productList;
26+
return productListRepository.findOne(productListId);
4727
}
4828

49-
}
29+
}
Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,31 @@
1-
// Product.java
21
package tutorial.persistence.models;
32

3+
import com.fasterxml.jackson.annotation.JsonBackReference;
4+
5+
import javax.persistence.*;
46
import java.math.BigDecimal;
7+
import java.util.List;
58

9+
@Entity
610
public class Product {
711

8-
private final long id;
9-
private final String name;
10-
private final BigDecimal price;
12+
@Id
13+
@GeneratedValue
14+
private long id;
1115

12-
public Product(String name, BigDecimal price) {
13-
this.id = -1;
14-
this.name = name;
15-
this.price = price;
16-
}
16+
@Column(nullable=false)
17+
private String name;
18+
19+
@Column(nullable=false)
20+
private BigDecimal price;
1721

18-
public Product(long id, String name, BigDecimal price) {
19-
this.id = id;
22+
@ManyToMany(mappedBy="products")
23+
@JsonBackReference
24+
private List<ProductList> lists;
25+
26+
public Product() {}
27+
28+
public Product(String name, BigDecimal price) {
2029
this.name = name;
2130
this.price = price;
2231
}
@@ -33,4 +42,8 @@ public BigDecimal getPrice() {
3342
return price;
3443
}
3544

36-
}
45+
public List<ProductList> getLists() {
46+
return lists;
47+
}
48+
49+
}
Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
1-
// ProductList.java
21
package tutorial.persistence.models;
32

3+
import com.fasterxml.jackson.annotation.JsonManagedReference;
4+
5+
import javax.persistence.*;
46
import java.util.List;
57

8+
@Entity
69
public class ProductList {
710

8-
private final long id;
9-
private final String name;
11+
@Id
12+
@GeneratedValue
13+
private long id;
14+
15+
@Column(nullable=false)
16+
private String name;
17+
18+
@ManyToMany
19+
@JsonManagedReference
1020
private List<Product> products;
1121

12-
public ProductList(String name) {
13-
this.id = -1;
14-
this.name = name;
15-
}
22+
public ProductList() {}
1623

17-
public ProductList(long id, String name) {
18-
this.id = id;
24+
public ProductList(String name) {
1925
this.name = name;
2026
}
2127

@@ -31,8 +37,4 @@ public List<Product> getProducts() {
3137
return products;
3238
}
3339

34-
public void setProducts(List<Product> products) {
35-
this.products = products;
36-
}
37-
38-
}
40+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package tutorial.persistence.repositories;
2+
3+
import org.springframework.data.repository.CrudRepository;
4+
import tutorial.persistence.models.ProductList;
5+
6+
public interface ProductListRepository extends CrudRepository<ProductList, Long> {
7+
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package tutorial.persistence.repositories;
2+
3+
import org.springframework.data.repository.CrudRepository;
4+
import tutorial.persistence.models.Product;
5+
6+
public interface ProductRepository extends CrudRepository<Product, Long> {
7+
8+
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
spring.profiles.active=dev
1+
spring.profiles.active=dev
2+
spring.jpa.hibernate.ddl-auto=none

0 commit comments

Comments
 (0)