搭建Struts2+Spring+Hibernate三大框架的步骤

来源:互联网 发布:mac面对面快传 编辑:程序博客网 时间:2024/05/17 14:25

                            SSH三大框架整合步骤

准备工作:导入ssh三大框架所需的jar包,创建struts.xml和applicationContext.xml文件。

 

applicationContext.xml文件的头部标签

 

1、先搭建struts框架

1-1:在web.xml文件中配置过滤器和映射。

<!-- 添加struts的过滤器 -->

  <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>

1-2:在struts.xml文件中配置packet和action

2、搭建spring框架

2-1:创建applicationContext.xml文件,为配置相应的bean做准备

2-2:在web.xml文件中配置applicationContext.xml文件和spring的监听

<!-- 配置applicationContext.xml文件 -->

  <context-param>

   <param-name>contextConfigLocation</param-name>

   <param-value>classpath:applicationContext.xml</param-value>

  </context-param>

  <!-- 配置spring的监听 -->

  <listener>

   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

  </listener>

3、搭建hibernate框架

第一步: 创建三层模型

第二步: 在applicationContext.xml文件中配置Hibernate框架

1、配置dataSource

1-1:配置驱动

1-2:URL

1-3:用户名

1-4:密码

1-5:连接池的配置(可选)

具体代码如下

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">

<!-- 配置驱动 -->

<property name="driverClass">

<value>数据库的驱动</value>

</property>

<!-- URL -->

<property name="jdbcUrl">

<value>数据库的URL</value>

</property>

<!-- 用户名-->

<property name="user">

<value>数据库用户名</value>

</property>

<!-- 密码-->

<property name="password">

<value>数据库密码</value>

</property>

<!-- 连接池的配置 -->

<property name="maxPoolSize">

<value>80</value>

</property>

<property name="minPoolSize">

<value>1</value>

</property>

<property name="initialPoolSize">

<value>1</value>

</property>

<property name="maxIdleTime">

<value>20</value>

</property>

</bean>

 

2、配置sessionFactory

        2-1:引用数据源bean

2-2:添加映射文件

2-3:配置属性信息

 

 具体代码如下

<!-- 配置sessionFactory -->

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

<!-- 引用数据源bean -->

<property name="dataSource" ref="dataSource"/>

<!-- 添加映射文件 -->

<property name="mappingResources">

<list>

<value>com/caokaiyuan/domain/User.hbm.xml</value>

</list>

</property>

<!-- 配置属性信息 -->

<property name="hibernateProperties">

<props>

<prop key="hibernate.dialect">方言</prop>

<prop key="hibernate.show_sql">true</prop>

<prop key="hibernate.hbm2ddl.auto">create</prop>

<prop key="hibernate.jdbc.batch_size">20</prop>

</props>

</property>

</bean>

 

 

 

3、配置事务

3-1:配置的管理器

3-2:配置事务的传播特性

3-3:AOP切面编程来整合事务

具体代码如下

<!-- 配置事务 -->

<!-- 配置的管理器 -->

<bean id="transactionManager"

class="org.springframework.orm.hibernate3.HibernateTransactionManager">

<property name="sessionFactory" ref="sessionFactory" />

</bean>

<!-- 配置事务的传播特性 -->

<tx:advice id="txAdvice">

<tx:attributes>

<tx:method name="get*" read-only="true" />

<tx:method name="find*" read-only="true" />

<tx:method name="query*" read-only="true" />

<tx:method name="*" propagation="REQUIRED" />

</tx:attributes>

</tx:advice>

<!-- AOP切面编程来整合事务  -->

<aop:config>

<aop:advisor pointcut="execution(* com.lanqiao.service.impl.*Service*.*(..))"

advice-ref="txAdvice" />

</aop:config>

 

4、配置dao层

5、配置service层

6、配置action对象

0 0