[SSH] Eclipse+Struts2+Hibernate4+Spring4的整合
在前面Struts2和Hibernate4的整合基础上,对Spring4进行整合,网上参考的大概都是Spring4+Hibernate3或者基于MyEclispe的,这里把自己研究后的配置和大家分享一下。
本次相关的版本如下:
Spring : spring-framework-4.0.1.RELEASE
1.将相关的jar包导入:
1.1libs目录下包含所有的jar包(不需要复制结尾为sources和javadoc的jar包)到SSHProject项目的lib目录
1.2 Hibernate内lib/optional/c3p0下的c3p0-0.9.2.1.jar复制到lib不目下
2.添加Spring的配置信息,放在WEB-INF下(都为Spring4的配置和Spring3的参数不同,大家注意一下相应的class名字)。
applicationContext.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- 定义数据源的信息 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass"> <value>com.mysql.jdbc.Driver</value> </property> <property name="jdbcUrl"> <value>jdbc:mysql://localhost/ss3h</value> </property> <property name="user"> <value>root</value> </property> <property name="password"> <value>root</value> </property> <property name="maxPoolSize"> <value>80</value> </property> <property name="minPoolSize"> <value>1</value> </property> <property name="initialPoolSize"> <value>1</value> </property> <property name="maxIdleTime"> <value>20</value> </property> </bean> <!--定义Hibernate的SessionFactory --> <!-- SessionFactory使用的数据源为上面的数据源 --> <!-- 指定了Hibernate的映射文件和配置信息 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource"> <ref local="dataSource" /> </property> <property name="mappingResources"> <list> <value>com/tiny/ssh/beans/User.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="show_sql">true</prop> <prop key="hibernate.jdbc.batch_size">20</prop> </props> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <bean id="baseDao" class="com.tiny.ssh.daoImpl.UserDao"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> <!--用户注册业务逻辑类 --> <bean id="userManager" class="com.tiny.ssh.serviceImp.UserManagerImpl"> <property name="dao"> <ref bean="baseDao" /> </property> </bean> <!-- 用户注册的Action --> <bean id="regAction" class="com.tiny.ssh.action.RegisterAction"> <property name="userManager"> <ref bean="userManager" /> </property> </bean> <!-- more bean definitions go here --> </beans>
3.删除Hibernate的配置文件Hibernate.cfg.xml和工厂类HibernateSesseionFactory类。他们的工作已经交给Spring去做,已经不再有用。
4.修改相应的类
接口类BaseDao.java:
package com.tiny.ssh.dao; import org.hibernate.HibernateException; public interface BaseDao { public void saveObject(Object obj) throws HibernateException; }
实现类UserDao.java (注意下面注释中不一样的地方,由于Hibernate4的默认配置不在有getCurrentSession()方法,要用openSession()来代替):
(##刚写博文的时候,这里有点弄的不是太清楚##根据 @galison 的提议修改##
如果替换为openSession(),则事务的管理由Hibernate来处理,Hibernate4与3不一样的地方,管理不是很方便,可以应用原来的getCurrentSession()来获取,此时由spring来管理事务,有如下两种方式(更详细的资料可以参考网络中的说明和文档)
(1)配置<
prop
key
=
"hibernate.current_session_context_class"
>org.springframework.orm.hibernate4.SpringSessionContext</
prop
>
(2)添加事务注解 @Transactional
)
package com.tiny.ssh.daoImpl; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Autowired; import com.tiny.ssh.dao.BaseDao; public class UserDao implements BaseDao { private SessionFactory sessionFactory; public SessionFactory getSessionFactory() { return sessionFactory; } @Autowired public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } private Session getCurrentSession() { return sessionFactory.openSession(); // return sessionFactory.getCurrentSession(); } @Override public void saveObject(Object obj) throws HibernateException { this.getCurrentSession().save(obj); } }
业务逻辑实现类UserManagerImpl.java:
package com.tiny.ssh.serviceImp; import org.hibernate.HibernateException; import org.springframework.beans.BeanUtils; import com.tiny.ssh.beans.User; import com.tiny.ssh.dao.BaseDao; import com.tiny.ssh.forms.UserForm; import com.tiny.ssh.service.UserManager; public class UserManagerImpl implements UserManager { private BaseDao dao; public void setDao(BaseDao dao) { this.dao = dao; } @Override public void regUser(UserForm userForm) throws HibernateException { User user = new User(); BeanUtils.copyProperties(userForm, user); dao.saveObject(user); } }
用户注册Action类RegisterAction.java :
package com.tiny.ssh.action; import com.opensymphony.xwork2.ActionSupport; import com.tiny.ssh.forms.UserForm; import com.tiny.ssh.service.UserManager; public class RegisterAction extends ActionSupport { private static final long serialVersionUID = 1L; private UserForm user; private UserManager userManager; public UserForm getUser() { return user; } public void setUser(UserForm user) { this.user = user; } public void setUserManager(UserManager userManager) { this.userManager = userManager; } public String execute() { try { userManager.regUser(user); return SUCCESS; } catch (Exception e) { e.printStackTrace(); return ERROR; } } }
加入Listener后的web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>SSH Project</display-name> <filter> <filter-name>SSH</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>SSH</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <welcome-file-list> <welcome-file>Register.jsp</welcome-file> </welcome-file-list> </web-app>
5.最终的层次结构
6.在期间有一些ClassNotFound的错误,按照网上的提示多导入了一些包,最终所引用的所有的包(共50个包)
7.结果
总结:至此,大概的环境都已经完事,希望大家在配置的时候用自己的名字,这样能更好的找到其中的映射文件已经功能,更快的上手,有疑问的欢迎留言一起交流 (^.^)
参考链接:
http://blog.csdn.net/yeohcooller/article/details/9316923