├── README.md ├── .gitignore ├── src ├── main │ ├── java │ │ └── guru │ │ │ └── springframework │ │ │ ├── repositories │ │ │ └── ProductRepository.java │ │ │ ├── SpringBootPostgreSQLApplication.java │ │ │ ├── services │ │ │ ├── ProductService.java │ │ │ └── ProductServiceImpl.java │ │ │ ├── converters │ │ │ ├── ProductToProductForm.java │ │ │ └── ProductFormToProduct.java │ │ │ ├── commands │ │ │ └── ProductForm.java │ │ │ ├── domain │ │ │ └── Product.java │ │ │ └── controllers │ │ │ └── ProductController.java │ └── resources │ │ ├── application.properties │ │ └── templates │ │ └── product │ │ ├── list.html │ │ ├── show.html │ │ └── productform.html └── test │ └── java │ └── guru │ └── springframework │ ├── SpringBootPostgreSQLApplicationTests.java │ └── repositories │ └── ProductRepositoryTest.java └── pom.xml /README.md: -------------------------------------------------------------------------------- 1 | # Spring Boot Postgress Example 2 | 3 | You can learn more about my courses [here](http://courses.springframework.guru/courses/) on my site. -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | 3 | # Mobile Tools for Java (J2ME) 4 | .mtj.tmp/ 5 | 6 | # Package Files # 7 | *.jar 8 | *.war 9 | *.ear 10 | 11 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 12 | hs_err_pid* 13 | 14 | target 15 | 16 | # IDE files 17 | .idea 18 | *.iml 19 | -------------------------------------------------------------------------------- /src/main/java/guru/springframework/repositories/ProductRepository.java: -------------------------------------------------------------------------------- 1 | package guru.springframework.repositories; 2 | 3 | import guru.springframework.domain.Product; 4 | import org.springframework.data.repository.CrudRepository; 5 | 6 | /** 7 | * Created by jt on 1/10/17. 8 | */ 9 | public interface ProductRepository extends CrudRepository { 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/guru/springframework/SpringBootPostgreSQLApplication.java: -------------------------------------------------------------------------------- 1 | package guru.springframework; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringBootPostgreSQLApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringBootPostgreSQLApplication.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/test/java/guru/springframework/SpringBootPostgreSQLApplicationTests.java: -------------------------------------------------------------------------------- 1 | package guru.springframework; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class SpringBootPostgreSQLApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/guru/springframework/services/ProductService.java: -------------------------------------------------------------------------------- 1 | package guru.springframework.services; 2 | 3 | import guru.springframework.commands.ProductForm; 4 | import guru.springframework.domain.Product; 5 | 6 | import java.util.List; 7 | 8 | /** 9 | * Created by jt on 1/10/17. 10 | */ 11 | public interface ProductService { 12 | 13 | List listAll(); 14 | 15 | Product getById(Long id); 16 | 17 | Product saveOrUpdate(Product product); 18 | 19 | void delete(Long id); 20 | 21 | Product saveOrUpdateProductForm(ProductForm productForm); 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/guru/springframework/converters/ProductToProductForm.java: -------------------------------------------------------------------------------- 1 | package guru.springframework.converters; 2 | 3 | import guru.springframework.commands.ProductForm; 4 | import guru.springframework.domain.Product; 5 | import org.springframework.core.convert.converter.Converter; 6 | import org.springframework.stereotype.Component; 7 | 8 | /** 9 | * Created by jt on 1/10/17. 10 | */ 11 | @Component 12 | public class ProductToProductForm implements Converter { 13 | @Override 14 | public ProductForm convert(Product product) { 15 | ProductForm productForm = new ProductForm(); 16 | productForm.setId(product.getId()); 17 | productForm.setDescription(product.getDescription()); 18 | productForm.setPrice(product.getPrice()); 19 | productForm.setImageUrl(product.getImageUrl()); 20 | return productForm; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/guru/springframework/converters/ProductFormToProduct.java: -------------------------------------------------------------------------------- 1 | package guru.springframework.converters; 2 | 3 | import guru.springframework.commands.ProductForm; 4 | import guru.springframework.domain.Product; 5 | import org.springframework.core.convert.converter.Converter; 6 | import org.springframework.stereotype.Component; 7 | import org.springframework.util.StringUtils; 8 | 9 | /** 10 | * Created by jt on 1/10/17. 11 | */ 12 | @Component 13 | public class ProductFormToProduct implements Converter { 14 | 15 | @Override 16 | public Product convert(ProductForm productForm) { 17 | Product product = new Product(); 18 | if (productForm.getId() != null && !StringUtils.isEmpty(productForm.getId())) { 19 | product.setId(new Long(productForm.getId())); 20 | } 21 | product.setDescription(productForm.getDescription()); 22 | product.setPrice(productForm.getPrice()); 23 | product.setImageUrl(productForm.getImageUrl()); 24 | return product; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/guru/springframework/commands/ProductForm.java: -------------------------------------------------------------------------------- 1 | package guru.springframework.commands; 2 | 3 | 4 | import java.math.BigDecimal; 5 | 6 | /** 7 | * Created by jt on 1/10/17. 8 | */ 9 | public class ProductForm { 10 | private Long id; 11 | private String description; 12 | private BigDecimal price; 13 | private String imageUrl; 14 | 15 | public Long getId() { 16 | return id; 17 | } 18 | 19 | public void setId(Long id) { 20 | this.id = id; 21 | } 22 | 23 | public String getDescription() { 24 | return description; 25 | } 26 | 27 | public void setDescription(String description) { 28 | this.description = description; 29 | } 30 | 31 | public BigDecimal getPrice() { 32 | return price; 33 | } 34 | 35 | public void setPrice(BigDecimal price) { 36 | this.price = price; 37 | } 38 | 39 | public String getImageUrl() { 40 | return imageUrl; 41 | } 42 | 43 | public void setImageUrl(String imageUrl) { 44 | this.imageUrl = imageUrl; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/guru/springframework/domain/Product.java: -------------------------------------------------------------------------------- 1 | package guru.springframework.domain; 2 | 3 | 4 | import javax.persistence.Entity; 5 | import javax.persistence.GeneratedValue; 6 | import javax.persistence.GenerationType; 7 | import javax.persistence.Id; 8 | import java.math.BigDecimal; 9 | 10 | 11 | @Entity 12 | public class Product { 13 | 14 | @Id 15 | @GeneratedValue(strategy = GenerationType.IDENTITY) 16 | private Long _id; 17 | private String description; 18 | private BigDecimal price; 19 | private String imageUrl; 20 | 21 | public Long getId() { 22 | return _id; 23 | } 24 | 25 | public void setId(Long id) { 26 | this._id = id; 27 | } 28 | 29 | public String getDescription() { 30 | return description; 31 | } 32 | 33 | public void setDescription(String description) { 34 | this.description = description; 35 | } 36 | 37 | public BigDecimal getPrice() { 38 | return price; 39 | } 40 | 41 | public void setPrice(BigDecimal price) { 42 | this.price = price; 43 | } 44 | 45 | public String getImageUrl() { 46 | return imageUrl; 47 | } 48 | 49 | public void setImageUrl(String imageUrl) { 50 | this.imageUrl = imageUrl; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | # =============================== 2 | # = DATA SOURCE 3 | # =============================== 4 | # Set here configurations for the database connection 5 | spring.datasource.url=jdbc:postgresql://localhost:5432/springbootdb 6 | spring.datasource.username=postgres 7 | spring.datasource.password=postgres@123 8 | spring.datasource.driver-class-name=org.postgresql.Driver 9 | # Keep the connection alive if idle for a long time (needed in production) 10 | spring.datasource.testWhileIdle=true 11 | spring.datasource.validationQuery=SELECT 1 12 | # =============================== 13 | # = JPA / HIBERNATE 14 | # =============================== 15 | # Show or not log for each sql query 16 | spring.jpa.show-sql=true 17 | # Hibernate ddl auto (create, create-drop, update): with "create-drop" the database 18 | # schema will be automatically created afresh for every start of application 19 | spring.jpa.hibernate.ddl-auto=create-drop 20 | 21 | # Naming strategy 22 | spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyHbmImpl 23 | spring.jpa.hibernate.naming.physical-strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy 24 | 25 | # Allows Hibernate to generate SQL optimized for a particular DBMS 26 | spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect -------------------------------------------------------------------------------- /src/test/java/guru/springframework/repositories/ProductRepositoryTest.java: -------------------------------------------------------------------------------- 1 | package guru.springframework.repositories; 2 | 3 | import guru.springframework.domain.Product; 4 | import org.junit.Assert; 5 | import org.junit.Before; 6 | import org.junit.Test; 7 | import org.junit.runner.RunWith; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.boot.test.context.SpringBootTest; 10 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 11 | 12 | import java.math.BigDecimal; 13 | 14 | @RunWith(SpringJUnit4ClassRunner.class) 15 | @SpringBootTest 16 | public class ProductRepositoryTest { 17 | 18 | private static final BigDecimal BIG_DECIMAL_100 = BigDecimal.valueOf(100.00); 19 | private static final String PRODUCT_DESCRIPTION = "a cool product"; 20 | private static final String IMAGE_URL = "http://an-imageurl.com/image1.jpg"; 21 | 22 | @Autowired 23 | private ProductRepository productRepository; 24 | 25 | @Before 26 | public void setUp() throws Exception { 27 | 28 | } 29 | 30 | @Test 31 | public void testPersistence() { 32 | //given 33 | Product product = new Product(); 34 | product.setDescription(PRODUCT_DESCRIPTION); 35 | product.setImageUrl(IMAGE_URL); 36 | product.setPrice(BIG_DECIMAL_100); 37 | 38 | //when 39 | productRepository.save(product); 40 | 41 | //then 42 | Assert.assertNotNull(product.getId()); 43 | Product newProduct = productRepository.findById(product.getId()).orElse(null); 44 | Assert.assertEquals((Long) 1L, newProduct.getId()); 45 | Assert.assertEquals(PRODUCT_DESCRIPTION, newProduct.getDescription()); 46 | Assert.assertEquals(BIG_DECIMAL_100.compareTo(newProduct.getPrice()), 0); 47 | Assert.assertEquals(IMAGE_URL, newProduct.getImageUrl()); 48 | } 49 | } -------------------------------------------------------------------------------- /src/main/java/guru/springframework/services/ProductServiceImpl.java: -------------------------------------------------------------------------------- 1 | package guru.springframework.services; 2 | 3 | import guru.springframework.commands.ProductForm; 4 | import guru.springframework.converters.ProductFormToProduct; 5 | import guru.springframework.domain.Product; 6 | import guru.springframework.repositories.ProductRepository; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.stereotype.Service; 9 | 10 | import java.util.ArrayList; 11 | import java.util.List; 12 | 13 | /** 14 | * Created by jt on 1/10/17. 15 | */ 16 | @Service 17 | public class ProductServiceImpl implements ProductService { 18 | 19 | private ProductRepository productRepository; 20 | private ProductFormToProduct productFormToProduct; 21 | 22 | @Autowired 23 | public ProductServiceImpl(ProductRepository productRepository, ProductFormToProduct productFormToProduct) { 24 | this.productRepository = productRepository; 25 | this.productFormToProduct = productFormToProduct; 26 | } 27 | 28 | 29 | @Override 30 | public List listAll() { 31 | List products = new ArrayList<>(); 32 | productRepository.findAll().forEach(products::add); //fun with Java 8 33 | return products; 34 | } 35 | 36 | @Override 37 | public Product getById(Long id) { 38 | return productRepository.findById(id).orElse(null); 39 | } 40 | 41 | @Override 42 | public Product saveOrUpdate(Product product) { 43 | productRepository.save(product); 44 | return product; 45 | } 46 | 47 | @Override 48 | public void delete(Long id) { 49 | productRepository.deleteById(id); 50 | 51 | } 52 | 53 | @Override 54 | public Product saveOrUpdateProductForm(ProductForm productForm) { 55 | Product savedProduct = saveOrUpdate(productFormToProduct.convert(productForm)); 56 | 57 | System.out.println("Saved Product Id: " + savedProduct.getId()); 58 | return savedProduct; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/main/resources/templates/product/list.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Spring Core Online Tutorial - List Products 5 | 6 | 7 | 10 | 11 | 13 | 14 | 16 | 17 | 18 |
19 |
20 |

Product List

21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 |
IdDescriptionPriceImage URLListEditDelete
View Edit Delete
41 |
42 |
43 |
44 | New Product 45 |
46 |
47 |
48 | 49 | 50 | -------------------------------------------------------------------------------- /src/main/resources/templates/product/show.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Spring Core Online Tutorial - Show Product 5 | 6 | 7 | 10 | 11 | 13 | 14 | 16 | 17 | 18 |
19 | 20 |
21 |
22 |

Show Product

23 |
24 |
25 |
26 |
27 |
28 |
29 | 30 |
31 |

Product Id

32 |
33 |
34 |
35 | 36 |
37 |

Description

38 |
39 |
40 |
41 | 42 |
43 |

Price

44 |
45 |
46 |
47 | 48 |
49 |

Image

50 |
51 |
52 |
53 |
54 |
55 |
56 | 57 | 58 | -------------------------------------------------------------------------------- /src/main/java/guru/springframework/controllers/ProductController.java: -------------------------------------------------------------------------------- 1 | package guru.springframework.controllers; 2 | 3 | import guru.springframework.commands.ProductForm; 4 | import guru.springframework.converters.ProductToProductForm; 5 | import guru.springframework.domain.Product; 6 | import guru.springframework.services.ProductService; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.stereotype.Controller; 9 | import org.springframework.ui.Model; 10 | import org.springframework.validation.BindingResult; 11 | import org.springframework.web.bind.annotation.PathVariable; 12 | import org.springframework.web.bind.annotation.RequestMapping; 13 | import org.springframework.web.bind.annotation.RequestMethod; 14 | 15 | import javax.validation.Valid; 16 | 17 | /** 18 | * Created by jt on 1/10/17. 19 | */ 20 | @Controller 21 | public class ProductController { 22 | private ProductService productService; 23 | 24 | private ProductToProductForm productToProductForm; 25 | 26 | @Autowired 27 | public void setProductToProductForm(ProductToProductForm productToProductForm) { 28 | this.productToProductForm = productToProductForm; 29 | } 30 | 31 | @Autowired 32 | public void setProductService(ProductService productService) { 33 | this.productService = productService; 34 | } 35 | 36 | @RequestMapping("/") 37 | public String redirToList(){ 38 | return "redirect:/product/list"; 39 | } 40 | 41 | @RequestMapping({"/product/list", "/product"}) 42 | public String listProducts(Model model){ 43 | model.addAttribute("products", productService.listAll()); 44 | return "product/list"; 45 | } 46 | 47 | @RequestMapping("/product/show/{id}") 48 | public String getProduct(@PathVariable String id, Model model){ 49 | model.addAttribute("product", productService.getById(Long.valueOf(id))); 50 | return "product/show"; 51 | } 52 | 53 | @RequestMapping("product/edit/{id}") 54 | public String edit(@PathVariable String id, Model model){ 55 | Product product = productService.getById(Long.valueOf(id)); 56 | ProductForm productForm = productToProductForm.convert(product); 57 | 58 | model.addAttribute("productForm", productForm); 59 | return "product/productform"; 60 | } 61 | 62 | @RequestMapping("/product/new") 63 | public String newProduct(Model model){ 64 | model.addAttribute("productForm", new ProductForm()); 65 | return "product/productform"; 66 | } 67 | 68 | @RequestMapping(value = "/product", method = RequestMethod.POST) 69 | public String saveOrUpdateProduct(@Valid ProductForm productForm, BindingResult bindingResult){ 70 | 71 | if(bindingResult.hasErrors()){ 72 | return "product/productform"; 73 | } 74 | 75 | Product savedProduct = productService.saveOrUpdateProductForm(productForm); 76 | 77 | return "redirect:/product/show/" + savedProduct.getId(); 78 | } 79 | 80 | @RequestMapping("/product/delete/{id}") 81 | public String delete(@PathVariable String id){ 82 | productService.delete(Long.valueOf(id)); 83 | return "redirect:/product/list"; 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | guru.springframework 7 | spring-boot-postgres 8 | 0.0.1-SNAPSHOT 9 | jar 10 | 11 | spring-boot-postgres 12 | Demo project for Spring Boot and PostgreSQL 13 | 14 | 15 | org.springframework.boot 16 | spring-boot-starter-parent 17 | 2.0.0.RELEASE 18 | 19 | 20 | 21 | 22 | UTF-8 23 | UTF-8 24 | 1.8 25 | 26 | 27 | 28 | 29 | org.postgresql 30 | postgresql 31 | 9.4-1206-jdbc42 32 | 33 | 34 | 35 | org.springframework.boot 36 | spring-boot-starter-data-jpa 37 | 38 | 39 | org.springframework.boot 40 | spring-boot-starter-thymeleaf 41 | 42 | 43 | org.springframework.boot 44 | spring-boot-starter-web 45 | 46 | 47 | 48 | org.springframework.boot 49 | spring-boot-starter-test 50 | test 51 | 52 | 53 | 54 | 55 | 56 | 57 | org.springframework.boot 58 | spring-boot-maven-plugin 59 | 60 | 61 | 62 | 63 | 64 | spring-snapshots 65 | Spring Snapshots 66 | https://repo.spring.io/snapshot 67 | 68 | true 69 | 70 | 71 | 72 | spring-milestones 73 | Spring Milestones 74 | https://repo.spring.io/milestone 75 | 76 | false 77 | 78 | 79 | 80 | 81 | 82 | 83 | spring-snapshots 84 | Spring Snapshots 85 | https://repo.spring.io/snapshot 86 | 87 | true 88 | 89 | 90 | 91 | spring-milestones 92 | Spring Milestones 93 | https://repo.spring.io/milestone 94 | 95 | false 96 | 97 | 98 | 99 | 100 | 101 | -------------------------------------------------------------------------------- /src/main/resources/templates/product/productform.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Spring Core Online Tutorial - Product Form 5 | 6 | 7 | 10 | 11 | 13 | 14 | 16 | 17 | 18 |
19 | 20 |

Product Details

21 |
22 |
23 | 24 |
25 |

Error Message

26 |
27 | 28 | 29 | 30 |
31 | 32 |
33 | 34 | 35 | 36 |
    37 |
  • 38 |
39 |
40 |
41 |
42 | 43 |
44 | 45 |
46 | 47 | 48 | 49 |
    50 |
  • 51 |
52 |
53 |
54 |
55 | 56 |
57 | 58 |
59 | 60 | 61 | 62 |
    63 |
  • 64 |
65 |
66 |
67 |
68 |
69 | 70 |
71 |
72 |
73 |
74 | 75 | 76 | --------------------------------------------------------------------------------