Project Name : springMvcHibernate2
File 1 : RedirectController.java
CODE :
package com.springMvc.Controller;
import java.util.*;
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 com.springMvc.Model.Bank;
import com.springMvc.Service.RedirectService;
@Controller
public class RedirectController {
@Autowired
RedirectService redirectservice;
@RequestMapping("/")
public String showForm1() {
return "bank-form";
}
// insert
@RequestMapping(value="/insert", method=RequestMethod.POST)
public String addData(@ModelAttribute("bank") Bank bank) {
redirectservice.addData(bank);
return "redirect:/display";
}
// display
@RequestMapping(value="/display", method = RequestMethod.GET)
public String displayData(Model model) {
List<Bank> listBank = redirectservice.displayData();
model.addAttribute("listBank", listBank);
return "bank-list";
}
// delete
@RequestMapping(value="/delete/{bid}", method = RequestMethod.GET)
public String deleteData(@PathVariable("bid") int bid) {
redirectservice.deleteData(bid);
return "redirect:/display";
}
// get Data
@RequestMapping(value="/edit/{bid}", method = RequestMethod.GET)
public String getData(@PathVariable("bid") int bid, Model model) {
Bank bank = redirectservice.getData(bid);
model.addAttribute("bank", bank);
return "bank-form";
}
// update
@RequestMapping(value="/update", method=RequestMethod.GET)
public String updateData(@ModelAttribute("bank") Bank bank) {
redirectservice.updateData(bank);
return "redirect:/display";
}
}
File 2 : RedirectService.java
CODE :
package com.springMvc.Service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.springMvc.Dao.RedirectDao;
import com.springMvc.Model.Bank;
@Service
public class RedirectService {
@Autowired
RedirectDao redirectdao;
@Transactional
public void addData(Bank bank) {
redirectdao.addData(bank);
}
@Transactional
public List<Bank> displayData(){
return redirectdao.displayData();
}
@Transactional
public void deleteData(int bid) {
redirectdao.deleteData(bid);
}
@Transactional
public Bank getData(int bid) {
return redirectdao.getData(bid);
}
@Transactional
public void updateData(Bank bank) {
redirectdao.updateData(bank);
}
}
File 3 : RedirectDao.java
CODE :
package com.springMvc.Dao;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.springMvc.Model.Bank;
@Component
public class RedirectDao {
@Autowired
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public Bank addData(Bank bank) {
Session session = this.sessionFactory.getCurrentSession();
session.persist(bank);
return bank;
}
public List<Bank> displayData(){
Session session = this.sessionFactory.getCurrentSession();
List<Bank> listBank = session.createQuery("from Bank").list();
return listBank;
}
public void deleteData(int bid) {
Session session = sessionFactory.getCurrentSession();
Bank bank = session.get(Bank.class, new Integer(bid));
if(null!=bank) {
session.delete(bank);
}
}
public Bank getData(int bid) {
Session session = sessionFactory.getCurrentSession();
Bank bank = session.get(Bank.class, new Integer(bid));
return bank;
}
public void updateData(Bank bank) {
Session session = this.sessionFactory.getCurrentSession();
session.update(bank);
}
}
File 4 : Bank.java
CODE :
package com.springMvc.Model;
import javax.persistence.*;
@Entity
@Table(name = "tblbankcrud")
public class Bank {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "bid")
private int bid;
@Column(name = "name")
private String name;
@Column(name = "designation")
private String designation;
@Column(name = "phone")
private String phone;
@Column(name = "des")
private String des;
@Column(name = "bday")
private String bday;
@Column(name = "city")
private String city;
@Column(name = "hobby")
private String hobby;
@Column(name = "gender")
private String gender;
@Override
public String toString() {
return "Bank [bid=" + bid + ", name=" + name + ", designation=" + designation + ", phone=" + phone + ", des="
+ des + ", bday=" + bday + ", city=" + city + ", hobby=" + hobby + ", gender=" + gender + "]";
}
public int getBid() {
return bid;
}
public void setBid(int bid) {
this.bid = bid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getDes() {
return des;
}
public void setDes(String des) {
this.des = des;
}
public String getBday() {
return bday;
}
public void setBday(String bday) {
this.bday = bday;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Bank(int bid, String name, String designation, String phone, String des, String bday, String city,
String hobby, String gender) {
super();
this.bid = bid;
this.name = name;
this.designation = designation;
this.phone = phone;
this.des = des;
this.bday = bday;
this.city = city;
this.hobby = hobby;
this.gender = gender;
}
public Bank() {
super();
}
}
0 comments:
Post a Comment