上班一段时间了,在公司用的是ssi框架。大概知道了一点原理。所以就想自己也整一个ssh框架出来。通过最近的学习,终于有点眉目了。下面我就从搭建环境开始。

1,下载环境所需要的包,但是我们只需要其中的一部分jar包。下面是搭建好的框架所需要的所有jar包。

 

 

2.首先介绍hibernate和spring的整合。

   整合过程中可以将hibernate的配置文件hibernate.cfg.xml去了,他的一些配置文件直接写在spring的applicationContext.xml中。下面贴上我的applicationContext.xml的配置

<?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:context="http://www.springframework.org/schema/context"
      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.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    <!-- 打开注解 -->
    <context:annotation-config />
    <!-- 打开自动扫描 -->
    <context:component-scan base-package="com.hoperun" />
    <!-- 配置数据源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" value="com.mysql.jdbc.Driver" /> 
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/oa?useUnicode=true&amp;characterEncoding=UTF-8" /> 
        <property name="user" value="root" /> 
        <property name="password" value="123" /> 
        <!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 --> 
        <property name="initialPoolSize" value="1" /> 
        <!--连接池中保留的最小连接数。--> 
        <property name="minPoolSize" value="1" /> 
        <!--连接池中保留的最大连接数。Default: 15 --> 
        <property name="maxPoolSize" value="300" /> 
        <!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 --> 
        <property name="maxIdleTime" value="60" /> 
        <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 --> 
        <property name="acquireIncrement" value="5" /> 
        <!--每60秒检查所有连接池中的空闲连接。Default: 0 --> 
        <property name="idleConnectionTestPeriod" value="60" /> 
    </bean>
    <!-- 配置sessionFactory -->
    <bean id="sessionFactory" 
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
        <property name="dataSource" ref="dataSource" /> 
        <!-- 映射文件的路径 -->
        <property name="mappingResources"> 
            <list> 
                <value>com/hoperun/login/entity/LoginEntity.hbm.xml</value>
            </list> 
        </property> 
        <property name="hibernateProperties"> 
            <props> 
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 
                <prop key="hibernate.hbm2ddl.auto">create</prop> 
                <prop key="hibernate.show_sql">true</prop> 
                <prop key="hibernate.format_sql">true</prop>
            </props> 
        </property> 
    </bean> 
    <!-- hibernate的事务管理器 -->
 <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
     <property name="sessionFactory">
      <ref bean="sessionFactory"/>
     </property>
 </bean>
    <!-- hibernate的事务传播特性 -->
    <tx:advice id="txadvice" transaction-manager="transactionManager">
        <tx:attributes>
         <tx:method name="add*" propagation="REQUIRED"/>
         <tx:method name="delete*" propagation="REQUIRED"/>
         <tx:method name="modify*" propagation="REQUIRED"/>
         <tx:method name="*" propagation="NOT_SUPPORTED" read-only="true"/>
        </tx:attributes>
    </tx:advice>
    <!-- 哪些类的方法参与事务 -->
    <aop:config>
     <aop:pointcut id="pointcut" expression="execution(* com.hoperun..*.service..*.*(..))"/>
     <aop:advisor pointcut-ref="pointcut" advice-ref="txadvice"/>
    </aop:config>
    <aop:aspectj-autoproxy proxy-target-class="true"/>
   
</beans>
3,然后整合struts2和spring

  主要是在web.xml中进行配置,下面贴上web.xml的配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath*:applicationContext*.xml</param-value>
 </context-param>
 <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
 <filter>
  <filter-name>OpenSessionInViewFilter</filter-name>
  <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
 </filter>
 <filter-mapping>
  <filter-name>OpenSessionInViewFilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
 <filter>
  <filter-name>struts2</filter-name>
  <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
 </filter>
 <filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
</web-app>
 

这样配置文件就完成了。

下面介绍以下其中以下配置文件的意思

web.xml中的OpenSessionInViewFilter过滤器是为了解决hibernate的session生命周期过短的问题,这个要结合事务来。applicationContext.xml中的pointcut指明了哪些类中的方法在事务中处理,这个类下面的方法可能有很多单独的dao操作,所以要在一个session范围内,那么就最好使用OpenSessionInViewFilter过滤器来处理。让session处在一个threadLocal中。

我的框架层次是分为三层:action作为控制层,service层负责处理业务,必须在事务中完成,dao层负责跟数据库打交道。