spring整合hibernate4的怎样配置

viphyy 的BLOG
用户名:viphyy
文章数:24
访问量:5964
注册日期:
阅读量:5863
阅读量:12276
阅读量:405632
阅读量:1094236
51CTO推荐博文
最新SpringMVC+Spring4.1.1+hibernate4.3.5+maven 整合详细配置app主配置web.xml文件如下:&web-app&xmlns="/xml/ns/javaee"&xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="/xml/ns/javaee&/xml/ns/javaee/web-app_3_0.xsd"
version="3.0"&
&!--&Spring4&--&
&context-param&
&param-name&contextConfigLocation&/param-name&
&param-value&
classpath:spring/spring.applicationContext.xml
&/param-value&
&/context-param&
&listener&
&listener-class&org.springframework.web.context.ContextLoaderListener&/listener-class&
&/listener&
&!--&Spring4.0&MVC配置&--&
&servlet-name&springMVC&/servlet-name&
&servlet-class&org.springframework.web.servlet.DispatcherServlet&/servlet-class&
&init-param&
&param-name&contextConfigLocation&/param-name&
&param-value&classpath:spring/spring.web.xml&/param-value&
&/init-param&
&load-on-startup&1&/load-on-startup&
&/servlet&
&filter-name&encodingFilter&/filter-name&
&filter-class&org.springframework.web.filter.CharacterEncodingFilter&/filter-class&
&init-param&
&param-name&encoding&/param-name&
&param-value&UTF-8&/param-value&
&/init-param&
&filter-mapping&
&filter-name&encodingFilter&/filter-name&
&url-pattern&*.do&/url-pattern&
&/filter-mapping&
&filter-name&httpMethodFilter&/filter-name&
&filter-class&org.springframework.web.filter.HiddenHttpMethodFilter&/filter-class&
&filter-mapping&
&filter-name&httpMethodFilter&/filter-name&
&servlet-name&springMVC&/servlet-name&
&/filter-mapping&
&servlet-mapping&
&servlet-name&springMVC&/servlet-name&
&url-pattern&*.do&/url-pattern&
&/servlet-mapping&
&servlet-mapping&
&servlet-name&springMVC&/servlet-name&
&url-pattern&*.action&/url-pattern&
&/servlet-mapping&
&!--&错误处理&--&
&error-page&
&error-code&403&/error-code&
&location&/jsp/error/403.jsp&/location&
&/error-page&
&error-page&
&error-code&404&/error-code&
&location&/jsp/error/404.jsp&/location&
&/error-page&
&error-page&
&error-code&405&/error-code&
&location&/jsp/error/405.jsp&/location&
&/error-page&
&error-page&
&error-code&500&/error-code&
&location&/jsp/error/500.jsp&/location&
&/error-page&
&welcome-file-list&
&welcome-file&index.jsp&/welcome-file&
&/welcome-file-list&
&!--&&filter&&&filter-name&sessionFilter&/filter-name&&&filter-class&com.everich.everichscm.filter.SessionFilter&/filter-class&&
&/filter&&&filter-mapping&&&filter-name&sessionFilter&/filter-name&&&url-pattern&/*&/url-pattern&&
&/filter-mapping&&--&
&!--&下载保存&--&
&mime-mapping&
&extension&doc&/extension&
&mime-type&application/msword&/mime-type&
&/mime-mapping&
&mime-mapping&
&extension&xls&/extension&
&mime-type&application/msexcel&/mime-type&
&/mime-mapping&
&mime-mapping&
&extension&xlsx&/extension&
&mime-type&application/msexcel&/mime-type&
&/mime-mapping&
&mime-mapping&
&extension&pdf&/extension&
&mime-type&application/pdf&/mime-type&
&/mime-mapping&
&/web-app&spring配置文件:spring.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"&xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"&xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:ehcache="http://ehcache-spring-/svn/schema/ehcache-spring"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context&
http://www.springframework.org/schema/context/spring-context.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://ehcache-spring-/svn/schema/ehcache-spring&&&
http://ehcache-spring-/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd"&
&!--&AOP&自动代理&--&
&aop:aspectj-autoproxy&/&
&context:property-placeholder&location="classpath:/spring/*.properties"&/&
&context:annotation-config&/&
&context:component-scan&base-package="com.everich.everichscm"
use-default-filters="true"&
&context:exclude-filter&type="annotation"
expression="org.springframework.stereotype.Controller"&/&
&/context:component-scan&
&bean&id="dataSource"&name="dataSource"
class="mons.dbcp.BasicDataSource"&
&property&name="driverClassName"&value="${basic.driverClassName}"&/&
&property&name="url"&value="${basic.url}"&/&
&property&name="username"&value="${basic.username}"&/&
&property&name="password"&value="${basic.password}"&/&
&&bean&id="dataSourceSql"&name="dataSourceSql"
class="mons.dbcp.BasicDataSource"&destroy-method="close"&
&property&name="driverClassName"&value="${mssql.driverClassName}"&/&
&property&name="url"&value="${mssql.url}"&/&
&property&name="username"&value="${mssql.username}"&/&
&property&name="password"&value="${mssql.password}"&/&
&bean&id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&
&property&name="dataSource"&ref="dataSource"&/&
&property&name="packagesToScan"&
&value&com.everich.everichscm.entity&/value&
&/property&
&property&name="hibernateProperties"&
&prop&key="hibernate.dialect"&${basic.dialect}&/prop&
&prop&key="hibernate.show_sql"&${basic.show_sql}&/prop&
&prop&key="hibernate.format_sql"&${basic.format_sql}&/prop&
&prop&key="hbm2ddl.auto"&${basic.hbm2ddl.auto}&/prop&
&prop&key="hibernate.hbm2ddl.auto"&${basic.hibernate.hbm2ddl.auto}&/prop&
&!--&配置缓存提供商&--&
&prop&key="hibernate.cache.provider_class"&org.hibernate.cache.EhCacheProvider&/prop&
&!--&开启二级缓存&--&
&prop&key="hibernate.cache.use_second_level_cache"&true&/prop&
&!--&hibernate的查询缓存&--&
&prop&key="hibernate.cache.use_query_cache"&true&/prop&
&!--&指定ehcache.xml的位置&--&
&prop&key="hibernate.cache.provider_configuration_file_resource_path"&/ehcache.xml&/prop&
&/property&
&bean&id="exceptionTranslator"
class="org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator"&
&property&name="dataSource"&
&ref&bean="dataSource"&/&
&/property&
&tx:annotation-driven&transaction-manager="txManager"&/&
&bean&id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager"&
&property&name="sessionFactory"&ref="sessionFactory"&/&
&tx:advice&id="transactionAdvice"&transaction-manager="txManager"&
&tx:attributes&
&tx:method&name="add*"&propagation="REQUIRED"&/&
&tx:method&name="save*"&propagation="REQUIRED"&/&
&tx:method&name="update*"&propagation="REQUIRED"&/&
&tx:method&name="modify*"&propagation="REQUIRED"&/&
&tx:method&name="delete*"&propagation="REQUIRED"&/&
&tx:method&name="query*"&propagation="SUPPORTS"&read-only="true"&/&
&/tx:attributes&
&/tx:advice&
&/beans&springMVC配置文件:spring.web.xml&?xml&version="1.0"&encoding="UTF-8"?&
&beans&xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"&xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/mvc
&&&&&&&&http://www.springframework.org/schema/mvc/spring-mvc.xsd
&&&&&&&&http://www.springframework.org/schema/context
&&&&&&&&http://www.springframework.org/schema/context/spring-context-3.2.xsd"&
&context:annotation-config&/&
&!--&spring&mvc&扫描设置:&与spring容器分开加载&--&
&context:component-scan&base-package="com.everich.everichscm"
use-default-filters="false"&
&context:include-filter&type="annotation"
expression="org.springframework.stereotype.Controller"&/&
&/context:component-scan&
&!--&匹配&Controller&--&
class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"&/&
&!--&使用annocation创建Controller的bean&--&
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"&/&
&!--&rest形式的视图解析器&--&
class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"&
&property&name="mediaTypes"&
&entry&key="json"&value="application/json"&/&
&entry&key="xml"&value="text/xml"&/&
&entry&key="htm"&value="text/html"&/&
&/property&
&property&name="defaultContentType"&value="text/html"&&/property&
&property&name="favorParameter"&value="true"&&/property&
&!--&国际化支持&--&
&bean&id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource"&
&property&name="basename"&
&value&global,systemMgr&/value&
&/property&
class="org.springframework.web.servlet.view.InternalResourceViewResolver"&
&property&name="viewClass"
value="org.springframework.web.servlet.view.JstlView"&/&
&property&name="prefix"&value="/jsp/"&&/property&
&property&name="suffix"&value=".jsp"&&/property&
&property&name="order"&value="2"&&/property&
&bean&id="tiles_viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver"&
&property&name="viewClass"
value="org.springframework.web.servlet.view.tiles2.TilesView"&/&
&property&name="order"&value="1"&&/property&
&bean&id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles2.TilesConfigurer"&
&property&name="definitions"&
&value&/jsp/tiles/tiles-definition.xml&/value&
&/property&
&!--启动Spring&MVC的注解功能,设置编码方式,防止乱码&--&
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"&
&property&name="messageConverters"&
class="org.springframework.http.converter.StringHttpMessageConverter"&
&property&name="supportedMediaTypes"&
&value&text/charset=UTF-8&/value&
&/property&
&/property&
&!--&异常处理&--&
&bean&id="exceptionResolver"
class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"&
&property&name="exceptionMappings"&
&prop&key="com.everich.everichscm.util.exception.SystemException"&error/500&/prop&
&prop&key="com.everich.everichscm.util.exception.BusinessException"&error/errorpage&/prop&
&prop&key="java.lang.exception"&error/500&/prop&
&/property&
&/beans&项目的pom.xml文件,由于内容过多,只粘部分内容&project&xmlns="http://maven.apache.org/POM/4.0.0"&xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0&http://maven.apache.org/maven-v4_0_0.xsd"&
&modelVersion&4.0.0&/modelVersion&
&groupId&app&/groupId&
&artifactId&app&/artifactId&
&packaging&war&/packaging&
&version&0.0.1-SNAPSHOT&/version&
&name&app&/name&
&url&http://maven.apache.org&/url&
&properties&
&spring-version&4.1.1.RELEASE&/spring-version&
&hibernate-version&3.6.1.Final&/hibernate-version&
&tiles-version&2.2.2&/tiles-version&
&slf4j-version&1.5.8&/slf4j-version&
&jackson-version&1.9.13&/jackson-version&
&jstl-version&1.1.2&/jstl-version&
&/properties&
&dependencies&
&dependency&
&groupId&junit&/groupId&
&artifactId&junit&/artifactId&
&version&4.11&/version&
&scope&test&/scope&
&/dependency&
&dependency&
&groupId&net.sourceforge.jtds&/groupId&
&artifactId&jtds&/artifactId&
&version&1.2.4&/version&
&/dependency&
&dependency&
&groupId&com.sun.mail&/groupId&
&artifactId&javax.mail&/artifactId&
&version&1.5.2&/version&
&/dependency&
&dependency&
&groupId&javax.activation&/groupId&
&artifactId&activation&/artifactId&
&version&1.1&/version&
&/dependency&
&dependency&
&groupId&org.springframework.integration&/groupId&
&artifactId&spring-integration-mail&/artifactId&
&version&4.1.0.RELEASE&/version&
&/dependency&
&dependency&
&groupId&net.sf.ehcache&/groupId&
&artifactId&ehcache-core&/artifactId&
&version&2.6.9&/version&
&/dependency&
&dependency&
&groupId&com.googlecode.ehcache-spring-annotations&/groupId&
&artifactId&ehcache-spring-annotations&/artifactId&
&version&${jstl-version}&/version&
&/dependency&
&dependency&
&groupId&jstl&/groupId&
&artifactId&jstl&/artifactId&
&version&${jstl-version}&/version&
&/dependency&
&dependency&
&groupId&taglibs&/groupId&
&artifactId&standard&/artifactId&
&version&${jstl-version}&/version&
&/dependency&
&dependency&
&groupId&org.apache.struts&/groupId&
&artifactId&struts2-core&/artifactId&
&version&2.3.16.3&/version&
&/dependency&
&dependency&
&groupId&javax.mail&/groupId&
&artifactId&mail&/artifactId&
&version&1.5.0-b01&/version&
&/dependency&
&dependency&
&groupId&org.springframework&/groupId&
&artifactId&spring-beans&/artifactId&
&version&${spring-version}&/version&
&/dependency&
&dependency&
&groupId&org.springframework&/groupId&
&artifactId&spring-context&/artifactId&
&version&${spring-version}&/version&
&/dependency&
&dependency&
&groupId&org.springframework&/groupId&
&artifactId&spring-expression&/artifactId&
&version&${spring-version}&/version&
&/dependency&
&dependency&
&groupId&org.springframework&/groupId&
&artifactId&spring-aop&/artifactId&
&version&${spring-version}&/version&
&/dependency&
&dependency&
&groupId&aopalliance&/groupId&
&artifactId&aopalliance&/artifactId&
&version&1.0&/version&
&/dependency&
&dependency&
&groupId&org.springframework&/groupId&
&artifactId&spring-core&/artifactId&
&version&${spring-version}&/version&
&/dependency&
&dependency&
&groupId&org.springframework&/groupId&
&artifactId&spring-orm&/artifactId&
&version&${spring-version}&/version&
&/dependency&
&dependency&
&groupId&org.springframework&/groupId&
&artifactId&spring-web&/artifactId&
&version&${spring-version}&/version&
&/dependency&
&dependency&
&groupId&org.springframework&/groupId&
&artifactId&spring-webmvc&/artifactId&
&version&${spring-version}&/version&
&/dependency&
&dependency&
&groupId&org.springframework&/groupId&
&artifactId&spring-test&/artifactId&
&version&${spring-version}&/version&
&/dependency&
&dependency&
&groupId&javax.servlet&/groupId&
&artifactId&javax.servlet-api&/artifactId&
&version&3.1.0&/version&
&/dependency&
&dependency&
&groupId&javax.servlet.jsp&/groupId&
&artifactId&jsp-api&/artifactId&
&version&2.2&/version&
&/dependency&
&dependency&
&groupId&org.hibernate&/groupId&
&artifactId&hibernate-core&/artifactId&
&version&${hibernate-version}&/version&
&/dependency&
&dependency&
&groupId&org.hibernate&/groupId&
&artifactId&hibernate-ehcache&/artifactId&
&version&${hibernate-version}&/version&
&/dependency&
&dependency&
&groupId&org.slf4j&/groupId&
&artifactId&slf4j-nop&/artifactId&
&version&${slf4j-version}&/version&
&/dependency&
&dependency&
&groupId&org.slf4j&/groupId&
&artifactId&slf4j-log4j12&/artifactId&
&version&${slf4j-version}&/version&
&/dependency&
&dependency&
&groupId&org.slf4j&/groupId&
&artifactId&slf4j-api&/artifactId&
&version&${slf4j-version}&/version&
&/dependency&
&dependency&
&groupId&dom4j&/groupId&
&artifactId&dom4j&/artifactId&
&version&1.6.1&/version&
&/dependency&
&dependency&
&groupId&commons-collections&/groupId&
&artifactId&commons-collections&/artifactId&
&version&3.2.1&/version&
&/dependency&
了这篇文章
类别:┆阅读(0)┆评论(0)博客访问: 356829
博文数量: 138
博客积分: 5850
博客等级: 大校
技术积分: 1474
注册时间:
IT168企业级官微
微信号:IT168qiye
系统架构师大会
微信号:SACC2013
配置成功Hibernate同Spring结合的代码自动生成配置Hibernate sync支持Spring快速开发dao层- -&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& Hibernate SyncHShbmpojoDAODAODAODAO&SpringSpringDAODAO&1EclipseHSDAOExtrectSpring&2SpringcglibDAOSpringSpringIoCsetterDAOset(Session session)------------------------------------------------------------------------------HSDAOExtrectHSDAO_BaseRootDAODAOSpringHibernateDaoSupportHibernateDaoSupportHibernateDaoSupportSpringHSDAO^_^good!&HSEclipseHSmapping filenewentitymappingJDBCHSHShbmhbm.xmlhbmHS-->Synchronize and OverviewHSBasedaobaseHSDao_BaseRootDAODaoBaseUserDAOPOJOBaseUserdaoDaoUserDAODAObaseDAOSpringbaseDao_BaseRootDAOSpringHibernateDaoSupportHSSessionDAOBaseUserDAOEclipseExtractIUserDAOSpringAOPSessionFactoryDAO^_^&SpringsessionFactoryhibernatePropertiestrue加入到它的属性列表中,如下:&&&&&&&&& <property name="hibernateProperties">&&&&&&&& &&&&&&& <prop key="hibernate.dialect">&&&&&&&&& net.sf.hibernate.dialect.MySQLDialect&&&&&&& &&&&&&&&&&&&& <prop key="hibernate.show_sql">true&&&&&&&&&&& & <prop key="hibernate.cglib.use_reflection_optimizer">true&&&&&&&&&& &&&&&&&&&& BaseRootDAO extends HibernateDaoSupport&lHSbaseDAOHibernateDaoSupportsetSessionFactoryHibernateDaoSupportlHSDAOPOPOhbmHSHS&&HSlHSvmeclipseplugincom.hudson.hibernatesynchronizer_2.3.1hibernatesynchronizer.jarhibernatesynchronizervmBaseRootDAO.vmUltraEditimport org.springframework.orm.hibernate.support.HibernateDaoSHibernateDaoSupportjar&lwindows-->preferences-->HS-->snippetsDAOSpringHibernateDaoSupportBaseRootDAOSessionMothedprotected static void setSessionFactory (SessionFactory sessionFactory) {setSessionFactory((String) null, sessionFactory);}protected SessionFactory getSessionFactory() throws HibernateException {return getSessionFactory (getConfigurationFileName());}protected Session getSession() throws HibernateException {return createSession();}产生的DAO代码默认支持HibernateDaoSupport支持一对多关系和多对多关系文件到Address的一对多联系(双向)l中添加:&&&&&&& <set name="Address" lazy="true" inverse="true" cascade="all" >&&&&&&&&& <key column="id"/> &&&&&&&&& <one-to-many class="com.sxf.techSample.entity.Address" />&&&&&&& </set>l中添加:&&&&&& <many-to-one name="user" column="userid"&&&&&&&&&& class="com.sxf.techSample.entity.User" cascade="save-update">&&&&&& </many-to-one>l文件中生成以下代码/**&* Return the value associated with the column: Address&*/public java.util.Set getAddress () {return this._}/**&* Set the value related to the column: Address&* @param _address the Address value&*/public void setAddress (java.util.Set _address) {
相关热门文章
给主人留下些什么吧!~~
是否可以告诉俺哪能下HS???找了好半天也没找到,挺郁闷!
如果兄台肯把这个软件发给俺请联系我MSN
最近发现一个代码生成工具的好东东,是国人写的免费的.
这时官方网站上的介绍.
Spdevelop是国内唯一基于模板引擎的代码生成工具.还集成数据库建模软件。
Spdevelop致力于满足全球开发人员的愿望和需求. 两年来,我们一直坚持不懈追求一个目标:帮助改善开发团队在软件开发项目中的协作、提高软件质量、加速应用程序开发.
SpDevelop DB Designer是一款数据库建模软件,它可根据用户的需求进行调整,二次开发,它支持完全成熟的相应商业软件(如Visio或ERStudio)的所有功能,SpDevelop的数据库建模功能有:图形化设计,模块化设计,正向反向工程,基于VSS的项目管理,丰富的导出方式等功能.利用它,您甚至可以生成各种各样的代码,可以作为多种数据库的查询分析器
SpDevelop Code Gener是一种基于模板的代码生成工具,它使用类似于 ASP.NET 的语法来生成任意类型的代码或文本。与其它许多代码生成工具不同,SpDevelop不要求你订阅特定的应用程序设计或体系结构。借助SpDevelop可以生成
哦,感觉好麻烦
请作者联系我一下,我有几个问题想请教你。
我的QQ:4316551
请登录后评论。SSH环境搭建之Hibernate与Spring整合与最终整合测试(三) - 简书
SSH环境搭建之Hibernate与Spring整合与最终整合测试(三)
管理SessionFactory实例(只需一个)(使用IOC管理SessionFactory对象)
声明式事务管理
1.导入依赖包
除外(这里用的是hibernate-release-5.1.0.Final & spring-framework-4.2.5.RELEASE),还要导入aopalliance.jar(SSH框架必需包,否则在测试事务时会出错),同时还需要注意:struts-2.3.28\apps\struts2-blank.war\WEB-INF\lib目录下有javassist.jar,hibernate-release-5.1.0.Final\lib\required下也有javassist.jar,在进行整合时只需留下其中一个,否则会报错。
Paste_Image.png
2.在applicationContext.xml文件中添加配置sessionFactory的配置
&!-- 配置SessionFactory --&
&bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"&
&!-- 指定hibernate的配置文件位置 --&
&property name="configLocation" value="classpath:hibernate.cfg.xml"&&/property&
&!-- 配置c3p0数据库连接池 --&
&property name="dataSource"&
&bean class="com.mchange.boPooledDataSource"&
&!-- 数据连接信息 --&
&property name="jdbcUrl" value="${jdbcUrl}"&&/property&
&property name="driverClass" value="${driverClass}"&&/property&
&property name="user" value="${user}"&&/property&
&property name="password" value="${password}"&&/property&
&!-- 其他配置 --&
&!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 --&
&property name="initialPoolSize" value="3"&&/property&
&!--连接池中保留的最小连接数。Default: 3 --&
&property name="minPoolSize" value="3"&&/property&
&!--连接池中保留的最大连接数。Default: 15 --&
&property name="maxPoolSize" value="5"&&/property&
&!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 --&
&property name="acquireIncrement" value="3"&&/property&
&!-- 控制数据源内加载的PreparedStatements数量。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 --&
&property name="maxStatements" value="8"&&/property&
&!--maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 --&
&property name="maxStatementsPerConnection" value="5"&&/property&
&!--最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 --&
&property name="maxIdleTime" value="1800"&&/property&
&/property&
(需要注意的是:class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"中需要对应自己的Hibernate版本,否则会出现java.lang.NoClassDefFoundError: [Lorg/hibernate/engine/FilterD的错误提示,配置声明式事务管理时也需要修改成对应的版本)
3.新建属性文件:jdbc.properties
= jdbc:mysql:///数据库名字
driverClass
= com.mysql.jdbc.Driver
在applicationContext.xml里导入.properties属性文件
&!-- 导入外部的properties文件 --&
&context:property-placeholder location="classpath:jdbc.properties"/&
(需要注意的是,使用context:标签需要在头部声明)
4.配置声明式事务管理
&!-- 配置声明式事务管理(采用注解的方式) --&
&bean id="txManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"&
&property name="sessionFactory" ref="sessionFactory"&&/property&
&tx:annotation-driven transaction-manager="txManager"/&
(需要注意的是,使用tx:annotation-driven标签需要在头部声明)
applicationContext.xml最终内容为:
&?xml version="1.0" encoding="UTF-8"?&
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"&
自动扫描与装配bean--&
&context:component-scan base-package="domain"&&/context:component-scan&
&!-- 导入外部的properties文件 --&
&context:property-placeholder location="classpath:jdbc.properties"/&
&!-- 配置SessionFactory --&
&bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"&
&!-- 指定hibernate的配置文件位置 --&
&property name="configLocation" value="classpath:hibernate.cfg.xml"&&/property&
&!-- 配置c3p0数据库连接池 --&
&property name="dataSource"&
&bean class="com.mchange.boPooledDataSource"&
&!-- 数据连接信息 --&
&property name="jdbcUrl" value="${jdbcUrl}"&&/property&
&property name="driverClass" value="${driverClass}"&&/property&
&property name="user" value="${user}"&&/property&
&property name="password" value="${password}"&&/property&
&!-- 其他配置 --&
&!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 --&
&property name="initialPoolSize" value="3"&&/property&
&!--连接池中保留的最小连接数。Default: 3 --&
&property name="minPoolSize" value="3"&&/property&
&!--连接池中保留的最大连接数。Default: 15 --&
&property name="maxPoolSize" value="5"&&/property&
&!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 --&
&property name="acquireIncrement" value="3"&&/property&
&!-- 控制数据源内加载的PreparedStatements数量。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 --&
&property name="maxStatements" value="8"&&/property&
&!--maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 --&
&property name="maxStatementsPerConnection" value="5"&&/property&
&!--最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 --&
&property name="maxIdleTime" value="1800"&&/property&
&/property&
&!-- 配置声明式事务管理(采用注解的方式) --&
&bean id="txManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"&
&property name="sessionFactory" ref="sessionFactory"&&/property&
&tx:annotation-driven transaction-manager="txManager"/&
自动扫描与装配bean--&
&context:component-scan base-package="domain"&&/context:component-scan&
把要扫描的包写自己的包名)
5.web.xml配置
&?xml version="1.0" encoding="UTF-8"?&
&web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="/xml/ns/javaee" xsi:schemaLocation="/xml/ns/javaee /xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"&
&display-name&Struts2SpringZH&/display-name&
&!-- Strus2核心配置
&filter-name&struts2&/filter-name&
&filter-class&org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter&/filter-class&
&filter-mapping&
&filter-name&struts2&/filter-name&
&url-pattern&/*&/url-pattern&
&/filter-mapping&
&welcome-file-list&
&welcome-file&index.html&/welcome-file&
&/welcome-file-list&
&!-- 根据默认配置文件来初始化Spring容器 --&
&listener&
&listener-class&org.springframework.web.context.ContextLoaderListener&/listener-class&
&/listener&
&!-- 配置spring的用于初始化容器对象的监听器--&
&context-param&
&param-name&contextConfigLocation&/param-name&
&param-value&classpath:applicationContext*.xml,/WEB-INF/applicationContext*.xml&/param-value&
&/context-param&
&/web-app&
(注意:这里加入了Struts的配置)
Paste_Image.png
//Person.java
import javax.persistence.E
import javax.persistence.Id;
public class Person {
public int getId() {
public void setId(int id) {
public String getName() {
public void setName(String name) {
this.name =
Person.hbm.xml
&?xml version="1.0"?&
&!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" &
&hibernate-mapping&
&class name ="domain.Person"&
&!--name指定映射的类,table指定数据库中对应的表,省略则与类名一致--&
&id name ="id"&
&generator class ="native"&&/generator&
&property name ="name"&&/property&
&/hibernate-mapping&
hibernate.cfg.xml --!&
可以不用hibernate.cfg.xml,通过数据池访问数据库,通过注解的方式映射文件 --!&
&?xml version='1.0' encoding= 'utf-8'?&
&!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" &
&hibernate-configuration&
&session-factory&
&!-- Echo all executed SQL to stdout --&
&property name="show_sql"&true&/property&
&property name="format_sql"&true&/property&
&!-- Drop and re-create the database schema on startup --&
&property name="hbm2ddl.auto"&update&/property&
&mapping resource="domain/Person.hbm.xml" /&
&/session-factory&
&/hibernate-configuration&
applicationContext.xml
&?xml version="1.0" encoding="UTF-8"?&
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"&
自动扫描与装配bean--&
&context:component-scan base-package="domain"&&/context:component-scan&
&context:component-scan base-package="cn.test"&&/context:component-scan&
&!-- 导入外部的properties文件 --&
&context:property-placeholder location="classpath:jdbc.properties"/&
&!-- 配置SessionFactory --&
&bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"&
&!-- 指定hibernate的配置文件位置 --&
&property name="configLocation" value="classpath:hibernate.cfg.xml"&&/property&
&!-- 配置c3p0数据库连接池 --&
&property name="dataSource"&
&bean class="com.mchange.boPooledDataSource"&
&!-- 数据连接信息 --&
&property name="jdbcUrl" value="${jdbcUrl}"&&/property&
&property name="driverClass" value="${driverClass}"&&/property&
&property name="user" value="${user}"&&/property&
&property name="password" value="${password}"&&/property&
&!-- 其他配置 --&
&!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 --&
&property name="initialPoolSize" value="3"&&/property&
&!--连接池中保留的最小连接数。Default: 3 --&
&property name="minPoolSize" value="3"&&/property&
&!--连接池中保留的最大连接数。Default: 15 --&
&property name="maxPoolSize" value="5"&&/property&
&!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 --&
&property name="acquireIncrement" value="3"&&/property&
&!-- 控制数据源内加载的PreparedStatements数量。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 --&
&property name="maxStatements" value="8"&&/property&
&!--maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 --&
&property name="maxStatementsPerConnection" value="5"&&/property&
&!--最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 --&
&property name="maxIdleTime" value="1800"&&/property&
&/property&
&!-- 配置声明式事务管理(采用注解的方式) --&
&bean id="txManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"&
&property name="sessionFactory" ref="sessionFactory"&&/property&
&tx:annotation-driven transaction-manager="txManager"/&
#jdbc.properties
= jdbc:mysql:///test
driverClass
= com.mysql.jdbc.Driver
&?xml version="1.0" encoding="UTF-8"?&
&web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="/xml/ns/javaee" xsi:schemaLocation="/xml/ns/javaee /xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"&
&display-name&Struts2SpringZH&/display-name&
&!-- Strus2核心配置
&filter-name&struts2&/filter-name&
&filter-class&org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter&/filter-class&
&filter-mapping&
&filter-name&struts2&/filter-name&
&url-pattern&/*&/url-pattern&
&/filter-mapping&
&welcome-file-list&
&welcome-file&index.html&/welcome-file&
&/welcome-file-list&
&!-- 根据默认配置文件来初始化Spring容器 --&
&listener&
&listener-class&org.springframework.web.context.ContextLoaderListener&/listener-class&
&/listener&
&!-- 配置spring的用于初始化容器对象的监听器--&
&context-param&
&param-name&contextConfigLocation&/param-name&
&param-value&classpath:applicationContext*.xml,/WEB-INF/applicationContext*.xml&/param-value&
&/context-param&
&/web-app&
//TestService.java
package cn.
import javax.annotation.R
import org.hibernate.S
import org.hibernate.SessionF
import org.springframework.stereotype.S
import org.springframework.transaction.annotation.T
import domain.P
//要注入必要要求当前对象在容器里
@Service("testService")
public class TestService {
//注入bean
private SessionFactory sessionF
@Transactional
public void saveTwoPerson(){
//得到外面已经管理好事务的session而不是opensession
Session session = sessionFactory.getCurrentSession();
session.save(new Person());
session.save(new Person());
//SpringTest.java
package cn.
import org.hibernate.SessionF
import org.junit.T
import org.springframework.context.ApplicationC
import org.springframework.context.support.ClassPathXmlApplicationC
public class SpringTest {
private ApplicationContext ac =new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
//SessionFactory测试
public void testSessionFactory() throws Exception{
SessionFactory sessionFactory = (SessionFactory) ac.getBean("sessionFactory");
System.out.println(sessionFactory);
//测试事务
public void testTransaction() throws Exception{
TestService testService = (TestService) ac.getBean("testService");
testService.saveTwoPerson();
运行结果:
Paste_Image.png
Paste_Image.png
SSH框架最终测试:
在上述基础上,(看步骤1.导入依赖包)。在项目中的config目录新建struts.xml(内容即为里面的struts.xml):
&!-- struts.xml --&
&?xml version="1.0" encoding="UTF-8" ?&
&!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"&
&!-- 配置为开发模式,一旦文件改动,不需要重启,会立即生效 --&
&constant name="struts.devMode" value="true" /&
&!-- 把扩展名设置为action --&
&constant name="struts.action.extension" value="action" /&
&!-- 把主题配置为simple --&
&constant name="struts.ui.theme" value="simple" /&
&package name="default" namespace="/" extends="struts-default"&
&!-- 整合框架后class可以直接写bean名称 --&
&action name="test" class="testAction"&
&result name="success"&/index.jsp&/result&
&/package&
在test/cn.test下新建TestAction.java:
//TestAction.java
package cn.
import javax.annotation.R
import org.junit.T
import org.springframework.context.annotation.S
import org.springframework.stereotype.C
import com.opensymphony.xwork2.ActionS
@Controller
@Scope("prototype")
public class TestAction extends ActionSupport{
private TestService testS
public String execute() throws Exception {
System.out.println("TestAction.excute()");
testService.saveTwoPerson();
return "success";
最终结果:(访问test.action时,Console里打印语句,同时执行testService.saveTwoPerson()添加了两个新方法,说明成功整合了SSH框架)
最终显示结果
SSH框架中,Spring管理对象事务、Action管理请求,hibernate处理对象存储
将hibernate.cfg.xml合并到applicationContext.xml中(注解方式):
此时没有用到映射文件,采用自动扫描实体的方法自动扫描注释的实体:
将hibernate.cfg.xml合并到applicationContext.xml中(xml方式):
没有用到自动扫描实体,使用.hbm.xml文件映射:

我要回帖

更多关于 spring配置hibernate4 的文章

 

随机推荐