In Spring the hibernate integration becomes easy and you need not to create separate hibernate.cfg.xml for the project. You can configure the hibernate settings in the spring web application context itself. Please find the simple project using the spring hibernate configuration.
web.xml
------------------------------------------------------------------------------------------------------
TestDAO.java
1. antlr-2.7.6.jar
2 .commons-collections-3.1.jar
3. commons-logging.jar
4. dom4j-1.6.1.jar
5. hibernate-jpa-2.0-api-1.0.0.Final.jar
6. hibernate3.jar
7. javassist-3.9.0.GA.jar
8. jcl-over-slf4j-1.6.4-sources.jar
9. jstl.jar
10. jta-1.1.jar
11. mysql-connector-java-5.1.18-bin.jar
12. slf4j-api-1.6.4.jar
13. slf4j-jcl-1.6.4.jar
14. slf4j-log4j12-1.6.4-sources.jar
15. spring-webmvc.jar
16. spring.jar
17. standard.jar
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_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>CaptionContest</display-name>
<welcome-file-list>
<welcome-file>/home.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>SpringHibernate</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringHibernate</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
</web-app>
----------------------------------------------------------------------------
SpringHibernate-servlet.xml
Please replace your username, password and database name of the database in the below file in the respective positions.
<?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:dwr="http://www.directwebremoting.org/schema/spring-dwr"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.directwebremoting.org/schema/spring-dwr
http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd">
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/jsp/" />
<property name="suffix" value=".jsp"></property>
</bean>
<bean id="myDataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/YourDatabasename" />
<property name="username" value="YourUsername" />
<property name="password" value="YourPassword" />
</bean>
<bean id="mySessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="annotatedClasses">
<list>
<value>com.lnt.spring.User</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">validate</prop>
<prop key="hibernate.c3p0.minPoolSize">5</prop>
<prop key="hibernate.c3p0.maxPoolSize">20</prop>
<prop key="hibernate.c3p0.timeout">600</prop>
<prop key="hibernate.c3p0.max_statement">50</prop>
<prop key="hibernate.c3p0.testConnectionOnCheckout">false</prop>
</props>
</property>
</bean>
<bean id="user" class="com.lnt.spring.User" />
<bean name="/home.html" class="com.lnt.spring.TestController" >
<property name="user" ref="user" />
<property name="testDAO" ref="testDAO"></property>
</bean>
<bean id="testDAO" class="com.lnt.spring.TestDAO">
<property name="sessionFactory" ref="mySessionFactory" />
</bean>
</beans>
-------------------------------------------------------------------------
TestController.java
package com.lnt.spring;
import java.util.List;
import java.util.ListIterator;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
public class TestController extends MultiActionController{
private User user;
private TestDAO testDAO;
public void setTestDAO(TestDAO testDAO) {
this.testDAO = testDAO;
}
public void setUser(User user) {
this.user = user;
}
public ModelAndView home(HttpServletRequest request,HttpServletResponse response) throws Exception{
List<User> list=testDAO.loginRequest("prajith", "prajith");
ListIterator<User> itr=list.listIterator();
System.out.println(list.get(0).getFname());
System.out.println("list size :"+list.size());
while(itr.hasNext()){
user=itr.next();
System.out.println("user fname :"+user.getFname());
}
return new ModelAndView("home","user",user.getFname());
}
}
------------------------------------------------------------------------------------------------------
TestDAO.java
package com.lnt.spring;
import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;
public class TestDAO {
private HibernateTemplate hibernateTemplate;
public void setSessionFactory(SessionFactory sessionFactory){
this.hibernateTemplate = new HibernateTemplate(sessionFactory);
}
public List<User> loginRequest(String userName, String password) throws Exception{
List<User> list=hibernateTemplate.find("from User u where u.username = ? and u.password=? ",new Object[] {userName,password});
System.out.println("list size :"+list.size());
return list;
}
}
-------------------------------------------------------------------------
User.java
package com.lnt.spring;
import java.io.Serializable;
import javax.persistence.*;
/**
* The persistent class for the user database table.
*
*/
@Entity
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String fname;
private String password;
private String username;
public User() {
}
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
public String getFname() {
return this.fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
}
----------------------------------------------------------------------------------------------------
home.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>Insert title here</title>
</head>
<body>
<h1>The user First Name "${user}" is coming form the database using spring hibernate</h1>
</body>
</html>
--------------------------------------------------------------------------
Please find the project structure of the project.
--------------------------------------------------------------------------
Please note the required jar list.
1. antlr-2.7.6.jar
2 .commons-collections-3.1.jar
3. commons-logging.jar
4. dom4j-1.6.1.jar
5. hibernate-jpa-2.0-api-1.0.0.Final.jar
6. hibernate3.jar
7. javassist-3.9.0.GA.jar
8. jcl-over-slf4j-1.6.4-sources.jar
9. jstl.jar
10. jta-1.1.jar
11. mysql-connector-java-5.1.18-bin.jar
12. slf4j-api-1.6.4.jar
13. slf4j-jcl-1.6.4.jar
14. slf4j-log4j12-1.6.4-sources.jar
15. spring-webmvc.jar
16. spring.jar
17. standard.jar
No comments:
Post a Comment