Struts2、Spring2.5、Hibernate3整合实例
这两天利用空余时间做了一个Struts2、Spring2.5、Hibernate3的整合实例工程,其实也遇到了一些问题,但网络就是强大,不一会工夫就解决了,现在整个整合过程总结如下:以备后忘!
首先,还是分别到Struts2、Spring2.5、Hibernate3的官方网站上下载资源包,其中大概都是所需框架的jar包,和其应用实例。
其次:
【Struts2配置】:
①在Eclipse(J2EE版)中建立一个动态工程,完全不需要MyEclipse插件的支持,因为所需要的框架jar包等,我们都从网上下载下来了。
②将struts2的jar包复制粘贴进WEB-INF/lib目录,再将struts2的样例程序中的struts.xml复制到工程的src目录下。
③配置web.xml,将struts2的配置信息编写好如下:
<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>/*</url-pattern> </filter-mapping>
④根据工程情况,编写struts.xml文件如下:
<action name="findall" method="execute"> <result name="success">/views/success.html</result> <result name="error">/views/error.html</result></action>
⑤此时我们的struts2配置已大功告成,下面来Spring的配置>>待继...②③④⑤⑥⑦⑧⑨⑩
【Spring2.5配置】
①将Spring2.5的框架资源jar包,复制粘贴入WEB-INF/lib目录下,将WEB-INF中的任意目录新建一个spring的bean配置文件applicationContext.xml(当然这个文件名可以任意取),也可以直接到sping的示例包中,复制一个applicationContext.xml到工程中来,然后依照内容格式进行bean的配置和依赖关系注入。
②在applicationContext.xml中,除了完成对bean的配置管理外,还要完成对hibernate的集成配置,如:sessionFactory等...配置文件如下:
<?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:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"><bean id="dataSource" value="oracle.jdbc.driver.OracleDriver"></property><property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl"></property><property name="username" value="scott"></property><property name="password" value="orcl"></property></bean><bean id="sessionFactory" ref="dataSource"></property><property name="hibernateProperties"><props><prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop><prop key="hibernate.show_sql">true</prop></props></property><property name="mappingResources"><list><!-- 以下用来列出所有的PO映射文件--><value>com/rojs/bean/Person_Book.hbm.xml</value></list></property></bean><!-- hibernate 模板,用来代替 sessionFactory --><bean id="hibernateTemplate" ref="sessionFactory"/><property name="cacheQueries" value="true"/></bean><bean id="transactionManager" ref="dataSource"/><property name="sessionFactory" ref="sessionFactory"/></bean><bean id="pbookDao" scope="singleton"><property name="sessionFactory"><ref bean="sessionFactory"/></property></bean><bean id="pbookService" ref="pbookDao"></property></bean><bean id="findAllAction" scope="prototype"><property name="pbservice" ref="pbookService"></property></bean></beans>
?③配置好spring的applicationContext.xml后,还需在web.xml中加以配置spring,才能让服务在启动时去读取applicationContext.xml文件,以便对整个bean工厂的初始化,web.xml中添加如:
<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/SpringConf/applicationContext.xml</param-value> </context-param> <listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>
【Hibernate3配置】
①将hibernate3的资源jar包,复制粘贴入WEB-INF/lib目录中。
?
②配置好类和数据库表的关系映射文件xxxhbm.xml文件,并将此文件在spring的appliactionContext.xml文件的<sessionFactory>中加以关联配置,以便读取些映射文件,配置如下:
<bean id="sessionFactory" ref="dataSource"></property><property name="hibernateProperties"><props><prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop><prop key="hibernate.show_sql">true</prop></props></property><property name="mappingResources"><list><!-- 以下用来列出所有的PO映射文件--><value>com/rojs/bean/Person_Book.hbm.xml</value></list></property></bean>
?
③此时的hibernate3配置也就完成了。
最后:整个Struts2、Spring2.5、Hibernate3的框架整合配置就完成了。
以下是整合过程中,使用到的框架资源jar包截图如下:
?? ? ? ? ? ? ? ?