Recent News

Part 3 : Spring Maven Crud Application On Product Data (Controller, Dao, Model files)

 Hello Readers, 

I am going to share part 3 of my interview question which is CRUD application ask in practical round so here I am sharing the complete code here.

hope you will enjoy it !


         INCLUDED CONTROLLER, DAO, MODEL FILES 




Project Name : meavenCrudSpring

this file is controller

package name :  meavenCrudSpring.Controller

File 1 :  mainController (location: D:\Spring_Projects\meavenCrudSpring\src\main\java\meavenCrudSpring\Controller\)

CODE : 

package meavenCrudSpring.Controller;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
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.view.RedirectView;

import meavenCrudSpring.Dao.productDao;
import meavenCrudSpring.Model.Product;

@Controller
public class mainController {

@Autowired
private productDao productdao;
@RequestMapping("/")
public String home(Model mo) {
List<Product> products = productdao.getProducts();
mo.addAttribute("products", products);
return "index";
}
// show add product form
@RequestMapping("/add-product")
public String addProduct(Model m) {
m.addAttribute("title", "Add Product");
return "add_product_form";
}
//handle product form
@RequestMapping(value="/handle-product", method= RequestMethod.POST)
public RedirectView handleProduct(@ModelAttribute Product product, HttpServletRequest request) {
System.out.println(product);
productdao.createProduct(product);
RedirectView redirectview = new RedirectView();
redirectview.setUrl(request.getContextPath()+"/");
return redirectview;
}
// handler for delete
@RequestMapping("/delete/{productId}")
public RedirectView deleteProduct(@PathVariable("productId") int productId, HttpServletRequest request) {
this.productdao.deletProduct(productId);
RedirectView redirectview = new RedirectView();
redirectview.setUrl(request.getContextPath()+"/");
return redirectview;
}
// handler for update
@RequestMapping("upadte/{productId}")
public String updateForm(@PathVariable ("productId") int pid, Model model) {
Product product = this.productdao.getProduct(pid);
model.addAttribute("product", product);
return "update_form";
}

}

-----------------------------------------------------------------------------------------------------------------------------

this file is dao

package name : meavenCrudSpring.Dao

File 2 :  mainDao (location: D:\Spring_Projects\meavenCrudSpring\src\main\java\meavenCrudSpring\Dao\)

CODE : 

package meavenCrudSpring.Dao;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.orm.hibernate5.HibernateTemplate;

import org.springframework.stereotype.Component;

import org.springframework.transaction.annotation.Transactional;

import meavenCrudSpring.Model.Product;


@Component

public class productDao {


@Autowired

private HibernateTemplate hibernateTemplate;

// create product

@Transactional

public void createProduct(Product product) {

this.hibernateTemplate.saveOrUpdate(product);

}

// get the single product

public Product getProduct(int pid) {

return this.hibernateTemplate.get(Product.class, pid);

}

// get all products

public List<Product> getProducts(){

List<Product> products = this.hibernateTemplate.loadAll(Product.class);

return products;

}

// delete single product

@Transactional

public void deletProduct(int pid) {

Product p = this.hibernateTemplate.load(Product.class, pid);

this.hibernateTemplate.delete(p);

}

}

-----------------------------------------------------------------------------------------------------------------------------


this file is model(database)

package name : meavenCrudSpring.Model

File 3 :  Product (location: D:\Spring_Projects\meavenCrudSpring\src\main\java\meavenCrudSpring\Model\)

CODE : 

package meavenCrudSpring.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.AUTO)

private int id;

private String name;

private String description;

private long price;

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getDescription() {

return description;

}

public void setDescription(String description) {

this.description = description;

}

public long getPrice() {

return price;

}

public void setPrice(long price) {

this.price = price;

}

@Override

public String toString() {

return "Product [id=" + id + ", name=" + name + ", description=" + description + ", price=" + price + "]";

}

public Product(int id, String name, String description, long price) {

super();

this.id = id;

this.name = name;

this.description = description;

this.price = price;

}

public Product() {

super();

}

}


Thanks for Reading !!

SHARE
  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment