上班一段時間了,在公司用的是ssi框架。大概知道了一點原理。因此就想本身也整一個ssh框架出來。經過最近的學習,終於有點眉目了。下面我就從搭建環境開始。java
1,下載環境所須要的包,可是咱們只須要其中的一部分jar包。下面是搭建好的框架所須要的全部jar包。mysql
2.首先介紹hibernate和spring的整合。web
整合過程當中能夠將hibernate的配置文件hibernate.cfg.xml去了,他的一些配置文件直接寫在spring的applicationContext.xml中。下面貼上個人applicationContext.xml的配置spring
<?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&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和springsql
主要是在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>
express
這樣配置文件就完成了。apache
下面介紹如下其中如下配置文件的意思session
web.xml中的OpenSessionInViewFilter過濾器是爲了解決hibernate的session生命週期太短的問題,這個要結合事務來。applicationContext.xml中的pointcut指明瞭哪些類中的方法在事務中處理,這個類下面的方法可能有不少單獨的dao操做,因此要在一個session範圍內,那麼就最好使用OpenSessionInViewFilter過濾器來處理。讓session處在一個threadLocal中。app
個人框架層次是分爲三層:action做爲控制層,service層負責處理業務,必須在事務中完成,dao層負責跟數據庫打交道。