从0开始学习ssh之搭建环境

ssh即struts+spring+Hibernate,从头开始学习这个框架。

struts环境配置,首先在apps目录下找到struts2-blank-xxx.war这个文件,这是已经发布好的war包。其中找到lib文件夹,添加其中所有jar到网站lib下面。再找到src/java下面的struts.xml文件,添加到网站的src文件夹中。打开web.xml拷贝struts的核心配置到网站的web.xml,核心配置如下

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

之后,修改struts.xml

<struts>
	<!-- 开发模式 -->
    <constant name="struts.devMode" value="true" />
	<!-- 扩展名配成action -->
	<constant name="struts.action.extension" value="action" />
	<!-- 主题配置为simple -->
	<constant name="struts.ui.theme" value="simple" />
	
	
    <package name="default" namespace="/" extends="struts-default">
    </package>

    <!-- Add packages here -->

</struts>

配置了开发模式,扩展名为action,以及模板为simple

以上就是struts2的配置,之后自己写aciton部分就可以

hibernate配置

添加和ibernate3.jar lib文件夹下required的所有,jpa下的所有,optional下c3p0的所有(数据库连接池),mysql-connector-java-5.1.5-bin,然后把log4j.properties(日志文件)、hibernate.cfg.xml(配置文件),放到网站src目录下.

hibernate.cfg.xml如下

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>

	<!-- 1,数据库连接信息 -->
	<property name="dialect">
		org.hibernate.dialect.MySQL5InnoDBDialect
	</property>
	<!-- 
		<property name="connection.url">jdbc:mysql:///数据库名</property>
		<property name="connection.driver_class">com.jdbc.mysql.Driver</property>
		<property name="connection.username">数据库用户名</property>
		<property name="connection.password">数据库密码</property>
	-->

	<!-- 2,其他配置 -->
	<property name="show_sql">true</property>
	<property name="hbm2ddl.auto">update</property>

	<!-- 3,导入映射文件 -->
	<mapping resource="cn/itcast/oa/domain/User.hbm.xml" />


</session-factory>

</hibernate-configuration>

以上是hibernate的配置

spring配置

添加spring.jar,aspectj文件夹下所有,cglib-nodep-2.1_3.jar,commons-logging.jar(日志),

applicationContext.xml放在src下,代码如下

<?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: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/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
				http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

	<!-- 自动扫描与装配bean -->
	<context:component-scan base-package="cn.itcast.oa"></context:component-scan>

	


</beans>

 以上是spring配置

总结一下

Struts2
jar包
struts.xml, web.xml
Hibernate
jar包:核心包, 必须包, jpa, c3p0, jdbc
hibernate.cfg.xml
Spring
jar包
appicationContext.xml

接下来就是要进行整合

首先整合spring和struts2

1.在web.xml中配置Spring的监听器

	<!-- 配置Spring的用于初始化容器对象的监听器 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext*.xml</param-value>
	</context-param>

2.lib中添加struts2-spring-plugin-2.1.8.1 jar包

这样整合之后struts.xml可以直接写bean的名称

接下来进行Hibernate与Spring整合

1.管理SessionFactory实例(只需要一个)

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

  2,声明式事务管理

<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<tx:annotation-driven transaction-manager="txManager"/>

  

原文地址:https://www.cnblogs.com/icysnow/p/5661243.html