-by RAGHU SIR [NARESH IT, HYDERABAD, P: 040-2374 6666/ 9000994007 /08]
Spring Boot CURD Thymeleaf
Web + Data JPA + MySQL + Lombok
[Link]:-
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version><!--$NO-MVN-MAN-VER$-->
</dependency>
<dependency>
<groupId>[Link]</groupId>
<artifactId>lombok</artifactId>
</dependency>
[Link]:-
[Link]=9898
[Link]-class-name=[Link]
[Link]=jdbc:mysql://localhost:3306/boot
[Link]=root
[Link]=root
[Link]=[Link].MySQL55Dialect
[Link]-auto=create
[Link]-sql=true
1|Page
-by RAGHU SIR [NARESH IT, HYDERABAD, P: 040-2374 6666/ 9000994007 /08]
[Link]:-
<html xmlns:th="[Link]
<body>
<h3> WELCOME TO PRODUCT REGISTER PAGE</h3>
<form th:action="@{/product/save}" method="POST" th:object="${product}">
<pre>
CODE : <input type="text" th:field="*{prodCode}"/>
COST : <input type="text" th:field="*{prodCost}"/>
<input type="submit" value="Create"/>
</pre>
</form>
<span th:text="${message}"></span>
</body>
</html>
[Link]:-
<!DOCTYPE html>
<html xmlns:th="[Link]
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h3>WELCOME TO PRODUCTS DATA PAGE</h3>
<table border="1">
<tr>
<th>ID</th>
<th>CODE</th>
<th>COST</th>
<th>GST</th>
<th>DISCOUNT</th>
<th colspan="3">OPERATION</th>
</tr>
<tr th:each="ob:${list}">
<td th:text="${[Link]}"> </td>
<td th:text="${[Link]}"> </td>
<td th:text="${[Link]}"> </td>
<td th:text="${[Link]}"> </td>
<td th:text="${[Link]}"> </td>
<td>
<a th:href="@{/product/delete/{id}(id=${[Link]})}">DELETE</a>
</td>
<td>
2|Page
-by RAGHU SIR [NARESH IT, HYDERABAD, P: 040-2374 6666/ 9000994007 /08]
<a th:href="@{/product/view/{id}(id=${[Link]})}">VIEW</a>
</td>
<td>
<a th:href="@{/product/edit/{id}(id=${[Link]})}">EDIT</a>
</td>
</tr>
</table>
<span th:text="${message}"></span>
</body>
</html>
[Link]:-
<!DOCTYPE html>
<html xmlns:th="[Link]
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<table border="1">
<tr> <th>ID</th> <td th:text="${[Link]}"> </td> </tr>
<tr> <th>CODE</th> <td th:text="${[Link]}"> </td> </tr>
<tr> <th>COST</th> <td th:text="${[Link]}"> </td> </tr>
<tr> <th>GST</th> <td th:text="${[Link]}"> </td> </tr>
<tr> <th>DISCOUNT</th> <td th:text="${[Link]}"> </td> </tr>
</table>
</body>
</html>
[Link]:-
<html xmlns:th="[Link]
<body>
<h3> WELCOME TO PRODUCT EDIT PAGE</h3>
<form th:action="@{/product/update}" method="POST" th:object="${product}">
<pre>
ID : <input type="text" th:field="*{prodId}" readonly="readonly">
3|Page
-by RAGHU SIR [NARESH IT, HYDERABAD, P: 040-2374 6666/ 9000994007 /08]
CODE : <input type="text" th:field="*{prodCode}"/>
COST : <input type="text" th:field="*{prodCost}"/>
<input type="submit" value="Update"/>
</pre>
</form>
</body>
</html>
Model class:-
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@Data
@Entity
public class Product {
@Id
@GeneratedValue
private Integer prodId;
private String prodCode;
private Double prodCost;
private Double gst;
private Double discount;
Repository Interface:-
package [Link];
import [Link];
import [Link];
public interface ProductRepository
extends JpaRepository<Product, Integer>
{
4|Page
-by RAGHU SIR [NARESH IT, HYDERABAD, P: 040-2374 6666/ 9000994007 /08]
IService:-
package [Link];
import [Link];
import [Link];
import [Link];
public interface IProductService {
List<Product> getAllProducts();
void deleteProduct(Integer id);
Optional<Product> getOneproduct(Integer id);
Integer saveProduct(Product p);
ServiceImpl:-
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@Service
public class ProductServiceImpl
implements IProductService
{
@Autowired
private ProductRepository repo; //HAS-A
@Override
public List<Product> getAllProducts() {
List<Product> list=[Link]();
return list;
}
5|Page
-by RAGHU SIR [NARESH IT, HYDERABAD, P: 040-2374 6666/ 9000994007 /08]
@Override
public void deleteProduct(Integer id) {
[Link](id);
}
@Override
public Optional<Product> getOneproduct(Integer id) {
Optional<Product> opt=[Link](id);
return opt;
}
@Override
public Integer saveProduct(Product p) {
double gst=[Link]() * 8/100.0;
double discount=[Link]() * 12/100.0;
[Link](gst);
[Link](discount);
Integer id=[Link](p).getProdId();
return id;
}
}
Controller:-
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@Controller
@RequestMapping("/product")
public class ProductController {
@Autowired
private IProductService service;
6|Page
-by RAGHU SIR [NARESH IT, HYDERABAD, P: 040-2374 6666/ 9000994007 /08]
/**
* URL : /all , GET
* showProducts() send data to UI
* Page : ProductsData
*/
@RequestMapping("/all")
public String showProducts(Model model) {
List<Product> list=[Link]();
[Link]("list", list);
return "ProductsData";
}
/**
* on click DELETE hyperLink this
* method is called.
* as: /delete/{id} , GET
* deleteProduct
*/
@RequestMapping("/delete/{id}")
public String deleteProduct(
@PathVariable("id")Integer id,
Model model
)
{
//deleting row
[Link](id);
//fetching new data
List<Product> list=[Link]();
//send to ui
[Link]("list", list);
[Link]("message", id+" deleted");
return "ProductsData";
//return "redirect:../all";
}
/**
* On click View HyperLink display
* one row data at ProductOne HTML
* Page, URL : /view/{id}
* method: getOneProduct
*/
7|Page
-by RAGHU SIR [NARESH IT, HYDERABAD, P: 040-2374 6666/ 9000994007 /08]
@RequestMapping("/view/{id}")
public String getOneProduct(
@PathVariable("id")Integer id,
Model model
)
{
Optional<Product> opt=[Link](id);
if([Link]()) {
[Link]("ob", [Link]());
}else {
[Link]("message", "NO DATA FOUND");
}
return "ProductOne";
}
/**
* This method is used to display
* Form with empty inputs
* Page: [Link]
* URL : /reg, Type:GET
* method: showRegPage()
* Form Backing Obj: product
*/
@RequestMapping("/reg")
public String showRegPage(Model model) {
[Link]("product", new Product());
return "ProductReg";
}
/**
* On click Form submit
* read ModelAttribute
* call service layer save method
* return ProduReg with success
* message.
* URL : /save, type:POST
* method: saveProduct()
*/
@RequestMapping(value = "/save"
,method = [Link])
public String saveProduct(
8|Page
-by RAGHU SIR [NARESH IT, HYDERABAD, P: 040-2374 6666/ 9000994007 /08]
@ModelAttribute Product product,
Model model)
{
Integer id=[Link](product);
String message="Product saved with :"+id;
[Link]("message", message);
//clear Form
[Link]("product", new Product());
return "ProductReg";
}
/***
* On click Edit HyperLink
* load object data from DB
* and display at UI using Model
* URL : /edit/{id} , Type:GET
* showEditPage() : [Link]
*/
@RequestMapping("/edit/{id}")
public String showEditPage(
@PathVariable("id")Integer pid,
Model model)
{
Optional<Product> ob=[Link](pid);
if([Link]()) {
[Link]("product", [Link]());
}else {
[Link]("product", new Product());
}
return "ProductEdit";
}
/**
* On click Update button,
* Read Form Data using @ModelAttribute
* call service layer save()
* Return to UI : [Link]
* updateProduct() , URL /update, POST
* Also fetch new data
*/
@RequestMapping(value = "/update",
method = [Link])
public String updateProduct(
@ModelAttribute Product product,
9|Page
-by RAGHU SIR [NARESH IT, HYDERABAD, P: 040-2374 6666/ 9000994007 /08]
Model model )
{
Integer id=[Link](product);
[Link]("message", "Product updated:"+id);
//fetch new data from DB
List<Product> list=[Link]();
[Link]("list", list);
return "ProductsData";
}
FB: [Link]
10 | P a g e