集成struts,spring,hibernate时,对于初学者来说最大的麻烦就其繁琐的xml配置文件。现在三者都对基于注解的配置提供了良好的支持。在struts2中,使用convent plugin,得益于annotation和规约,配置过程得以大大减少。在spring2.5也可以使用@Autowired,进行注入,使用@Controller,@Service,@Repository注解,自动定义bean,还支持annotation风格的声明式事务支持,以及aspectJ类似的AOP。hibernate也可以使用JPA标准注解定义实体描述,避免使用mapping文件。
当然,对于annotation和xml风格的配置,谁更好,更多依赖个人兴趣。但使用annotation确实减少了很多配置工作量。本文采用annotation风格的配置,以TaskList为例子讲解struts2 spring hibernate的集成。项目文件见附件。
一:配置struts2。
首先在web.xml文件中配置filter
Xml代码
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="14" height="15" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0">
<param name="flashvars" value="clipboard=%09%3Cfilter%3E%0A%09%09%3Cfilter-name%3Estruts2%3C%2Ffilter-name%3E%0A%09%09%3Cfilter-class%3Eorg.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter%3C%2Ffilter-class%3E%0A%09%3C%2Ffilter%3E%0A%0A%09%3Cfilter-mapping%3E%0A%09%09%3Cfilter-name%3Estruts2%3C%2Ffilter-name%3E%0A%09%09%3Curl-pattern%3E*.action%3C%2Furl-pattern%3E%0A%09%3C%2Ffilter-mapping%3E">
<param name="src" value="http://www.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf">
<embed type="application/x-shockwave-flash" width="14" height="15" src="http://www.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%09%3Cfilter%3E%0A%09%09%3Cfilter-name%3Estruts2%3C%2Ffilter-name%3E%0A%09%09%3Cfilter-class%3Eorg.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter%3C%2Ffilter-class%3E%0A%09%3C%2Ffilter%3E%0A%0A%09%3Cfilter-mapping%3E%0A%09%09%3Cfilter-name%3Estruts2%3C%2Ffilter-name%3E%0A%09%09%3Curl-pattern%3E*.action%3C%2Furl-pattern%3E%0A%09%3C%2Ffilter-mapping%3E"></embed></object>
- <filter>
- <filter-name>struts2</filter-name>
- <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
- </filter>
-
- <filter-mapping>
- <filter-name>struts2</filter-name>
- <url-pattern>*.action</url-pattern>
- </filter-mapping>
然后在classpath中创建struts.xml配置文件。
Xml代码
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="14" height="15" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0">
<param name="flashvars" value="clipboard=%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22UTF-8%22%3F%3E%0A%3C!DOCTYPE%20struts%20PUBLIC%20%22-%2F%2FApache%20Software%20Foundation%2F%2FDTD%20Struts%20Configuration%202.1%2F%2FEN%22%0A%20%20%20%20%20%20%20%20%22http%3A%2F%2Fstruts.apache.org%2Fdtds%2Fstruts-2.1.dtd%22%3E%0A%3Cstruts%3E%0A%09%3Cconstant%20name%3D%22struts.devMode%22%20value%3D%22true%22%20%2F%3E%0A%09%3Cconstant%20name%3D%22struts.convention.default.parent.package%22%20value%3D%22default-package%22%20%2F%3E%0A%09%3Cconstant%20name%3D%22struts.convention.package.locators%22%20value%3D%22action%22%20%2F%3E%0A%09%3Cpackage%20name%3D%22default-package%22%20extends%3D%22convention-default%22%3E%0A%0A%09%09%3Cdefault-action-ref%20name%3D%22index%22%20%2F%3E%0A%0A%09%09%3Caction%20name%3D%22index%22%20%20%3E%0A%09%09%09%3Cresult%3E%2FWEB-INF%2Fcontent%2Findex.jsp%3C%2Fresult%3E%0A%09%09%3C%2Faction%3E%09%0A%0A%09%3C%2Fpackage%3E%0A%3C%2Fstruts%3E">
<param name="src" value="http://www.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf">
<embed type="application/x-shockwave-flash" width="14" height="15" src="http://www.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22UTF-8%22%3F%3E%0A%3C!DOCTYPE%20struts%20PUBLIC%20%22-%2F%2FApache%20Software%20Foundation%2F%2FDTD%20Struts%20Configuration%202.1%2F%2FEN%22%0A%20%20%20%20%20%20%20%20%22http%3A%2F%2Fstruts.apache.org%2Fdtds%2Fstruts-2.1.dtd%22%3E%0A%3Cstruts%3E%0A%09%3Cconstant%20name%3D%22struts.devMode%22%20value%3D%22true%22%20%2F%3E%0A%09%3Cconstant%20name%3D%22struts.convention.default.parent.package%22%20value%3D%22default-package%22%20%2F%3E%0A%09%3Cconstant%20name%3D%22struts.convention.package.locators%22%20value%3D%22action%22%20%2F%3E%0A%09%3Cpackage%20name%3D%22default-package%22%20extends%3D%22convention-default%22%3E%0A%0A%09%09%3Cdefault-action-ref%20name%3D%22index%22%20%2F%3E%0A%0A%09%09%3Caction%20name%3D%22index%22%20%20%3E%0A%09%09%09%3Cresult%3E%2FWEB-INF%2Fcontent%2Findex.jsp%3C%2Fresult%3E%0A%09%09%3C%2Faction%3E%09%0A%0A%09%3C%2Fpackage%3E%0A%3C%2Fstruts%3E"></embed></object>
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
- "http://struts.apache.org/dtds/struts-2.1.dtd">
- <struts>
- <constant name="struts.devMode" value="true" />
- <constant name="struts.convention.default.parent.package" value="default-package" />
- <constant name="struts.convention.package.locators" value="action" />
- <package name="default-package" extends="convention-default">
-
- <default-action-ref name="index" />
-
- <action name="index" >
- <result>/WEB-INF/content/index.jsp</result>
- </action>
-
- </package>
- </struts>
struts.devMode属性,配置启用调试,将有更多的错误信息输出,便于排错。struts.convention.default.parent.package属性,指定使用注解标注的控制器的默认包。可以在这个默认包中配置全局信息。
struts.convention.package.locators属性,为查找控制器包路径的关键字。如com.mycompany.action,com.mycompany.action.user,都会被struts2扫描。里面有继承至Action的类,或类名以Action结尾的类,都会做为Action处理。
<default-action-ref name="index" />指定了默认action,如果指定的action不存在则访问该action。
把struts2-spring-plugin-2.1.6.jar添加到classpath中,struts2会自动扫描struts-plugin.xml文件,该文件自动注册了com.opensymphony.xwork2.ObjectFactory,完成和spring的集成。
二:配置spring
在web.xml中加入ContextLoaderListener,用以启动spring容器。用contextConfigLocation指定spring配置文件路径,可以使用*通配符结尾。
Xml代码
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="14" height="15" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0">
<param name="flashvars" value="clipboard=%09%3Clistener%3E%0A%09%09%3Clistener-class%3Eorg.springframework.web.context.ContextLoaderListener%0A%09%09%3C%2Flistener-class%3E%0A%09%3C%2Flistener%3E%0A%09%3Ccontext-param%3E%0A%09%09%3Cparam-name%3EcontextConfigLocation%3C%2Fparam-name%3E%0A%09%09%3Cparam-value%3Eclasspath%3A%2FapplicationContext.xml%3C%2Fparam-value%3E%0A%09%3C%2Fcontext-param%3E">
<param name="src" value="http://www.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf">
<embed type="application/x-shockwave-flash" width="14" height="15" src="http://www.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%09%3Clistener%3E%0A%09%09%3Clistener-class%3Eorg.springframework.web.context.ContextLoaderListener%0A%09%09%3C%2Flistener-class%3E%0A%09%3C%2Flistener%3E%0A%09%3Ccontext-param%3E%0A%09%09%3Cparam-name%3EcontextConfigLocation%3C%2Fparam-name%3E%0A%09%09%3Cparam-value%3Eclasspath%3A%2FapplicationContext.xml%3C%2Fparam-value%3E%0A%09%3C%2Fcontext-param%3E"></embed></object>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener
- </listener-class>
- </listener>
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:/applicationContext.xml</param-value>
- </context-param>
配置applicationContext.xml
Xml代码
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="14" height="15" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0">
<param name="flashvars" value="clipboard=%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22UTF-8%22%3F%3E%0A%3Cbeans%3E%0A%0A%09%3Ccontext%3Acomponent-scan%20base-package%3D%22persistence%2Cservice%2Caction%2Caop%22%2F%3E%0A%09%0A%09%3Caop%3Aaspectj-autoproxy%20%20%2F%3E%0A%0A%09%3Ctx%3Aannotation-driven%20transaction-manager%3D%22transactionManager%22%20%2F%3E%0A%09%0A%09%3Cbean%20id%3D%22transactionManager%22%20class%3D%22org.springframework.orm.hibernate3.HibernateTransactionManager%22%20%3E%0A%09%09%3Cproperty%20name%3D%22sessionFactory%22%20ref%3D%22sessionFactory%22%20%2F%3E%0A%09%3C%2Fbean%3E%0A%0A%09%3Cbean%20id%3D%22sessionFactory%22%20class%3D%22org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean%22%20%3E%09%0A%09%09%3Cproperty%20name%3D%22configLocation%22%20value%3D%22classpath%3Ahibernate.cfg.xml%22%20%2F%3E%0A%09%3C%2Fbean%3E%0A%0A%09%3Cbean%20id%3D%22hibernateTemplate%22%20class%3D%22org.springframework.orm.hibernate3.HibernateTemplate%22%20%3E%0A%09%09%3Cproperty%20name%3D%22sessionFactory%22%20ref%3D%22sessionFactory%22%20%2F%3E%0A%09%3C%2Fbean%3E%0A%09%0A%3C%2Fbeans%3E">
<param name="src" value="http://www.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf">
<embed type="application/x-shockwave-flash" width="14" height="15" src="http://www.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22UTF-8%22%3F%3E%0A%3Cbeans%3E%0A%0A%09%3Ccontext%3Acomponent-scan%20base-package%3D%22persistence%2Cservice%2Caction%2Caop%22%2F%3E%0A%09%0A%09%3Caop%3Aaspectj-autoproxy%20%20%2F%3E%0A%0A%09%3Ctx%3Aannotation-driven%20transaction-manager%3D%22transactionManager%22%20%2F%3E%0A%09%0A%09%3Cbean%20id%3D%22transactionManager%22%20class%3D%22org.springframework.orm.hibernate3.HibernateTransactionManager%22%20%3E%0A%09%09%3Cproperty%20name%3D%22sessionFactory%22%20ref%3D%22sessionFactory%22%20%2F%3E%0A%09%3C%2Fbean%3E%0A%0A%09%3Cbean%20id%3D%22sessionFactory%22%20class%3D%22org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean%22%20%3E%09%0A%09%09%3Cproperty%20name%3D%22configLocation%22%20value%3D%22classpath%3Ahibernate.cfg.xml%22%20%2F%3E%0A%09%3C%2Fbean%3E%0A%0A%09%3Cbean%20id%3D%22hibernateTemplate%22%20class%3D%22org.springframework.orm.hibernate3.HibernateTemplate%22%20%3E%0A%09%09%3Cproperty%20name%3D%22sessionFactory%22%20ref%3D%22sessionFactory%22%20%2F%3E%0A%09%3C%2Fbean%3E%0A%09%0A%3C%2Fbeans%3E"></embed></object>
- <?xml version="1.0" encoding="UTF-8"?>
- <beans>
-
- <context:component-scan base-package="persistence,service,action,aop"/>
-
- <aop:aspectj-autoproxy />
-
- <tx:annotation-driven transaction-manager="transactionManager" />
-
- <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" >
- <property name="sessionFactory" ref="sessionFactory" />
- </bean>
-
- <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" >
- <property name="configLocation" value="classpath:hibernate.cfg.xml" />
- </bean>
-
- <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate" >
- <property name="sessionFactory" ref="sessionFactory" />
- </bean>
-
- </beans>
<context:component-scan />指定Bean扫描的包,多个包逗号隔开,任何标注了@Component,@Controller,@Service,@Repository的类,都会被自动识别为bean。
<aop:aspectj-autoproxy />声明aspectj动态代理,启用注解驱动的aspectj配置。
<tx:annotation-driven />启用注解驱动的声明事务支持。
然后定义了sessionFactory和transactionManager,hibernateTemplate用来注入到Dao中,取代继承的方式使用spring对hibernate的集成支持。
三:hibernate配置
hibernate配置独立配置,方便修改。
Xml代码
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="14" height="15" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0">
<param name="flashvars" value="clipboard=%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22UTF-8%22%3F%3E%0A%3C!DOCTYPE%20hibernate-configuration%20PUBLIC%20%22-%2F%2FHibernate%2FHibernate%20Configuration%20DTD%203.0%2F%2FEN%22%20%22http%3A%2F%2Fhibernate.sourceforge.net%2Fhibernate-configuration-3.0.dtd%22%3E%0A%3Chibernate-configuration%3E%0A%09%3Csession-factory%3E%0A%09%09%3Cproperty%20name%3D%22hibernate.dialect%22%3Eorg.hibernate.dialect.MySQLDialect%3C%2Fproperty%3E%0A%09%09%3Cproperty%20name%3D%22hibernate.connection.driver_class%22%3Ecom.mysql.jdbc.Driver%3C%2Fproperty%3E%0A%09%09%3Cproperty%20name%3D%22hibernate.connection.url%22%3Ejdbc%3Amysql%3A%2F%2Flocalhost%3A3306%2Fsshdemo%3C%2Fproperty%3E%0A%09%09%3Cproperty%20name%3D%22hibernate.connection.username%22%3Eroot%3C%2Fproperty%3E%0A%09%09%3Cproperty%20name%3D%22hibernate.connection.password%22%3Eroot%3C%2Fproperty%3E%0A%09%09%3Cproperty%20name%3D%22hibernate.hbm2ddl.auto%22%3Eupdate%3C%2Fproperty%3E%0A%09%09%3Cproperty%20name%3D%22hibernate.show_sql%22%3Etrue%3C%2Fproperty%3E%0A%09%09%3Cproperty%20name%3D%22hibernate.format_sql%22%3Etrue%3C%2Fproperty%3E%0A%09%09%3Cproperty%20name%3D%22hibernate.current_session_context_class%22%3Ethread%3C%2Fproperty%3E%09%09%0A%0A%09%09%3C!--%20%E6%9C%80%E5%A4%A7%E8%BF%9E%E6%8E%A5%E6%95%B0%20--%3E%0A%09%09%3Cproperty%20name%3D%22hibernate.c3p0.max_size%22%3E20%3C%2Fproperty%3E%0A%20%20%20%20%20%20%20%20%3C!--%20%E6%9C%80%E5%B0%8F%E8%BF%9E%E6%8E%A5%E6%95%B0%20--%3E%0A%09%09%3Cproperty%20name%3D%22hibernate.c3p0.min_size%22%3E5%3C%2Fproperty%3E%0A%20%20%20%20%20%20%20%20%3C!--%20%E8%8E%B7%E5%BE%97%E8%BF%9E%E6%8E%A5%E7%9A%84%E8%B6%85%E6%97%B6%E6%97%B6%E9%97%B4%2C%E5%A6%82%E6%9E%9C%E8%B6%85%E8%BF%87%E8%BF%99%E4%B8%AA%E6%97%B6%E9%97%B4%2C%E4%BC%9A%E6%8A%9B%E5%87%BA%E5%BC%82%E5%B8%B8%EF%BC%8C%E5%8D%95%E4%BD%8D%E6%AF%AB%E7%A7%92%20--%3E%0A%09%09%3Cproperty%20name%3D%22hibernate.c3p0.timeout%22%3E120%3C%2Fproperty%3E%0A%20%20%20%20%20%20%20%20%3C!--%20%E6%9C%80%E5%A4%A7%E7%9A%84PreparedStatement%E7%9A%84%E6%95%B0%E9%87%8F%20--%3E%0A%09%09%3Cproperty%20name%3D%22hibernate.c3p0.max_statements%22%3E100%3C%2Fproperty%3E%0A%20%20%20%20%20%20%20%20%3C!--%20%E6%AF%8F%E9%9A%94120%E7%A7%92%E6%A3%80%E6%9F%A5%E8%BF%9E%E6%8E%A5%E6%B1%A0%E9%87%8C%E7%9A%84%E7%A9%BA%E9%97%B2%E8%BF%9E%E6%8E%A5%20%EF%BC%8C%E5%8D%95%E4%BD%8D%E6%98%AF%E7%A7%92--%3E%0A%09%09%3Cproperty%20name%3D%22hibernate.c3p0.idle_test_period%22%3E120%3C%2Fproperty%3E%0A%20%20%20%20%20%20%20%20%3C!--%20%E5%BD%93%E8%BF%9E%E6%8E%A5%E6%B1%A0%E9%87%8C%E9%9D%A2%E7%9A%84%E8%BF%9E%E6%8E%A5%E7%94%A8%E5%AE%8C%E7%9A%84%E6%97%B6%E5%80%99%EF%BC%8CC3P0%E4%B8%80%E4%B8%8B%E8%8E%B7%E5%8F%96%E7%9A%84%E6%96%B0%E7%9A%84%E8%BF%9E%E6%8E%A5%E6%95%B0%20--%3E%0A%09%09%3Cproperty%20name%3D%22hibernate.c3p0.acquire_increment%22%3E2%3C%2Fproperty%3E%0A%20%20%20%20%20%20%20%20%3C!--%20%E6%AF%8F%E6%AC%A1%E9%83%BD%E9%AA%8C%E8%AF%81%E8%BF%9E%E6%8E%A5%E6%98%AF%E5%90%A6%E5%8F%AF%E7%94%A8%20--%3E%0A%09%09%3Cproperty%20name%3D%22hibernate.c3p0.validate%22%3Etrue%3C%2Fproperty%3E%0A%0A%09%09%3Cmapping%20class%3D%22domain.Task%22%20%2F%3E%0A%09%3C%2Fsession-factory%3E%0A%3C%2Fhibernate-configuration%3E%0A">
<param name="src" value="http://www.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf">
<embed type="application/x-shockwave-flash" width="14" height="15" src="http://www.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22UTF-8%22%3F%3E%0A%3C!DOCTYPE%20hibernate-configuration%20PUBLIC%20%22-%2F%2FHibernate%2FHibernate%20Configuration%20DTD%203.0%2F%2FEN%22%20%22http%3A%2F%2Fhibernate.sourceforge.net%2Fhibernate-configuration-3.0.dtd%22%3E%0A%3Chibernate-configuration%3E%0A%09%3Csession-factory%3E%0A%09%09%3Cproperty%20name%3D%22hibernate.dialect%22%3Eorg.hibernate.dialect.MySQLDialect%3C%2Fproperty%3E%0A%09%09%3Cproperty%20name%3D%22hibernate.connection.driver_class%22%3Ecom.mysql.jdbc.Driver%3C%2Fproperty%3E%0A%09%09%3Cproperty%20name%3D%22hibernate.connection.url%22%3Ejdbc%3Amysql%3A%2F%2Flocalhost%3A3306%2Fsshdemo%3C%2Fproperty%3E%0A%09%09%3Cproperty%20name%3D%22hibernate.connection.username%22%3Eroot%3C%2Fproperty%3E%0A%09%09%3Cproperty%20name%3D%22hibernate.connection.password%22%3Eroot%3C%2Fproperty%3E%0A%09%09%3Cproperty%20name%3D%22hibernate.hbm2ddl.auto%22%3Eupdate%3C%2Fproperty%3E%0A%09%09%3Cproperty%20name%3D%22hibernate.show_sql%22%3Etrue%3C%2Fproperty%3E%0A%09%09%3Cproperty%20name%3D%22hibernate.format_sql%22%3Etrue%3C%2Fproperty%3E%0A%09%09%3Cproperty%20name%3D%22hibernate.current_session_context_class%22%3Ethread%3C%2Fproperty%3E%09%09%0A%0A%09%09%3C!--%20%E6%9C%80%E5%A4%A7%E8%BF%9E%E6%8E%A5%E6%95%B0%20--%3E%0A%09%09%3Cproperty%20name%3D%22hibernate.c3p0.max_size%22%3E20%3C%2Fproperty%3E%0A%20%20%20%20%20%20%20%20%3C!--%20%E6%9C%80%E5%B0%8F%E8%BF%9E%E6%8E%A5%E6%95%B0%20--%3E%0A%09%09%3Cproperty%20name%3D%22hibernate.c3p0.min_size%22%3E5%3C%2Fproperty%3E%0A%20%20%20%20%20%20%20%20%3C!--%20%E8%8E%B7%E5%BE%97%E8%BF%9E%E6%8E%A5%E7%9A%84%E8%B6%85%E6%97%B6%E6%97%B6%E9%97%B4%2C%E5%A6%82%E6%9E%9C%E8%B6%85%E8%BF%87%E8%BF%99%E4%B8%AA%E6%97%B6%E9%97%B4%2C%E4%BC%9A%E6%8A%9B%E5%87%BA%E5%BC%82%E5%B8%B8%EF%BC%8C%E5%8D%95%E4%BD%8D%E6%AF%AB%E7%A7%92%20--%3E%0A%09%09%3Cproperty%20name%3D%22hibernate.c3p0.timeout%22%3E120%3C%2Fproperty%3E%0A%20%20%20%20%20%20%20%20%3C!--%20%E6%9C%80%E5%A4%A7%E7%9A%84PreparedStatement%E7%9A%84%E6%95%B0%E9%87%8F%20--%3E%0A%09%09%3Cproperty%20name%3D%22hibernate.c3p0.max_statements%22%3E100%3C%2Fproperty%3E%0A%20%20%20%20%20%20%20%20%3C!--%20%E6%AF%8F%E9%9A%94120%E7%A7%92%E6%A3%80%E6%9F%A5%E8%BF%9E%E6%8E%A5%E6%B1%A0%E9%87%8C%E7%9A%84%E7%A9%BA%E9%97%B2%E8%BF%9E%E6%8E%A5%20%EF%BC%8C%E5%8D%95%E4%BD%8D%E6%98%AF%E7%A7%92--%3E%0A%09%09%3Cproperty%20name%3D%22hibernate.c3p0.idle_test_period%22%3E120%3C%2Fproperty%3E%0A%20%20%20%20%20%20%20%20%3C!--%20%E5%BD%93%E8%BF%9E%E6%8E%A5%E6%B1%A0%E9%87%8C%E9%9D%A2%E7%9A%84%E8%BF%9E%E6%8E%A5%E7%94%A8%E5%AE%8C%E7%9A%84%E6%97%B6%E5%80%99%EF%BC%8CC3P0%E4%B8%80%E4%B8%8B%E8%8E%B7%E5%8F%96%E7%9A%84%E6%96%B0%E7%9A%84%E8%BF%9E%E6%8E%A5%E6%95%B0%20--%3E%0A%09%09%3Cproperty%20name%3D%22hibernate.c3p0.acquire_increment%22%3E2%3C%2Fproperty%3E%0A%20%20%20%20%20%20%20%20%3C!--%20%E6%AF%8F%E6%AC%A1%E9%83%BD%E9%AA%8C%E8%AF%81%E8%BF%9E%E6%8E%A5%E6%98%AF%E5%90%A6%E5%8F%AF%E7%94%A8%20--%3E%0A%09%09%3Cproperty%20name%3D%22hibernate.c3p0.validate%22%3Etrue%3C%2Fproperty%3E%0A%0A%09%09%3Cmapping%20class%3D%22domain.Task%22%20%2F%3E%0A%09%3C%2Fsession-factory%3E%0A%3C%2Fhibernate-configuration%3E%0A"></embed></object>
- <?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>
- <property name="hibernate.dialect"font-weight: bold; color:
分享到:
相关推荐
集成Struts2、Spring和Hibernate时,需要注意配置文件的正确设置,包括Action配置、Spring Bean的定义、Hibernate的数据库连接和实体映射。同时,理解这三个框架的工作原理和相互作用,对于解决问题和优化代码至关...
**Struts2、Spring、Hibernate集成** 集成这三个框架可以形成强大的企业级应用开发平台。在实际项目中,通常使用Spring来管理Struts2的Action实例,通过Spring的DI实现Action类的依赖注入。同时,Spring负责管理...
6. 实体类(Entity):对应数据库表的Java类,使用Hibernate注解。 7. DAO接口及实现:处理数据库操作,例如UserDAO。 8. Service接口及实现:封装业务逻辑,例如UserService。 9. Action类:处理用户请求,例如...
本项目"Struts+Spring+Hibernate注解零配置整合"的目标是展示如何通过注解方式实现这三大框架的无缝集成,从而减少XML配置文件的使用,提高开发效率和代码可读性。 在传统的Struts、Spring和Hibernate整合中,大量...
SSH框架,即Struts2、Hibernate和Spring的组合,是Java Web开发中广泛使用的三大开源框架。它们各自负责Web应用程序的不同层面,通过整合可以提供一套完整的解决方案,实现MVC(Model-View-Controller)设计模式,...
2. 集成Spring:在Struts2中使用Spring插件,通过注解或XML配置将Action类交由Spring管理,实现依赖注入。同时,配置Spring的ApplicationContext,定义Bean和依赖关系。 3. 集成Hibernate:在Spring配置文件中配置...
开发者可能会在该文件中找到一个简单的Struts2 Action,一个带有注解的Spring Bean,以及一个用Hibernate注解配置的实体类。通过分析和运行这个`test`文件,可以更好地理解三大框架如何协同工作,以及注解如何简化...
【SSH 注解开发】是Java Web开发中一种常见的技术栈组合,由Struts2、Hibernate和Spring框架共同构建。这个学生信息管理系统就是基于这种技术栈,利用注解的方式来简化配置,提高开发效率。 Struts2作为MVC(模型-...
Struts2、Hibernate和...总的来说,Struts2、Hibernate和Spring的注解整合提供了更简洁、高效的开发方式,减少了XML配置,提高了代码可读性。通过理解并熟练掌握这些注解,开发者能更轻松地构建复杂的企业级应用。
在这个"Spring3+Struts2+Hibernate3 全注解集成开发"项目中,我们将深入探讨如何使用这三个框架的注解版本进行集成,以实现对Emp表的操作。 首先,Spring3是依赖注入(DI)和面向切面编程(AOP)的优秀框架。在全...
通过这个登录实例,你可以学习到如何在实际项目中集成Struts2、Spring和Hibernate,理解它们之间的协作方式,以及如何编写和配置相关的代码。同时,这也将帮助你掌握Java Web开发的基础知识,提高解决实际问题的能力...
Struts2、Spring3和Hibernate是Java开发中的三大框架,它们的集成使用极大地提升了Web应用的开发效率和可维护性。在这个项目"SSHWithAnnotationDemo"中,开发者利用注解功能,DAO泛型以及通用分页技术,进一步优化了...
Struts2.3.28、Spring4.1.6和Hibernate...以上就是Struts2.3.28、Spring4.1.6和Hibernate4.3.8整合的关键知识点,以及注解在整合中的作用。理解并熟练掌握这些内容,能够帮助开发者更有效地构建和维护Java EE应用程序。
5. **整合步骤**:在Struts2的Action中,通过Spring的Autowired注解注入需要的服务类,这些服务类通常会持有Hibernate的SessionFactory,从而完成DAO层的数据库操作。 6. **测试与运行**:在MyEclipse8.0环境下,将...
在IT行业中,SSH(Spring、Struts2、Hibernate)是一个经典的Java Web开发框架组合,而Redis则是一个高性能的键值存储系统,常用于缓存和数据持久化。将SSH与Redis整合,可以提升应用程序的性能和响应速度。下面将...
`struts2-spring-plugin.xml`配置Struts2与Spring的集成,确保Action类由Spring容器管理。`spring-context.xml`中,需要配置数据源、SessionFactory、事务管理器以及各业务层和DAO层的bean。Hibernate的`hibernate....
在这个特定的项目中,开发者选择了SSH2的特定版本:Struts2.1.6、Spring2.5.6和Hibernate3.3,并且强调了全注解开发,这意味着在配置文件中尽可能地使用注解来代替XML配置。 首先,让我们详细了解一下这三个框架的...