手动搭建SSH框架Struts2+Spring4+Hibernate4
1.搭建Struts2配置环境
所需jar包
(1)首先在web.xml中加入Struts的过滤器
<span style="white-space:pre"> </span><<span style="font-size:18px;">!-- 配置 Struts2框架的核心Filter--> <filter> <!-- 核心Filter的名字 --> <filter-name>struts2</filter-name> <!-- 核心Filter的实现类 --> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <span style="white-space:pre"> </span><!-- 配置Filter拦截的URL --> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping></span>
(2)创建struts.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
<span style="white-space:pre"> </span>"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
<span style="white-space:pre"> </span>"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<span style="white-space:pre"> </span><!-- 配置全局错误页面 -->
<global-results>
<result name="error">/WEB-INF/jsp/error.jsp</result>
</global-results>
<span style="white-space:pre"> </span><!-- 配置异常处理 -->
<global-exception-mappings>
<exception-mapping exception="java.lang.Exception" result="error"/>
</global-exception-mappings>
<span style="white-space:pre"> </span><!-- 配置用来处理请求的action -->
<action name="index">
<result type="redirectAction">
<param name="actionName">HelloWorld</param>
<param name="namespace">/example</param>
</result>
</action>
</package>
</struts>
2.搭建Hibernate环境
所需jar包
创建hibernate.cfg.xml文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC <span style="white-space:pre"> </span>"-//Hibernate/Hibernate Configuration DTD 3.0//EN" <span style="white-space:pre"> </span>"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <span style="white-space:pre"> </span><!-- 配置数据库方言 --> <span style="white-space:pre"> </span><property name="hibernate.dialect"> org.hibernate.dialect.MySQL5InnoDBDialect</property> <span style="white-space:pre"> </span><!-- 输出运行时生成的SQL语句 --> <span style="white-space:pre"> </span><property name="hibernate.show_sql">true</property> <span style="white-space:pre"> </span><!-- 格式或SQL输出语句 --> <span style="white-space:pre"> </span><property name="hibernate.format_sql">true</property> <span style="white-space:pre"> </span><!-- 根据映射文件自动验证数据库表结构或自动创建自动更新数据库表结构 --> <span style="white-space:pre"> </span><property name="hibernate.hbm2ddl.auto">update</property> </session-factory> </hibernate-configuration>
3.搭建Spring环境
创建applicationContext.xml文件
</pre></div></div><pre><?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
<span style="white-space:pre"> </span>xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<span style="white-space:pre"> </span>xmlns:context="http://www.springframework.org/schema/context"
<span style="white-space:pre"> </span>xmlns:tx="http://www.springframework.org/schema/tx"
<span style="white-space:pre"> </span>xmlns:aop="http://www.springframework.org/schema/aop"
<span style="white-space:pre"> </span>xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
<span style="white-space:pre"> </span>http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
<span style="white-space:pre"> </span>http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
<span style="white-space:pre"> </span>http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<span style="white-space:pre"> </span><!-- 导入资源文件 -->
<span style="white-space:pre"> </span><context:property-placeholder location="classpath:db.properties" />
<span style="white-space:pre"> </span><!-- 数据源配置 -->
<span style="white-space:pre"> </span><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<span style="white-space:pre"> </span><property name="user" value="${jdbc.user}"></property>
<span style="white-space:pre"> </span><property name="password" value="${jdbc.password}"></property>
<span style="white-space:pre"> </span><property name="driverClass" value="${jdbc.driverClass}"></property>
<span style="white-space:pre"> </span><property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<span style="white-space:pre"> </span><property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
<span style="white-space:pre"> </span><property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
<span style="white-space:pre"> </span></bean>
<span style="white-space:pre"> </span><!-- 配置SessionFactory -->
<span style="white-space:pre"> </span><bean id="sessionFactory"
<span style="white-space:pre"> </span>class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<span style="white-space:pre"> </span><property name="dataSource" ref="dataSource"></property>
<span style="white-space:pre"> </span><property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<span style="white-space:pre"> </span><property name="mappingLocations" value="classpath:com/atguigu/ssh/entities/*.hbm.xml"></property>
<span style="white-space:pre"> </span></bean>
<span style="white-space:pre"> </span><!-- 配置Spring的声明式事务 -->
<span style="white-space:pre"> </span><!-- 配置hibernate事务管理器 -->
<span style="white-space:pre"> </span><bean id="transactionManager"
<span style="white-space:pre"> </span>class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<span style="white-space:pre"> </span><property name="sessionFactory" ref="sessionFactory"></property>
<span style="white-space:pre"> </span></bean>
<span style="white-space:pre"> </span><!-- 配置事务属性 -->
<span style="white-space:pre"> </span><tx:advice id="txAcvice" transaction-manager="transactionManager">
<span style="white-space:pre"> </span><tx:attributes>
<span style="white-space:pre"> </span><tx:method name="get*" read-only="true" />
<span style="white-space:pre"> </span><tx:method name="lastNameIsValid" read-only="true"/>
<span style="white-space:pre"> </span><tx:method name="*" />
<span style="white-space:pre"> </span></tx:attributes>
<span style="white-space:pre"> </span></tx:advice>
<span style="white-space:pre"> </span><!-- 配置事务切入点 -->
<span style="white-space:pre"> </span><aop:config>
<span style="white-space:pre"> </span><aop:pointcut expression="execution(* com.atguigu.ssh.service.*.*(..) )" id="txPointcut"/>
<span style="white-space:pre"> </span><aop:advisor advice-ref="txAcvice" pointcut-ref="txPointcut"/>
<span style="white-space:pre"> </span></aop:config>
</beans>
</pre><h3><span style="white-space:pre"></span>编辑web.xml添加Spring监听器</h3></div></div><div><pre name="code" class="html"><span style="white-space:pre"> </span><context-param>
<span style="white-space:pre"> </span><param-name>contextConfigLocation</param-name>
<span style="white-space:pre"> </span><param-value>classpath:applicationContext*.xml</param-value>
<span style="white-space:pre"> </span></context-param>
<span style="white-space:pre"> </span><!-- Bootstraps the root web application context before servlet initialization -->
<span style="white-space:pre"> </span><listener>
<span style="white-space:pre"> </span><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
<span style="white-space:pre"> </span></listener>