Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- **Spring-servlet.xml**
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-4.2.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">
- <!-- enable component scan -->
- <context:component-scan base-package="com.spring.mvc"/>
- <mvc:annotation-driven/>
- <!-- handler mapping configuration -->
- <bean id="handlerMapping
- class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
- <!-- resolver configuration -->
- <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="prefix" value="/"/>
- <property name="suffix" value=".jsp"/>
- </bean>
- <bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
- <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
- <property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"/>
- <property name="username" value="system"/>
- <property name="password" value="tiger"/>
- </bean>
- <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
- <property name="dataSource" ref="ds"></property>
- <property name="annotatedClasses">
- <list>
- <value>com.spring.mvc.model</value>
- </list>
- </property>
- <property name="hibernateProperties">
- <props>
- <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
- <prop key="hibernate.show_sql">true</prop>
- <prop key="hibernate.hbm2ddl.auto">update</prop>
- </props>
- </property>
- </bean>
- <bean id="ht" class="org.springframework.orm.hibernate3.HibernateTemplate">
- <property name="sessionFactory" ref="sessionFactory"/>
- </bean>
- </beans>
- **Controlleer**
- package com.spring.mvc.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.ModelMap;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.bind.annotation.RequestParam;
- import org.springframework.web.servlet.ModelAndView;
- import com.spring.mvc.dao.EmpDao;
- import com.spring.mvc.model.Employee;
- @Controller
- public class EmployeeController
- {
- @Autowired
- private EmpDao employeeDao;
- @RequestMapping(value="/save",method=RequestMethod.POST)
- public ModelAndView saveEmployee(HttpServletRequest request)
- {
- Employee employee=new Employee();
- employee.setEmpId(Integer.parseInt(request.getParameter("empid")));
- employee.setEmpName(request.getParameter("empname"));
- employee.setSalary(Long.parseLong(request.getParameter("empsalary")));
- employee.setEmpAddress(request.getParameter("empaddress"));
- employee.setEmpAge(Integer.parseInt(request.getParameter("empage")));
- employeeDao.addEmployee(employee);
- return new ModelAndView("index");
- }
- @RequestMapping(value="/employees",method=RequestMethod.GET)
- public ModelAndView listEmployees(ModelMap map)
- {
- List<Employee> list=employeeDao.listEmployeess();
- map.addAttribute("employees",list);
- return new ModelAndView("employeesList");
- }
- @RequestMapping(value="/add",method=RequestMethod.GET)
- public ModelAndView addEmployee()
- {
- return new ModelAndView("addEmployee");
- }
- @RequestMapping(value="/delete",method=RequestMethod.GET)
- public ModelAndView deleteEmployee(@RequestParam("id")int empid,ModelMap map)
- {
- Employee employee=new Employee();
- employee.setEmpId(empid);
- employeeDao.deleteEmployee(employee);
- List<Employee> list=employeeDao.listEmployeess();
- map.addAttribute("employees",list);
- return new ModelAndView("employeesList");
- }
- @RequestMapping(value="/edit",method=RequestMethod.GET)
- public ModelAndView editEmployee(@RequestParam("id")int empid)
- {
- return new ModelAndView("editEmployee","eid",empid);
- }
- }
- **dao classes**
- package com.spring.mvc.dao;
- import java.util.List;
- import com.spring.mvc.model.Employee;
- public interface EmpDao
- {
- public void addEmployee(Employee employee);
- public List<Employee> listEmployeess();
- public void deleteEmployee(Employee employee);
- }
- **EmpDaoImpl**
- package com.spring.mvc.dao;
- import java.util.List;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.orm.hibernate3.HibernateTemplate;
- import org.springframework.stereotype.Repository;
- import com.spring.mvc.model.Employee;
- @Repository("employeeDao")
- public class EmpDaoImpl implements EmpDao
- {
- @Autowired
- public HibernateTemplate ht;
- public void addEmployee(Employee employee)
- {
- ht.saveOrUpdate(employee);
- }
- public List<Employee> listEmployeess()
- {
- List<Employee> list=ht.loadAll(Employee.class);
- return list;
- }
- public void deleteEmployee(Employee employee)
- {
- ht.delete(employee);
- }
- }
- **Pojo class**
- package com.spring.mvc.model;
- import java.io.Serializable;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.GeneratedValue;
- import javax.persistence.GenerationType;
- import javax.persistence.Id;
- import javax.persistence.Table;
- @Entity
- @Table(name="Employee")
- public class Employee implements Serializable
- {
- @Id
- @GeneratedValue(strategy=GenerationType.AUTO)
- @Column(name="empid")
- private Integer empId;
- @Column(name="empname",length=10)
- private String empName;
- @Column(name="empaddress",length=10)
- private String empAddress;
- @Column(name="salary")
- private Long salary;
- @Column(name="empage")
- private Integer empAge;
- public Integer getEmpId() {
- return empId;
- }
- public void setEmpId(Integer empId) {
- this.empId = empId;
- }
- public String getEmpName() {
- return empName;
- }
- public void setEmpName(String empName) {
- this.empName = empName;
- }
- public String getEmpAddress() {
- return empAddress;
- }
- public void setEmpAddress(String empAddress) {
- this.empAddress = empAddress;
- }
- public Long getSalary() {
- return salary;
- }
- public void setSalary(Long salary) {
- this.salary = salary;
- }
- public Integer getEmpAge() {
- return empAge;
- }
- public void setEmpAge(Integer empAge) {
- this.empAge = empAge;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement