SSH(Spring4+Struts2+Hibernate4)框架整合
1.加入Spring4
①. 加入 jar 包
②. 配置 web.xml 文件
<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>
③. 加入 Spring 的配置文件applicationContext.xml
2. 加入 Hibernate
①. 加入 jar 包
②. 在类路径下加入 hibernate.cfg.xml 文件, 在其中配置 hibernate 的基本属性
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- 配置 hibernate 的基本属性 --> <!-- 方言 --> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 是否显示sql并且格式化 --> <property name="hibernate.show_sql">true</property> <property name="hibernate.format_sql">true</property> <!-- 生成数据表的策略 --> <property name="hibernate.hbm2ddl.auto">update</property> <!-- 配置 hibernate 二级缓存相关的属性. --> </session-factory> </hibernate-configuration>
③. 建立持久化类, 和其对应的 .hbm.xml 文件(略)
④. 和 Spring 进行整合
i. 加入 c3p0 和 MySQL 的驱动包
ii. 在 Spring 的配置文件中配置: 数据源, SessionFactory, 声明式事务
<!-- 导入属性文件 --> <context:property-placeholder location="classpath:db.properties" /> <!-- 配置c3p0数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="user" value="${user}"></property> <property name="password" value="${password}"></property> <property name="jdbcUrl" value="${url}"></property> <property name="driverClass" value="${driverclass}"></property> <property name="initialPoolSize" value="${initPoolSize}"></property> <property name="maxPoolSize" value="${maxPoolSize}"></property> </bean> <!-- 配置hibernate的SessionFactory实例 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> <property name="mappingLocations" value="classpath:com/atguigu/ssh/entities/*.hbm.xml"></property> </bean> <!-- 配置Spring声明时事务 --> <!-- 配置Hibernate事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 配置事务属性 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="get*" read-only="true"/> <tx:method name="*"/> </tx:attributes> </tx:advice> <!-- 配置事务的切入点,并将事务的属性与切入点关联起来 --> <aop:config> <aop:pointcut expression="execution(* com.atguigu.ssh.service.*.*(..))" id="txPointCut"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/> </aop:config>
⑤. 启动项目, 会看到生成对应的数据表
3. 加入 Struts2
①. 加入 jar 包:
注意:要加入一个与Spring关联的包
若有重复的 jar 包, 则需要删除版本较低的. javassist-3.11.0.GA.jar
②. 在 web.xml 文件中配置 Struts2 的 Filter
<!-- 配置 Struts2 的 Filter --> <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>
③. 在 Spring 的配置文件中正常配置 Action
注意: Action 的 scope 为 prototype
<bean id="employeeAction" class="com.atguigu.ssh.action.EmployeeAction" scope="prototype"> <property name="employeeService" ref="employeeService"></property> </bean>
④. 加入 Struts2 的配置文件
注意:action中的class必须是Spring IOC容器(applicationContext.xml)中的bean
<?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"> <struts> <package name="default" namespace="/" extends="struts-default"> <action name="" class="employeeAction" method=""> </action> </package> </struts>