集成原理:Spring管理Struts 和 Hibernate
搭建顺序先搭建Struts1.2 Hibernate3.2 Spring2.0 配置文件,再集成。
- hibernate 3.2搭建文件
a)配置文件hibernate.cfg.xml (数据源配置文件) log4j.properties (日志文件)
hibernate.cfg.xml sessionFactory 属性:
<property name="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.username">BAM2012</property>
<property name="hibernate.connection.password">BAM2012</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:ORCL</property>
<property name="show_sql">true</property>
<mapping resource="com/hibernate3/demo/Test.hbm.xml" />
b)添加jar文件
核心包hibernate3.jar 及lib文件下面全部jar包 和 class数据库驱动包
c) hibernate.cfg.xml的路径不需要在web.xml文件中指定路径
Configuration cfg=new Configuration().configure();在单独实用hibernate的时候configure()自动会找到src目录下的该文件;在spring集成中则在spring的配置文件中加入指定路径。
- spring 2.0 搭建文件
a)添加spring.jar log4j-1.2.14.jar commons-logging.jar aspectjrt.jar aspectjweaver.jar
b)添加applicationContext.xml 文件
- struts 1.2 搭建文件
a)添加jar包(7个) struts.jar antlr.jar commons-beanutils.jar commons-digester.jar
commons-fileupload.jar commons-logging.jar commons-validator.jar jakarta-oro.jar
b)配置ActionServlet (action控制器)
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
过滤所有的do 操作,转入对应的action实例中
c)添加action 和 actionForm 实例并配置到struts-config.xml文件中
actionForm作为表单提交后的数据封装类型,通过struts-config.xml将表单传递到对应的action中进行操作。
配置文件整理:
1.Web.xml tomcat启动配置文件:配置struts 配置文件路径 和 spring配置文件路径
2.Struts-config.xml struts配置文件
(代理类型 className=org.apache.struts.validator.ValidatorPlugIn 验证插件代理类
type= org.apache.struts.action.DynaActionForm表单代理类型
type= org.springframework.web.struts.DelegatingActionProxy action代理类型)
3.applicationContext.xml spring配置文件
a).applicationContext-action.xml 配置action 管理struts配置文件
b).applicationContext-beans.xml 配置 业务逻辑类 DAO数据层 管理业务配置文件
c).applicationContext-common.xml 配置 hibernate 或 连接池 管理 数据库连接配置文件
(org.springframework.orm.hibernate3.LocalSessionFactoryBean
sessionFactory Spring管理hibernate sessionFactory类
)
<!-- 配置事务管理器 -->
<!-- 配置事务的传播特性 -->
<!-- 那些类的哪些方法参与事务 -->
- hibernate.cfg.xml hibernate配置文件 配置sessionFactoy类容
- proxool.xml 连接池配置文件(若配置连接池则可以不配置hibernate.cfg.xml文件)
(数据源代理类 org.logicalcobwebs.proxool.ProxoolDataSource)
数据源部分
ApplicationContext-common.xml
Spring框架管理SessionFactory。
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" /> 数据源bean
</property>
<property name="mappingResources">数据库模型映射文件路径
<list>
<value>
/com/index/report/model/SpecialThemeFn.hbm.xml
</value>
</list>
</property>
<property name="hibernateProperties">hibernate配置信息
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="connection.autocommit">true</prop>
<!-- 解决"ClassNotFoundException: org.hibernate.hql.ast.HqlToken"异常
<prop key="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory
</prop>
解决"java.sql.SQLException: 不允许的操作: streams type cannot be used in batching" -->
<prop key="hibernate.jdbc.batch_size">100</prop>
</props>
</property>
</bean> 链接池数据源配置 proxool-0[1].9.1.jar proxool-cglib.jar
<bean id="dataSource"
class="org.logicalcobwebs.proxool.ProxoolDataSource">
<property name="driver">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="driverUrl">
<value>jdbc:oracle:thin:@10.25.xx.xx:1521:xxxxxx</value>
</property>
<property name="user" value="xxxxx"/>
<property name="password" value="xxxxxx"/>
<property name="alias" value="proxool.conn"/>
<property name="maximumActiveTime" value="100000"/>
<property name="prototypeCount" value="2"/>
<property name="maximumConnectionCount" value="1000"/>
<property name="minimumConnectionCount" value="2"/>
<property name="simultaneousBuildThrottle" value="20"/>
<property name="houseKeepingTestSql" value="select CURRENT_DATE"/>
</bean>
proxool.xml 文件
<?xml version="1.0" encoding="UTF-8"?>
<!-- the proxool configuration can be embedded within your own application's.
Anything outside the "proxool" tag is ignored. -->
<something-else-entirely>
<proxool>
<alias>conn</alias> 实例名
<driver-url>jdbc:oracle:thin:@10.25.xx.xx:1521:xxxxx</driver-url>
<driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
<driver-properties>
<property name="user" value="xxxxxx" />
<property name="password" value="xxxxxx" />
</driver-properties>
<maximum-connection-count>10000</maximum-connection-count>
<minimum-connection-count>10</minimum-connection-count>
<house-keeping-sleep-time>90000</house-keeping-sleep-time>
<maximum-new-connections>10000</maximum-new-connections>
<prototype-count>5</prototype-count>
<test-before-use>true</test-before-use>
<house-keeping-test-sql>select sysdate from dual</house-keeping-test-sql>
</proxool>
</something-else-entirely>
Web.xml文件配置(加载连接池)
<servlet>
<servlet-name>proxoolServletConfigurator</servlet-name>
<servlet-class>org.logicalcobwebs.proxool.configuration.ServletConfigurator</servlet-class>
<init-param>
<param-name>xmlFile</param-name>
<param-value>/WEB-INF/proxool.xml</param-value> <!--配置文件信息-->
</init-param>
<load-on-startup>1</load-on-startup>//优先加载
</servlet>
<servlet>
<servlet-name>proxooladmin</servlet-name>
<servlet-class>org.logicalcobwebs.proxool.admin.servlet.AdminServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>proxooladmin</servlet-name>
<url-pattern>/proxooladmin</url-pattern><!-- proxool的管理界面可以看到其的使用情况-->
</servlet-mapping>
一份完整的 web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name>
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>singleSession </param-name>
<param-value>false</param-value>
</init-param>
</filter>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>
org.apache.struts.action.ActionServlet
</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<filter>
<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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>proxoolServletConfigurator</servlet-name>
<servlet-class>org.logicalcobwebs.proxool.configuration.ServletConfigurator</servlet-class>
<init-param>
<param-name>xmlFile</param-name>
<param-value>/WEB-INF/proxool.xml</param-value> <!--配置文件信息-->
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>proxooladmin</servlet-name>
<servlet-class>org.logicalcobwebs.proxool.admin.servlet.AdminServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>proxooladmin</servlet-name>
<url-pattern>/proxooladmin</url-pattern><!-- proxool的管理界面可以看到其的使用情况-->
</servlet-mapping>
<!-- http://localhost:8080/webname/proxooladmin 可以打开后管理界面 -->
<session-config>
<session-timeout>30</session-timeout>
</session-config>
</web-app>