Spring 3.2 MVC Hibernate Example

In this section, you will learn to integrate Spring 3.2 MVC with Hibernate.

Spring 3.2 MVC Hibernate Example

Spring 3.2 MVC Hibernate Example

In this section, you will learn to integrate Spring 3.2 MVC with Hibernate.

The hierarchical structure of the application directory is given below :

The jar file used is shown below :

The query for creating database table in Mysql is given below :

CREATE TABLE `article` ( 
	`article_id` bigint(20) NOT NULL AUTO_INCREMENT, 
	`article_name` varchar(65) NOT NULL, 
	`article_desc` text NOT NULL, 
	`date_added` datetime DEFAULT NULL, 
	PRIMARY KEY (`article_id`) 
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1 

APPLICATION FLOW :

When you execute the application, the first page will be :

By clicking on List of Articles, you can see the list of previously added articles. Also, you can add article by clicking on the hyperlink "Add Article" as shown below:

By clicking on Show All Articles, you can see the the list of previously added articles. When you save the above added article, you will forwarded to the below page containing the list of added articles :

CODES

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"
version="2.5">
<servlet>
	<servlet-name>dispatcher</servlet-name>
	<servlet-class>
		org.springframework.web.servlet.DispatcherServlet
	</servlet-class>
	<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
	<servlet-name>dispatcher</servlet-name>
	<url-pattern>*.html</url-pattern>
</servlet-mapping>
<welcome-file-list>
	<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

dispatcher-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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

<context:property-placeholder location="classpath:jdbc.properties" />
<context:component-scan base-package="net.roseindia" />

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

<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/view/" />
<property name="suffix" value=".jsp" />
</bean>

<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${database.driver}" />
<property name="url" value="${database.url}" />
<property name="username" value="${database.user}" />
<property name="password" value="${database.password}" />
</bean>

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>net.roseindia.model.Article</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
</props>
</property>
</bean>

<bean id="hibernateTransactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Spring 3.2 MVC and Hibernate Example</title>
</head>
<body>
<h1>Spring 3.2 MVC and Hibernate Example</h1>

<a href="articles.html">List of Articles</a>
<br />
<a href="articles/add.html">Add Article</a>
</body>
</html>

addArticle.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>

<html>
<head>
<title>Add Article</title>
</head>
<body>
<h1>Add Article</h1>

<c:url var="viewArticlesUrl" value="/articles.html" />
<a href="${viewArticlesUrl}">Show All Articles</a>
<br></br>
<c:url var="saveArticleUrl" value="/articles/save.html" />
<form:form modelAttribute="article" method="POST"
action="${saveArticleUrl}">
<form:label path="articleName">Article Name:</form:label>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<form:input path="articleName" size="46" />
<br></br>
<form:label path="articleDesc">Article Description:</form:label>
<form:textarea path="articleDesc" rows="3" cols="40" />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="submit" value="Save Article" />
</form:form>

</body>
</html>

articlesList.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<title>All Articles</title>
</head>
<body>
<h1>List Articles</h1>
<a href="articles/add.html">Add Article</a>
<br></br>
<c:if test="${!empty articles}">
<table border="5" width="600">
<tr>
<th>Article ID</th>
<th>Article Name</th>
<th>Article Desc</th>
<th>Added Date</th>
</tr>

<c:forEach items="${articles}" var="article">
<tr>
<td><c:out value="${article.articleId}" /></td>
<td><c:out value="${article.articleName}" /></td>
<td><c:out value="${article.articleDesc}" /></td>
<td><c:out value="${article.addedDate}" /></td>
</tr>
</c:forEach>
</table>
</c:if>
</body>
</html>

jdbc.properties

database.driver=com.mysql.jdbc.Driver
database.url=jdbc:mysql://localhost/spring_ankDb
database.user=root
database.password=root
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.show_sql=true

Click to see the code/classes/interfaces of the below listed packages :

Download Source Code