Create Spring3MVC Hibernate3 Example using Maven in Eclipse

CREATE TABLE CONTACTS
(
     id           INT PRIMARY KEY AUTO_INCREMENT,
     firstname    VARCHAR (30),
     lastname    VARCHAR (30),
     telephone   VARCHAR (15),
     email       VARCHAR (30),
     created     TIMESTAMP DEFAULT NOW()
);

File: src/main/java/com/dufeng/contact/form/Contact.java
package com.dufeng .contact.form;
 
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
 
@Entity
@Table (name= "CONTACTS" )
public class Contact {
     
     @Id
     @Column (name= "ID" )
     @GeneratedValue
     private Integer id;
     
     @Column (name= "FIRSTNAME" )
     private String firstname;
 
     @Column (name= "LASTNAME" )
     private String lastname;
 
     @Column (name= "EMAIL" )
     private String email;
     
     @Column (name= "TELEPHONE" )
     private String telephone;
     
     
     public String getEmail() {
         return email;
     }
     public String getTelephone() {
         return telephone;
     }
     public void setEmail(String email) {
         this .email = email;
     }
     public void setTelephone(String telephone) {
         this .telephone = telephone;
     }
     public String getFirstname() {
         return firstname;
     }
     public String getLastname() {
         return lastname;
     }
     public void setFirstname(String firstname) {
         this .firstname = firstname;
     }
     public void setLastname(String lastname) {
         this .lastname = lastname;
     }
     public Integer getId() {
         return id;
     }
     public void setId(Integer id) {
         this .id = id;
     }
}

File: src/main/java/com/dufeng/contact/dao/ContactDAO.java

package com.dufeng .contact.dao;
 
import java.util.List;
 
import net.viralpatel.contact.form.Contact;
 
public interface ContactDAO {
     
     public void addContact(Contact contact);
     public List<Contact> listContact();
     public void removeContact(Integer id);
}
File: src/main/java/com/dufeng/contact/dao/ContactDAOImpl.java
package com.dufeng .contact.dao;
 
import java.util.List;
 
import com.dufeng .contact.form.Contact;
 
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
 
@Repository
public class ContactDAOImpl implements ContactDAO {
 
     @Autowired
     private SessionFactory sessionFactory;
 
     public void addContact(Contact contact) {
         sessionFactory.getCurrentSession().save(contact);
     }
 
     public List<Contact> listContact() {
 
         return sessionFactory.getCurrentSession().createQuery( "from Contact" )
                 .list();
     }
 
     public void removeContact(Integer id) {
         Contact contact = (Contact) sessionFactory.getCurrentSession().load(
                 Contact. class , id);
         if ( null != contact) {
             sessionFactory.getCurrentSession().delete(contact);
         }
 
     }
}

File: src/main/java/com/dufeng/contact/service/ContactService.java
package com.dufeng .contact.service;
 
import java.util.List;
 
import net.viralpatel.contact.form.Contact;
 
public interface ContactService {
     
     public void addContact(Contact contact);
     public List<Contact> listContact();
     public void removeContact(Integer id);
}
File: src/main/java/com/dufeng/contact/service/ContactServiceImpl.java
package com.dufeng .contact.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.dufeng .contact.dao.ContactDAO;
import com.dufeng .contact.form.Contact;
 
@Service
public class ContactServiceImpl implements ContactService {
 
     @Autowired
     private ContactDAO contactDAO;
     
     @Transactional
     public void addContact(Contact contact) {
         contactDAO.addContact(contact);
     }
 
     @Transactional
     public List<Contact> listContact() {
 
         return contactDAO.listContact();
     }
 
     @Transactional
     public void removeContact(Integer id) {
         contactDAO.removeContact(id);
     }
}
File: /src/webapp/WEB-INF/web.xml
<? xml version = "1.0" encoding = "UTF-8" ?>
< web-app xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
     xmlns = "http://java.sun.com/xml/ns/javaee"
     xmlns:web = "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     id = "WebApp_ID" version = "2.5" >
     < display-name >Spring3-Hibernate</ display-name >
    
     < servlet >
         < servlet-name >spring</ servlet-name >
         < servlet-class >
             org.springframework.web.servlet.DispatcherServlet
         </ servlet-class >
         < load-on-startup >1</ load-on-startup >
     </ servlet >
     < servlet-mapping >
         < servlet-name >spring</ servlet-name >
         < url-pattern >/</ url-pattern >
     </ servlet-mapping >
</ web-app >
File: /src/main/webapp/WEB-INF/jdbc.properties
jdbc.driverClassName= com.mysql.jdbc.Driver
jdbc.dialect=org.hibernate.dialect.MySQLDialect
jdbc.databaseurl=jdbc:mysql://localhost:3306/demo
jdbc.username=root
jdbc.password=root
File: /src/main/webapp/WEB-INF/spring-servlet.xml
<? xml  version = "1.0" encoding = "UTF-8" ?>
< beans xmlns = "http://www.springframework.org/schema/beans"
     xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
     xmlns:aop = "http://www.springframework.org/schema/aop"
     xmlns:context = "http://www.springframework.org/schema/context"
     xmlns:jee = "http://www.springframework.org/schema/jee"
     xmlns:lang = "http://www.springframework.org/schema/lang"
     xmlns:p = "http://www.springframework.org/schema/p"
     xmlns:tx = "http://www.springframework.org/schema/tx"
     xmlns:util = "http://www.springframework.org/schema/util"
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
         http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
 
     < context:annotation-config />
     < context:component-scan base-package = "com.dufeng.contact" />
 
     < bean id = "jspViewResolver"
         class = "org.springframework.web.servlet.view.InternalResourceViewResolver" >
         < property name = "viewClass"
             value = "org.springframework.web.servlet.view.JstlView" />
         < property name = "prefix" value = "/WEB-INF/jsp/" />
         < property name = "suffix" value = ".jsp" />
     </ bean >
 
     < bean id = "messageSource"
         class = "org.springframework.context.support.ReloadableResourceBundleMessageSource" >
         < property name = "basename" value = "classpath:messages" />
         < property name = "defaultEncoding" value = "UTF-8" />
     </ bean >
     < bean id = "propertyConfigurer"
         class = "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
         p:location = "/WEB-INF/jdbc.properties" />
 
     < bean id = "dataSource"
         class = "org.apache.commons.dbcp.BasicDataSource" destroy-method = "close"
         p:driverClassName = "${jdbc.driverClassName}"
         p:url = "${jdbc.databaseurl}" p:username = "${jdbc.username}"
         p:password = "${jdbc.password}" />
 
 
     < bean id = "sessionFactory"
         class = "org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
         < property name = "dataSource" ref = "dataSource" />
         < property name = "configLocation" >
             < value >classpath:hibernate.cfg.xml</ value >
         </ property >
         < property name = "configurationClass" >
             < value >org.hibernate.cfg.AnnotationConfiguration</ value >
         </ property >
         < property name = "hibernateProperties" >
             < props >
                 < prop key = "hibernate.dialect" >${jdbc.dialect}</ prop >
                 < prop key = "hibernate.show_sql" >true</ prop >
             </ props >
         </ property >
     </ bean >
 
     < tx:annotation-driven />
     < bean id = "transactionManager"
         class = "org.springframework.orm.hibernate3.HibernateTransactionManager" >
         < property name = "sessionFactory" ref = "sessionFactory" />
     </ bean >
</ beans >
File: /src/main/resources/hibernate.cfg.xml
<? xml version = '1.0' encoding = 'utf-8' ?>
<!DOCTYPE hibernate-configuration PUBLIC
     "-//Hibernate/Hibernate Configuration DTD//EN"
     "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
 
< hibernate-configuration >
     < session-factory >
         < mapping class = "com.dufeng.contact.form.Contact" />
     </ session-factory >
         
</ hibernate-configuration >
File: /src/main/resources/messages_en.properties
label.firstname=First Name
label.lastname=Last Name
label.email=Email
label.telephone=Telephone
label.addcontact=Add Contact
 
label.menu=Menu
label.title=Contact Manager
label.footer=&copy; ViralPatel.net
File: /src/main/java/com/dufeng/contact/controller/ContactController.java
package com.dufeng .contact.controller;
 
import java.util.Map;
 
import com.dufeng .contact.form.Contact;
import com.dufeng .contact.service.ContactService;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
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;
 
@Controller
public class ContactController {
 
     @Autowired
     private ContactService contactService;
 
     @RequestMapping ( "/index" )
     public String listContacts(Map<String, Object> map) {
 
         map.put( "contact" , new Contact());
         map.put( "contactList" , contactService.listContact());
 
         return "contact" ;
     }
 
     @RequestMapping (value = "/add" , method = RequestMethod.POST)
     public String addContact( @ModelAttribute ( "contact" )
     Contact contact, BindingResult result) {
 
         contactService.addContact(contact);
 
         return "redirect:/index" ;
     }
 
     @RequestMapping ( "/delete/{contactId}" )
     public String deleteContact( @PathVariable ( "contactId" )
     Integer contactId) {
 
         contactService.removeContact(contactId);
 
         return "redirect:/index" ;
     }
}
File: /src/main/webapp/WEB-INF/jsp/contact.jsp
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
< html >
< head >
     < title >Spring 3 MVC Series - Contact Manager | viralpatel.net</ title >
</ head >
< body >
 
< h2 >Contact Manager</ h2 >
 
< form:form method = "post" action = "add.html" commandName = "contact" >
 
     < table >
     < tr >
         < td >< form:label path = "firstname" >< spring:message code = "label.firstname" /></ form:label ></ td >
         < td >< form:input path = "firstname" /></ td >
     </ tr >
     < tr >
         < td >< form:label path = "lastname" >< spring:message code = "label.lastname" /></ form:label ></ td >
         < td >< form:input path = "lastname" /></ td >
     </ tr >
     < tr >
         < td >< form:label path = "email" >< spring:message code = "label.email" /></ form:label ></ td >
         < td >< form:input path = "email" /></ td >
     </ tr >
     < tr >
         < td >< form:label path = "telephone" >< spring:message code = "label.telephone" /></ form:label ></ td >
         < td >< form:input path = "telephone" /></ td >
     </ tr >
     < tr >
         < td colspan = "2" >
             < input type = "submit" value = "<spring:message code=" label.addcontact"/>"/>
         </ td >
     </ tr >
</ table
</ form:form >
 
     
< h3 >Contacts</ h3 >
< c:if  test = "${!empty contactList}" >
< table class = "data" >
< tr >
     < th >Name</ th >
     < th >Email</ th >
     < th >Telephone</ th >
     < th >&nbsp;</ th >
</ tr >
< c:forEach items = "${contactList}" var = "contact" >
     < tr >
         < td >${contact.lastname}, ${contact.firstname} </ td >
         < td >${contact.email}</ td >
         < td >${contact.telephone}</ td >
         < td >< a href = "delete/${contact.id}" >delete</ a ></ td >
     </ tr >
</ c:forEach >
</ table >
</ c:if >
 
</ body >
</ html >

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值