CRUD Example Spring

 package com.rmigb.controller;

 

 import com.util.ArrayList;

 

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

 import org.springframework.stereotype.Controller;

 import org.springframework.ui.Model;

 import org.springframework.validation.BindingResult;

 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.bind.support.SessionStatus;

 

 import com.rmigb.bo.Person;

 import com.rmigb.service.PersonService;

 import java.util.List;

 

 @Controller

 public class PersonController {

     

     @Autowired

     PersonService service;

     

     @RequestMapping(value = "/add"method = RequestMethod.GET)

     public String add(Model model) {

         model.addAttribute("person"new Person());

         return "addperson";

     }

     

     @RequestMapping(value = "/save"method = RequestMethod.POST)

     public String save(@ModelAttribute("person"Person person,

             BindingResult br,  Model model) {

         if (br.hasErrors()) {

             return "addperson";

         }

         if (person.getId() 0) {

             service.updatePerson(person);

         else {

             service.addPerson(person);

         }

         return "redirect:list";

     }

     

     @RequestMapping(value = "/list"method = RequestMethod.GET)

     public String list(Model model) {

         List<Person> personlist = service.listPerson();

         model.addAttribute("personlist"personlist);

         return "listperson";

     }

     

     @RequestMapping(value = "/edit/{id}"method = RequestMethod.GET)

     public String edit(@PathVariable Integer idModel model) {

         model.addAttribute("person"service.getPerson(id));

         return "addperson";

     }

     

     @RequestMapping(value = "/delete/{id}"method = RequestMethod.GET)

     public String delete(@PathVariable Integer id) {

         service.deletePerson(id);

         return "redirect:/list";

     }

 }

 package com.rmigb.bo;

 

 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 = "person")

 public class Person {

 

     @Id

     @GeneratedValue(strategy = GenerationType.IDENTITY)

     @Column(unique = truenullable = false)

     private int id;

     @Column

     String fname;

     @Column

     String lname;

     @Column

     int age;

     @Column

     float salary;

 

     public int getId() {

         return id;

     }

 

     public void setId(int id) {

         this.id = id;

     }

 

     public String getFname() {

         return fname;

     }

 

     public void setFname(String fname) {

         this.fname = fname;

     }

 

     public String getLname() {

         return lname;

     }

 

     public void setLname(String lname) {

         this.lname = lname;

     }

 

     public int getAge() {

         return age;

     }

 

     public void setAge(int age) {

         this.age = age;

     }

 

     public float getSalary() {

         return salary;

     }

 

     public void setSalary(float salary) {

         this.salary = salary;

     }

 

     @Override

     public String toString() {

         return "Id: " id "; fname : " fname "' age : " age "; Salary: " salary;

     }

 }

 package com.rmigb.dao;

 

 import java.util.List;

 import org.springframework.stereotype.Repository;

 import com.rmigb.bo.Person;

 

 public interface Persondao {

   public Person getPerson(int id);

 

   public List<Person> listPersons();

 

   public void deletePerson(int id);

 

   public void addPerson(Person person);

 

   public void updatePerson(Person person);

 

 }

 package com.rmigb.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 org.springframework.stereotype.Repository;

 import com.rmigb.bo.Person;

 

 @Repository

 public class PersondaoImpl implements Persondao {

 

   @Autowired

   private SessionFactory sessionFactory;

 

   public Session getCurrentSession() {

     return sessionFactory.getCurrentSession();

   }

 

   @Override

   public Person getPerson(int id) {

     Person person (PersongetCurrentSession().get(Person.classid);

     return person;

   }

 

   @Override

   public List<Person> listPersons() {

     return getCurrentSession().createQuery("From Person").list();

   }

 

   @Override

   public void deletePerson(int id) {

     Person person getPerson(id);

     getCurrentSession().delete(person);

   }

 

   @Override

   public void addPerson(Person person) {

     getCurrentSession().save(person);

 

   }

 

   @Override

   public void updatePerson(Person person) {

     getCurrentSession().update(person);

 

   }

 }

 package com.rmigb.service;

 

 import java.util.List;

 import com.rmigb.bo.Person;

 

 public interface PersonService {

   public Person getPerson(int id);

 

   public void updatePerson(Person person);

 

   public List<Person> listPerson();

 

   public void deletePerson(int id);

 

   public void addPerson(Person person);

 

 }

 package com.rmigb.service;

 

 import java.util.List;

 

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

 import org.springframework.stereotype.Component;

 import org.springframework.stereotype.Service;

 import org.springframework.transaction.annotation.Transactional;

 

 import com.rmigb.bo.Person;

 import com.rmigb.dao.Persondao;

 

 @Service

 @Transactional

 public class PersonServiceImpl implements PersonService {

 

   @Autowired

   Persondao personDao;

 

   @Override

   public Person getPerson(int id) {

     return personDao.getPerson(id);

   }

 

   @Override

   public void updatePerson(Person person) {

     personDao.updatePerson(person);

   }

 

   @Override

   public List<Person> listPerson() {

     return personDao.listPersons();

   }

 

   @Override

   public void deletePerson(int id) {

     personDao.deletePerson(id);

 

   }

 

   @Override

   public void addPerson(Person person) {

     personDao.addPerson(person);

 

   }

 

 }

 <%taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

 <%taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>

 <%taglib uri="http://www.springframework.org/tags" prefix="s"%>

 <%taglib uri="http://www.springframework.org/tags/form" prefix="form"%>

 <%page session="false"%>

 <html>

     <head>

         <title>Test</title>

     </head>

     <body>

         <a href="${pageContext.request.contextPath}/add">Click here to add new record</a>

         <h1>All Records</h1>

         <div>

             <c:choose>

                 <c:when test="${fn:length(personlist)>0}">

                     <table id="records">

                         <thead>

                             <tr>

                                 <th>Edit</th>

                                 <th>Delete</th>

                                 <th>First Name</th>

                                 <th>Last Name</th>

                                 <th>Age</th>

                                 <th>Salary</th>

                             </tr>

 

 

                             <c:forEach items="${personlist}" var="p">

                                 <tr>

                                     <td><a href="<c:url value="/edit/${p.id}"/>">EDIT</a></td>

                                     <td><a href="<c:url value="/delete/${p.id}"/>">DELETE</a></td>

                                     <td><c:out value="${p.fname}"></c:out></td>

                                     <td><c:out value="${p.lname}"></c:out></td>

                                     <td><c:out value="${p.age}"></c:out></td>

                                     <td><c:out value="${p.salary}"></c:out></td>

                                     </tr>

                             </c:forEach>

                         </thead>

                     </table>

                 </c:when>

                 <c:otherwise>

                     <label>Sorry! No Record To Display</label>

                 </c:otherwise>

             </c:choose>

         </div>

 

 

     </body>

 </html>

 <%taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

 <%taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>

 <%taglib uri="http://www.springframework.org/tags" prefix="s"%>

 <%taglib uri="http://www.springframework.org/tags/form" prefix="form"%>

 <%page session="false"%>

 <html>

 <head>

 <title>Add/Edit Record</title>

 </head>

 <body>

   <form:form commandName="person" action="/save" method="post">

     <table>

       <tr>

         <td><label>First Name : </label></td>

         <td><form:hidden path="id" /> <form:input path="fname" /></td>

       </tr>

       <tr>

         <td><label>Last Name : </label></td>

         <td><form:input path="lname" /></td>

       </tr>

       <tr>

         <td><label>Age: </label></td>

         <td><form:input path="age" /></td>

       </tr>

       <tr>

         <td><label>Salary: </label></td>

         <td><form:input path="salary" /></td>

       </tr>

       <tr>

         <td colspan="2"><input type="submit" value="Save"></input></td>

       </tr>

     </table>

   </form:form>

   

 </body>

 </html>

 <?xml version="1.0" encoding="UTF-8"?>

 <beans xmlns="http://www.springframework.org/schema/beans"

   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

   

   <!-- Root Context: defines shared resources visible to all other web components -->

     

 </beans>

 <?xml version="1.0" encoding="UTF-8"?>

 <beans:beans xmlns="http://www.springframework.org/schema/mvc"

   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"

   xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"

   xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd

     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

     http://www.springframework.org/schema/tx

         http://www.springframework.org/schema/tx/spring-tx.xsd

     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

 

   <annotation-driven />

 

   <resources mapping="/resources/**" location="/resources/" />

   <beans:bean

     class="org.springframework.web.servlet.view.InternalResourceViewResolver">

     <beans:property name="prefix" value="/WEB-INF/views/" />

     <beans:property name="suffix" value=".jsp" />

   </beans:bean>

 

   <context:component-scan base-package="com.rmigb" />

 

   <beans:bean id="dataSource"

     class="org.springframework.jdbc.datasource.DriverManagerDataSource">

     <beans:property name="driverClassName" value="com.mysql.jdbc.Driver" />

     <beans:property name="url" value="jdbc:mysql://localhost:3306/test" />

     <beans:property name="username" value="root" />

     <beans:property name="password" value="" />

   </beans:bean>

 

   <beans:bean id="sessionFactory"

     class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

     <beans:property name="dataSource">

       <beans:ref bean="dataSource" />

     </beans:property>

     <beans:property name="packagesToScan" value="com.rmigb.bo">

     </beans:property>

     <beans:property name="hibernateProperties">

       <beans:props>

         <beans:prop key="hibernate.hbm2ddl.auto">

           update

         </beans:prop>

         <beans:prop key="hibernate.show_sql">

           true

         </beans:prop>

         <beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect

         </beans:prop>

       </beans:props>

     </beans:property>

   </beans:bean>

 

 

   <beans:bean id="transactionManager"

     class="org.springframework.orm.hibernate4.HibernateTransactionManager">

     <beans:property name="sessionFactory">

       <beans:ref bean="sessionFactory" />

     </beans:property>

   </beans:bean>

 

   <tx:annotation-driven transaction-manager="transactionManager" />

 

 </beans:beans>

 <?xml version="1.0" encoding="UTF-8"?>

 <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"

   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

 

   <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->

   <context-param>

     <param-name>contextConfigLocation</param-name>

     <param-value>/WEB-INF/spring/root-context.xml</param-value>

   </context-param>

   

   <!-- Creates the Spring Container shared by all Servlets and Filters -->

   <listener>

     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

   </listener>

 

   <!-- Processes application requests -->

   <servlet>

     <servlet-name>appServlet</servlet-name>

     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

     <init-param>

       <param-name>contextConfigLocation</param-name>

       <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>

     </init-param>

     <load-on-startup>1</load-on-startup>

   </servlet>

     

   <servlet-mapping>

     <servlet-name>appServlet</servlet-name>

     <url-pattern>/</url-pattern>

   </servlet-mapping>

 

 </web-app>

 <?xml version="1.0" encoding="UTF-8"?>

 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

   <modelVersion>4.0.0</modelVersion>

   <groupId>com.mycompany</groupId>

   <artifactId>rohit</artifactId>

   <name>mynetbeansproject</name>

   <packaging>war</packaging>

   <version>1.0.0-BUILD-SNAPSHOT</version>

   <properties>

     <java-version>1.6</java-version>

     <org.springframework-version>3.1.1.RELEASE</org.springframework-version>

     <org.aspectj-version>1.6.10</org.aspectj-version>

     <org.slf4j-version>1.6.6</org.slf4j-version>

   </properties>

   <dependencies>

     <!-- Spring -->

     <dependency>

       <groupId>org.springframework</groupId>

       <artifactId>spring-context</artifactId>

       <version>${org.springframework-version}</version>

       <exclusions>

         <!-- Exclude Commons Logging in favor of SLF4j -->

         <exclusion>

           <groupId>commons-logging</groupId>

           <artifactId>commons-logging</artifactId>

          </exclusion>

       </exclusions>

     </dependency>

     <dependency>

       <groupId>org.springframework</groupId>

       <artifactId>spring-webmvc</artifactId>

       <version>${org.springframework-version}</version>

     </dependency>

         

     <!-- AspectJ -->

     <dependency>

       <groupId>org.aspectj</groupId>

       <artifactId>aspectjrt</artifactId>

       <version>${org.aspectj-version}</version>

     </dependency>  

     

     <!-- Logging -->

     <dependency>

       <groupId>org.slf4j</groupId>

       <artifactId>slf4j-api</artifactId>

       <version>${org.slf4j-version}</version>

     </dependency>

     <dependency>

       <groupId>org.slf4j</groupId>

       <artifactId>jcl-over-slf4j</artifactId>

       <version>${org.slf4j-version}</version>

       <scope>runtime</scope>

     </dependency>

     <dependency>

       <groupId>org.slf4j</groupId>

       <artifactId>slf4j-log4j12</artifactId>

       <version>${org.slf4j-version}</version>

       <scope>runtime</scope>

     </dependency>

     <dependency>

       <groupId>log4j</groupId>

       <artifactId>log4j</artifactId>

       <version>1.2.15</version>

       <exclusions>

         <exclusion>

           <groupId>javax.mail</groupId>

           <artifactId>mail</artifactId>

         </exclusion>

         <exclusion>

           <groupId>javax.jms</groupId>

           <artifactId>jms</artifactId>

         </exclusion>

         <exclusion>

           <groupId>com.sun.jdmk</groupId>

           <artifactId>jmxtools</artifactId>

         </exclusion>

         <exclusion>

           <groupId>com.sun.jmx</groupId>

           <artifactId>jmxri</artifactId>

         </exclusion>

       </exclusions>

       <scope>runtime</scope>

     </dependency>

 

     <!-- @@Inject -->

     <dependency>

       <groupId>javax.inject</groupId>

       <artifactId>javax.inject</artifactId>

       <version>1</version>

     </dependency>

         

     <!-- Servlet -->

     <dependency>

       <groupId>javax.servlet</groupId>

       <artifactId>servlet-api</artifactId>

       <version>2.5</version>

       <scope>provided</scope>

     </dependency>

     <dependency>

       <groupId>javax.servlet.jsp</groupId>

       <artifactId>jsp-api</artifactId>

       <version>2.1</version>

       <scope>provided</scope>

     </dependency>

     <dependency>

       <groupId>javax.servlet</groupId>

       <artifactId>jstl</artifactId>

       <version>1.2</version>

     </dependency>

   

     <!-- Test -->

     <dependency>

       <groupId>junit</groupId>

       <artifactId>junit</artifactId>

       <version>4.7</version>

       <scope>test</scope>

     </dependency>        

   </dependencies>

     <build>

         <plugins>

             <plugin>

                 <artifactId>maven-eclipse-plugin</artifactId>

                 <version>2.9</version>

                 <configuration>

                     <additionalProjectnatures>

                         <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>

                     </additionalProjectnatures>

                     <additionalBuildcommands>

                         <buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>

                     </additionalBuildcommands>

                     <downloadSources>true</downloadSources>

                     <downloadJavadocs>true</downloadJavadocs>

                 </configuration>

             </plugin>

             <plugin>

                 <groupId>org.apache.maven.plugins</groupId>

                 <artifactId>maven-compiler-plugin</artifactId>

                 <version>2.5.1</version>

                 <configuration>

                     <source>1.6</source>

                     <target>1.6</target>

                     <compilerArgument>-Xlint:all</compilerArgument>

                     <showWarnings>true</showWarnings>

                     <showDeprecation>true</showDeprecation>

                 </configuration>

             </plugin>

             <plugin>

                 <groupId>org.codehaus.mojo</groupId>

                 <artifactId>exec-maven-plugin</artifactId>

                 <version>1.2.1</version>

                 <configuration>

                     <mainClass>org.test.int1.Main</mainClass>

                 </configuration>

             </plugin>

         </plugins>

     </build>

 </project>

Comments

VARIABLES INFO

OUTPUT ON CONSOLE

PRESS DOWN ARROW