├── .gitignore
├── README.md
├── pom.xml
└── src
├── main
├── java
│ └── guru
│ │ └── springframework
│ │ ├── SpringBootDb2Application.java
│ │ ├── commands
│ │ └── ProductForm.java
│ │ ├── controllers
│ │ └── ProductController.java
│ │ ├── converters
│ │ ├── ProductFormToProduct.java
│ │ └── ProductToProductForm.java
│ │ ├── domain
│ │ └── Product.java
│ │ ├── repositories
│ │ └── ProductRepository.java
│ │ └── services
│ │ ├── ProductService.java
│ │ └── ProductServiceImpl.java
└── resources
│ ├── application.properties
│ └── templates
│ └── product
│ ├── list.html
│ ├── productform.html
│ └── show.html
└── test
└── java
└── guru
└── springframework
├── SpringBootDb2ApplicationTests.java
└── repositories
└── ProductRepositoryTest.java
/.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 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # spring-boot-db2-example
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | guru.springframework
8 | spring-boot-db2
9 | 0.0.1-SNAPSHOT
10 | jar
11 |
12 | spring-boot-db2
13 | Demo project for Spring Boot and DB2
14 |
15 |
16 | org.springframework.boot
17 | spring-boot-starter-parent
18 | 1.5.2.RELEASE
19 |
20 |
21 |
22 |
23 | UTF-8
24 | UTF-8
25 | 1.8
26 |
27 |
28 |
29 |
30 | com.ibm.db2.jcc
31 | https://artifacts.alfresco.com/nexus/content/repositories/public/
32 |
33 |
34 |
35 |
36 |
37 | com.ibm.db2.jcc
38 | db2jcc4
39 | 10.1
40 |
41 |
42 | org.springframework.boot
43 | spring-boot-starter-data-jpa
44 |
45 |
46 | org.springframework.boot
47 | spring-boot-starter-thymeleaf
48 |
49 |
50 | org.springframework.boot
51 | spring-boot-starter-web
52 |
53 |
54 |
55 | org.springframework.boot
56 | spring-boot-starter-test
57 | test
58 |
59 |
60 |
61 |
62 |
63 |
64 | org.springframework.boot
65 | spring-boot-maven-plugin
66 |
67 |
68 |
69 |
70 |
71 |
72 |
--------------------------------------------------------------------------------
/src/main/java/guru/springframework/SpringBootDb2Application.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 SpringBootDb2Application {
8 |
9 | public static void main(String[] args) {
10 | SpringApplication.run(SpringBootDb2Application.class, args);
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/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/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 |
--------------------------------------------------------------------------------
/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/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/domain/Product.java:
--------------------------------------------------------------------------------
1 | package guru.springframework.domain;
2 |
3 | import javax.persistence.Entity;
4 | import javax.persistence.GeneratedValue;
5 | import java.math.BigDecimal;
6 |
7 | /**
8 | * Created by jt on 1/10/17.
9 | */
10 | @Entity
11 | public class Product {
12 |
13 | @javax.persistence.Id
14 | @GeneratedValue
15 | private Long id;
16 | private String description;
17 | private BigDecimal price;
18 | private String imageUrl;
19 |
20 | public Long getId() {
21 | return id;
22 | }
23 |
24 | public void setId(Long id) {
25 | this.id = id;
26 | }
27 |
28 | public String getDescription() {
29 | return description;
30 | }
31 |
32 | public void setDescription(String description) {
33 | this.description = description;
34 | }
35 |
36 | public BigDecimal getPrice() {
37 | return price;
38 | }
39 |
40 | public void setPrice(BigDecimal price) {
41 | this.price = price;
42 | }
43 |
44 | public String getImageUrl() {
45 | return imageUrl;
46 | }
47 |
48 | public void setImageUrl(String imageUrl) {
49 | this.imageUrl = imageUrl;
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/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/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/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.findOne(id);
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.delete(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/application.properties:
--------------------------------------------------------------------------------
1 | # ===============================
2 | # = DATA SOURCE
3 | # ===============================
4 | # Set here configurations for the database connection
5 | spring.datasource.url=jdbc:db2://localhost:50000/EXAMPLE
6 | spring.datasource.username=db2inst1
7 | spring.datasource.password=db2inst1-pwd
8 | spring.datasource.driver-class-name=com.ibm.db2.jcc.DB2Driver
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 | # Naming strategy
21 | spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyHbmImpl
22 | spring.jpa.hibernate.naming.physical-strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
23 | # Allows Hibernate to generate SQL optimized for a particular DBMS
24 | spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.DB2Dialect
--------------------------------------------------------------------------------
/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 | Id |
24 | Description |
25 | Price |
26 | Image URL |
27 | List |
28 | Edit |
29 | Delete |
30 |
31 |
32 | |
33 | |
34 | |
35 | |
36 | View |
37 | Edit |
38 | Delete |
39 |
40 |
41 |
42 |
47 |
48 |
49 |
50 |
--------------------------------------------------------------------------------
/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 |
73 |
74 |
75 |
76 |
--------------------------------------------------------------------------------
/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 |
55 |
56 |
57 |
58 |
--------------------------------------------------------------------------------
/src/test/java/guru/springframework/SpringBootDb2ApplicationTests.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 SpringBootDb2ApplicationTests {
11 |
12 | @Test
13 | public void contextLoads() {
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/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.findOne(product.getId());
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 | }
--------------------------------------------------------------------------------