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 :
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 !!
0 comments:
Post a Comment