-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpringbootApplication.java
More file actions
141 lines (126 loc) · 6.11 KB
/
Copy pathSpringbootApplication.java
File metadata and controls
141 lines (126 loc) · 6.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package com.example.springbootjpacrudexample;
import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.example.springbootjpacrudexample.entities.Product;
import com.example.springbootjpacrudexample.repositories.ProductRepository;
@SpringBootApplication
public class SpringbootApplication implements CommandLineRunner {
@Autowired
ProductRepository productRepository;
public static void main(String[] args) {
SpringApplication.run(SpringbootApplication.class, args);
}
@Override
public void run(String... arg0) throws Exception {
System.out.println("\n----------------------------------------------------------------");
System.out.println("Sring boot examples by Deepak");
System.out.println(" . ____ _ __ _ _\n" +
" /\\\\ / ___'_ __ _ _(_)_ __ __ _ \\ \\ \\ \\\n" +
"( ( )\\___ | '_ | '_| | '_ \\/ _` | \\ \\ \\ \\\n" +
" \\\\/ ___)| |_)| | | | | || (_| | ) ) ) )\n" +
" ' |____| .__|_| |_|_| |_\\__, | / / / /\n" +
" =========|_|==============|___/=/_/_/_/");
System.out.println("\nSpring Boot JPA CRUD Examples");
System.out.println("----------------------------------------------------------------\n");
// ---- CRUD Operations ----
findAll(); // Retrieve (R)
findByPrimaryKey(); // Retrieve (R)
findUsingCustomeQuery(); // Retrieve (R)
createEntity(); // Create (C)
updateEntity(); // Update (U)
deleteEntity(); // Delete (D)
}
/**
* Retrieves all the records in a given table.
*/
private void findAll() {
Iterable<Product> products = productRepository.findAll();
System.out.println("\n***** Let's find all the records from the database table *****\n");
for (Product product : products) {
System.out.println("Product Id : " + product.getId());
System.out.println("Product Name : " + product.getName());
System.out.println("Product Price : " + product.getPrice());
System.out.println("Product Quantity : " + product.getQuantity());
System.out.println("====================================================");
}
}
/**
* Retrieves one record against a particular id.
*/
private void findByPrimaryKey() {
Optional<Product> product = productRepository.findById(1);
if (product.isPresent()) {
Product retrievedProduct = product.get();
System.out.println("\n***** Let's find a records by its id from the database table *****\n");
System.out.println("Product Id : " + retrievedProduct.getId());
System.out.println("Product Name : " + retrievedProduct.getName());
System.out.println("Product Price : " + retrievedProduct.getPrice());
System.out.println("Product Quantity : " + retrievedProduct.getQuantity());
System.out.println("====================================================");
}
}
/**
* Custom query example
*/
private void findUsingCustomeQuery() {
List<Product> products = productRepository.search("bi");
for(Product product: products) {
System.out.println("\n***** Let's find a records which has 'bi' in their names *****\n");
System.out.println("Product Id : " + product.getId());
System.out.println("Product Name : " + product.getName());
System.out.println("Product Price : " + product.getPrice());
System.out.println("Product Quantity : " + product.getQuantity());
System.out.println("====================================================");
}
}
/**
* Creates a new record in the database table using Spring Data JPA in Spring
* Boot.
*/
private void createEntity() {
System.out.println("\n***** Let's create a new record *****\n");
Product newProduct = new Product();
newProduct.setName("Mobile 2");
newProduct.setPrice(new BigDecimal(2000));
newProduct.setQuantity(4);
newProduct = productRepository.save(newProduct);
System.out.println("New product's id : " + newProduct.getId());
System.out.println("====================================================");
}
/**
* Updates a record in the database table using Spring Data JPA in Spring Boot.
*/
private void updateEntity() {
System.out.println("\n***** Let's update a record *****\n");
Optional<Product> product = productRepository.findById(3);
if (product.isPresent()) {
Product productToBeUpdated = product.get();
productToBeUpdated.setQuantity(productToBeUpdated.getQuantity() + 1);
productToBeUpdated = productRepository.save(productToBeUpdated);
System.out.println("Product Id : " + productToBeUpdated.getId());
System.out.println("Product Name : " + productToBeUpdated.getName());
System.out.println("Product Price : " + productToBeUpdated.getPrice());
System.out.println("Product Quantity : " + productToBeUpdated.getQuantity());
System.out.println("====================================================");
}
}
/**
* Deletes a record.
*/
private void deleteEntity() {
System.out.println("\n***** Let's delete a record *****");
productRepository.deleteById(2);
Optional<Product> product = productRepository.findById(3);
if (product.isPresent()) {
System.out.println("Record has not been deleted.");
} else {
System.out.println("Record has been deleted successfully.");
}
System.out.println("====================================================");
}
}