ssh整合步驟之一(搭建環境)

ssh整合主要可以分爲3個步驟:搭建環境、設計架構、實現邏輯

以下是搭建環境的步驟

1、導入jar包

      導入ssh基本jar包

2、導入ssh配置文件。

      包括(struts.xml   hibernate.cfg.xml   `````.hbm.xml    applicationContext.xml   jdbc/properties)

3、整合strut與spring

     1)web.xml

      在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的用於解決懶加載問題的過濾器 -->
	<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>*.action</url-pattern>
	</filter-mapping>
	
	

4、整合spring與hibernate

   1)在applicationContext.xml中加上以下代碼:

   

<context:component-scan base-package="com.njupt"></context:component-scan>
        
        <context:property-placeholder location="classpath:jdbc.properties" />
        
        <bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<!-- 指定hibernate的配置文件位置 -->
		<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
		<!-- 配置c3p0數據庫連接池 -->
		<property name="dataSource">
			<bean class="com.mchange.v2.c3p0.ComboPooledDataSource">
				<!-- 數據連接信息 -->
				<property name="jdbcUrl" value="${jdbcUrl}"></property>
				<property name="driverClass" value="${driverClass}"></property>
				<property name="user" value="${user}"></property>
				<property name="password" value="${password}"></property>
				<!-- 其他配置 -->
				<!--初始化時獲取三個連接,取值應在minPoolSize與maxPoolSize之間。Default: 3 -->
				<property name="initialPoolSize" value="3"></property>
				<!--連接池中保留的最小連接數。Default: 3 -->
				<property name="minPoolSize" value="3"></property>
				<!--連接池中保留的最大連接數。Default: 15 -->
				<property name="maxPoolSize" value="5"></property>
				<!--當連接池中的連接耗盡的時候c3p0一次同時獲取的連接數。Default: 3 -->
				<property name="acquireIncrement" value="3"></property>
				<!--
					控制數據源內加載的PreparedStatements數量。如果maxStatements與maxStatementsPerConnection均爲0,則緩存被關閉。Default:
					0
				-->
				<property name="maxStatements" value="8"></property>
				<!--
					maxStatementsPerConnection定義了連接池內單個連接所擁有的最大緩存statements數。Default: 0
				-->
				<property name="maxStatementsPerConnection" value="5"></property>
				<!--最大空閒時間,1800秒內未使用則連接被丟棄。若爲0則永不丟棄。Default: 0 -->
				<property name="maxIdleTime" value="1800"></property>
			</bean>
		</property>
	</bean>
	
	
	<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<tx:annotation-driven transaction-manager="txManager"/>
	
	

到這裏ssh就整合完了,以下是一些方便使用的功能:

1)hibernate.cfg.xml中貼上

<property name="hbm2ddl.auto">update</property> 
以上代碼完成自動建表功能

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章