Spring 3.1.1 + Struts 2.3.1.2 + Hibernate 4.1 整合(SSH)(一)

2014-11-24 07:23:29 · 作者: · 浏览: 0

最近一直有朋友在问,最新版的Spring、Struts、Hibernate整合老是有问题,昨晚大概看了一下。从Hibernate 4 开始,本身已经很好的实现了数据库事务模块,而Spring也把Hibernate4之后的HibernateDaoSupport去掉了,Spring建议使用官方的HibernateAPI进行操作。这样一来,以前习惯使用HibernateDaoSupport来操作的人来说刚刚开始可能有些不习惯。我跟据官方的说明,大概的整合一下。



现在把主要的代码和配置贴出来,供大家参考,其它配置文件和代码和以前没有什么大变化,直接就能用,主要就是Dao。


之前发过的几篇文章都被人转了N千次,但是我发现被转的文章都没有标明原作者,有的还把自己的大名写上去了。用我同事的一句说:“没文化真可怕!” 那些转载不留名的朋友,你们不羞愧吗?做人要厚道,转载请留名!



Web.xml


[html]
< xml version="1.0" encoding="UTF-8" >
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">
Eriloan_com

30


contextConfigLocation
classpath:/spring-config/applicationContext-*.xml


org.springframework.web.context.ContextLoaderListener


org.springframework.web.context.request.RequestContextListener


org.springframework.web.util.IntrospectorCleanupListener


org.apache.struts2.dispatcher.ng.listener.StrutsListener


encodingFilter
org.springframework.web.filter.CharacterEncodingFilter

encoding
UTF-8


forceEncoding
true



encodingFilter
/*


struts-cleanup
org.apache.struts2.dispatcher.ActionContextCleanUp


struts-cleanup

/*


struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter


struts2
/*


openSessionInViewFilter
org.springframework.orm.hibernate4.support.OpenSessionInViewFilter

sessionFactoryBeanName
sessionFactory


singleSession
true


flushMode
AUTO



openSessionInViewFilter
/*



index.jsp


404
/WEB-INF/errorPage/404.jsp


500
/WEB-INF/errorPage/500.jsp



Spring配置文件(applicationContext-common.xml):

[html]
< xml version="1.0" encoding="UTF-8" >
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util"
xmlns:tool="http://www.springframework.org/schema/tool" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/sprin