package com.crud.demoapp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class CrudappApplication {
public static void main(String[] args) {
SpringApplication.run(CrudappApplication.class, args);
}
}
package com.crud.demoapp.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.crud.demoapp.model.Product;
import com.crud.demoapp.service.ProductService;
@Controller
public class AppController {
@Autowired
private ProductService service;
@RequestMapping(“/”)
public String viewHomePage(Model model) {
List<Product> listProducts = service.listAll();
model.addAttribute(“listProducts”, listProducts);
return “index”;
}
@RequestMapping(“/new”)
public String showNewProductForm(Model model) {
Product product = new Product();
model.addAttribute(“product”, product);
return “new_product”;}
@RequestMapping(value = “/save”, method = RequestMethod.POST)
public String saveProduct(@ModelAttribute(“product”)Product product) {
service.save(product);
return “redirect:/”;
}
@RequestMapping(“/edit/{id}”)
public ModelAndView showEditProductForm(@PathVariable(name = “id”)Long id) {
ModelAndView mav = new ModelAndView(“edit_product”);
Product product = service.get(id);
mav.addObject(“product”, product);
return mav;
}
@RequestMapping(“/delete/{id}”)
public String deleteProduct(@PathVariable(name = “id”)Long id) {
service.delet(id);
return “redirect:/”;
}}
package com.crud.demoapp.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String brand;
private String madein;
private Float price;
public Product() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getMadein() {
return madein;
}
public void setMadein(String madein) {
this.madein = madein;
}
public Float getPrice() {
return price;
}
public void setPrice(Float price) {
this.price = price;
}}
package com.crud.demoapp.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import com.crud.demoapp.model.Product;
public interface ProductRepository extends JpaRepository<Product, Long>{
}
package com.crud.demoapp.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.crud.demoapp.model.Product;
import com.crud.demoapp.repository.ProductRepository;
@Service
public class ProductService {
@Autowired
private ProductRepository repo;
public List<Product> listAll(){
return repo.findAll();
}
public void save(Product product) {
repo.save(product);
}
public Product get(Long id) {
return repo.findById(id).get();
}
public void delet(Long id) {
repo.deleteById(id);
}}
<meta charset=“ISO-8859-1”>
<title>Edit Product</title>
</head>
<body>
<div align=“center”>
<h1>Edit Product</h1>
<br/>
<form action=“#” th:action=“@{/save}” th:object=“${product}” method=“post”>
<table border=“0” cellpadding=“10”>
<tr>
<td>Product ID:</td>
<td><input type=“text” th:field=“*{id}” readonly=“readonly” /></td>
</tr>
<tr>
<td>Product Name:</td>
<td><input type=“text” th:field=“*{name}” /></td>
</tr>
<tr>
<td>Product Brand:</td>
<td><input type=“text” th:field=“*{brand}” /></td>
</tr>
<tr>
<td>Made In:</td>
<td><input type=“text” th:field=“*{madein}” /></td>
</tr>
<tr>
<td>Product Price:</td>
<td><input type=“text” th:field=“*{price}” /></td>
</tr>
<tr>
<td colspan=“2”><button type=“submit”>Save</button></td>
</tr>
</table>
</form>
</div>
</body>
</html>
<!DOCTYPE html>
<html xmlns:th=“http://www.thymeleaf.org”>
<head>
<meta charset=“ISO-8859-1”>
<title>Product Services</title>
</head>
<body>
<div align=“center”>
<h1>Product Services</h1>
<a href=“new”>Create New Products</a><br/>
<table border=“1” cellpadding=“10”>
<thead>
<tr>
<th>Product Id</th>
<th>Name</th>
<th>Brand</th>
<th>Made In</th>
<th>Price</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<tr th:each=“product : ${listProducts}”>
<td th:text=“${product.id}”>Product ID</td>
<td th:text=“${product.name}”>Name</td>
<td th:text=“${product.brand}”>Brand</td>
<td th:text=“${product.madein}”>Made In</td>
<td th:text=“${product.price}”>Price</td>
<td>
<a th:href=“@{‘/edit/’ + ${product.id}}”>Edit</a>
<a th:href=“@{‘/delete/’ + ${product.id}}”>Delete</a>
</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
<!DOCTYPE html>
<html xmlns:th=“http://www.thymeleaf.org”>
<head>
<meta charset=“ISO-8859-1”>
<title>Create New Product</title>
</head>
<body>
<div align=“center”>
<h1>Create New Product</h1>
<br/>
<form action=“#” th:action=“@{/save}” th:object=“${product}” method=“post”>
<table border=“0” cellpadding=“10”>
<tr>
<td>Product Name:</td>
<td><input type=“text” th:field=“*{name}” /></td>
</tr>
<tr>
<td>Product Brand:</td>
<td><input type=“text” th:field=“*{brand}” /></td>
</tr>
<tr>
<td>Made In:</td>
<td><input type=“text” th:field=“*{madein}” /></td>
</tr>
<tr>
<td>Product Price:</td>
<td><input type=“text” th:field=“*{price}” /></td>
</tr>
<tr>
<td colspan=“2”><button type=“submit”>Save</button></td>
</tr>
</table>
</form>
</div>
</body>
</html>
#configuration for mysql
spring.jpa.hibernate.ddl-auto=none
spring.datasource.url=jdbc:mysql://localhost:3306/userdb
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
logging.level.root=