├── README.md
├── pom.xml
└── src
├── main
├── java
│ └── com
│ │ └── javatechie
│ │ └── crud
│ │ └── example
│ │ ├── SpringBootCrudExample2Application.java
│ │ ├── controller
│ │ └── ProductController.java
│ │ ├── entity
│ │ └── Product.java
│ │ ├── repository
│ │ └── ProductRepository.java
│ │ └── service
│ │ └── ProductService.java
└── resources
│ ├── application.properties
│ └── application.yml
└── test
└── java
└── com
└── javatechie
└── crud
└── example
└── SpringBootCrudExample2ApplicationTests.java
/README.md:
--------------------------------------------------------------------------------
1 | # spring-boot-crud-example
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | org.springframework.boot
7 | spring-boot-starter-parent
8 | 2.2.4.RELEASE
9 |
10 |
11 | com.javatechie
12 | spring-boot-crud-example-2
13 | 0.0.1-SNAPSHOT
14 | spring-boot-crud-example-2
15 | Demo project for Spring Boot
16 |
17 |
18 | 1.8
19 |
20 |
21 |
22 |
23 | org.springframework.boot
24 | spring-boot-starter-data-jpa
25 |
26 |
27 | org.springframework.boot
28 | spring-boot-starter-web
29 |
30 |
31 |
32 | mysql
33 | mysql-connector-java
34 | runtime
35 |
36 |
37 | org.projectlombok
38 | lombok
39 | true
40 |
41 |
42 | org.springframework.boot
43 | spring-boot-starter-test
44 | test
45 |
46 |
47 | org.junit.vintage
48 | junit-vintage-engine
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 | org.springframework.boot
58 | spring-boot-maven-plugin
59 |
60 |
61 |
62 |
63 |
64 |
--------------------------------------------------------------------------------
/src/main/java/com/javatechie/crud/example/SpringBootCrudExample2Application.java:
--------------------------------------------------------------------------------
1 | package com.javatechie.crud.example;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | @SpringBootApplication
7 | public class SpringBootCrudExample2Application {
8 |
9 | public static void main(String[] args) {
10 | SpringApplication.run(SpringBootCrudExample2Application.class, args);
11 | }
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/src/main/java/com/javatechie/crud/example/controller/ProductController.java:
--------------------------------------------------------------------------------
1 | package com.javatechie.crud.example.controller;
2 |
3 | import com.javatechie.crud.example.entity.Product;
4 | import com.javatechie.crud.example.service.ProductService;
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.web.bind.annotation.*;
7 |
8 | import java.util.List;
9 |
10 | @RestController
11 | public class ProductController {
12 |
13 | @Autowired
14 | private ProductService service;
15 |
16 | @PostMapping("/addProduct")
17 | public Product addProduct(@RequestBody Product product) {
18 | return service.saveProduct(product);
19 | }
20 |
21 | @PostMapping("/addProducts")
22 | public List addProducts(@RequestBody List products) {
23 | return service.saveProducts(products);
24 | }
25 |
26 | @GetMapping("/products")
27 | public List findAllProducts() {
28 | return service.getProducts();
29 | }
30 |
31 | @GetMapping("/productById/{id}")
32 | public Product findProductById(@PathVariable int id) {
33 | return service.getProductById(id);
34 | }
35 |
36 | @GetMapping("/product/{name}")
37 | public Product findProductByName(@PathVariable String name) {
38 | return service.getProductByName(name);
39 | }
40 |
41 | @PutMapping("/update")
42 | public Product updateProduct(@RequestBody Product product) {
43 | return service.updateProduct(product);
44 | }
45 |
46 | @DeleteMapping("/delete/{id}")
47 | public String deleteProduct(@PathVariable int id) {
48 | return service.deleteProduct(id);
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/src/main/java/com/javatechie/crud/example/entity/Product.java:
--------------------------------------------------------------------------------
1 | package com.javatechie.crud.example.entity;
2 |
3 | import lombok.AllArgsConstructor;
4 | import lombok.Data;
5 | import lombok.NoArgsConstructor;
6 |
7 | import javax.persistence.Entity;
8 | import javax.persistence.GeneratedValue;
9 | import javax.persistence.Id;
10 | import javax.persistence.Table;
11 |
12 | @Data
13 | @AllArgsConstructor
14 | @NoArgsConstructor
15 | @Entity
16 | @Table(name = "PRODUCT_TBL")
17 | public class Product {
18 |
19 | @Id
20 | @GeneratedValue
21 | private int id;
22 | private String name;
23 | private int quantity;
24 | private double price;
25 | }
26 |
--------------------------------------------------------------------------------
/src/main/java/com/javatechie/crud/example/repository/ProductRepository.java:
--------------------------------------------------------------------------------
1 | package com.javatechie.crud.example.repository;
2 |
3 | import com.javatechie.crud.example.entity.Product;
4 | import org.springframework.data.jpa.repository.JpaRepository;
5 |
6 | public interface ProductRepository extends JpaRepository {
7 | Product findByName(String name);
8 | }
9 |
10 |
--------------------------------------------------------------------------------
/src/main/java/com/javatechie/crud/example/service/ProductService.java:
--------------------------------------------------------------------------------
1 | package com.javatechie.crud.example.service;
2 |
3 | import com.javatechie.crud.example.entity.Product;
4 | import com.javatechie.crud.example.repository.ProductRepository;
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.stereotype.Service;
7 |
8 | import java.util.List;
9 |
10 | @Service
11 | public class ProductService {
12 | @Autowired
13 | private ProductRepository repository;
14 |
15 | public Product saveProduct(Product product) {
16 | return repository.save(product);
17 | }
18 |
19 | public List saveProducts(List products) {
20 | return repository.saveAll(products);
21 | }
22 |
23 | public List getProducts() {
24 | return repository.findAll();
25 | }
26 |
27 | public Product getProductById(int id) {
28 | return repository.findById(id).orElse(null);
29 | }
30 |
31 | public Product getProductByName(String name) {
32 | return repository.findByName(name);
33 | }
34 |
35 | public String deleteProduct(int id) {
36 | repository.deleteById(id);
37 | return "product removed !! " + id;
38 | }
39 |
40 | public Product updateProduct(Product product) {
41 | Product existingProduct = repository.findById(product.getId()).orElse(null);
42 | existingProduct.setName(product.getName());
43 | existingProduct.setQuantity(product.getQuantity());
44 | existingProduct.setPrice(product.getPrice());
45 | return repository.save(existingProduct);
46 | }
47 |
48 |
49 | }
50 |
--------------------------------------------------------------------------------
/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
2 | spring.datasource.url = jdbc:mysql://localhost:3306/javatechie
3 | spring.datasource.username = root
4 | spring.datasource.password = Password
5 | spring.jpa.show-sql = true
6 | spring.jpa.hibernate.ddl-auto = update
7 | spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
8 | server.port=9191
9 |
--------------------------------------------------------------------------------
/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | #spring:
2 | # datasource:
3 | # username: root
4 | # password: password
5 | # url: jdbc:mysql://localhost:3306/javatechie
6 | # driver-class-name: com.mysql.cj.jdbc.Driver
7 | # jpa:
8 | # show-sql: true
9 | # hibernate:
10 | # ddl-auto: update
11 | # properties.hibernate.dialect: org.hibernate.dialect.MySQL5Dialect
12 |
--------------------------------------------------------------------------------
/src/test/java/com/javatechie/crud/example/SpringBootCrudExample2ApplicationTests.java:
--------------------------------------------------------------------------------
1 | package com.javatechie.crud.example;
2 |
3 | import org.junit.jupiter.api.Test;
4 | import org.springframework.boot.test.context.SpringBootTest;
5 |
6 | @SpringBootTest
7 | class SpringBootCrudExample2ApplicationTests {
8 |
9 | @Test
10 | void contextLoads() {
11 | }
12 |
13 | }
14 |
--------------------------------------------------------------------------------