1、搭建环境
(1)新建WEB工程,编码设为UTF-8,设置方法:右键项目—Proerties—Resourse—Textfile encoding—Other—UTF-8。
(2)添加框架环境
(a)添加junit(MyEclipse自带junit的jar包)
(b)添加Struts2
加jar包:
Struts-2.1.8.1:
1,添加${Struts2_Home}/apps/struts2-blank-2.1.8.1.war/WEB-INF/lib/*.jar
2,添加struts2-spring整合插件的jar:
${Struts2_Home}/lib/struts2-spring-plugin-2.1.8.1.jar
加配置文件struts.xml、web.xml
在 web.xml 中配置Struts2的Filter:
<!-- Struts2的配置 -->
<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扩展名配置为action -->
<constant name="struts.action.extension"value="action" />
<!-- 指定主题使用simple(默认值为xhtml) -->
<constant name="struts.ui.theme" value="simple" />
<package name="default"namespace="/" extends="struts-default">
</package>
</struts>
2.3、Hibernate
Jar包:核心包、必须包、jpa、c3p0、jdbc
Hibernate-3.6:
1,添加 ${HIBERNATE_HOME}/hibernate3.jar
2,添加${HIBERNATE_HOME}/lib/required/*.jar
${HIBERNATE_HOME}/lib/jpa/hibernate-jpa-2.0-api-1.0.0.Final.jar
3,添加 JDBC 驱动包,mysql-connector-java-5.1.5-bin.jar
4,添加 slf4j-log4j12-1.5.8.jar、log4j-1.2.15.jar,
5,添加 c3p0-0.9.1.2.jar
配置文件:
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.Oracle10gDialect</property>
<!-- <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:orcl</property>
<property name="connection.username">oa</property>
<property name="connection.password">oa</property>
-->
<!-- 2.其他配置 -->
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<!-- 3.导入映射文件-->
<mapping resource="com/fly/oa/domain/User.hbm.xml" />
</session-factory>
</hibernate-configuration>
*.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.fly.oa.domain">
<class name="User" table="t_user">
<id name="id">
<generator class="sequence">
<param name="sequence">user_id</param>
</generator>
</id>
<property name="name">
</property>
</class>
</hibernate-mapping>
log4j.properties:
### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### direct messages to file hibernate.log ###
#log4j.appender.file=org.apache.log4j.FileAppender
#log4j.appender.file.File=hibernate.log
#log4j.appender.file.layout=org.apache.log4j.PatternLayout
#log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### set log levels - for more verbose logging change 'info' to 'debug' ###
log4j.rootLogger=warn, stdout
#log4j.logger.org.hibernate=info
log4j.logger.org.hibernate=debug
### log HQL query parser activity
#log4j.logger.org.hibernate.hql.ast.AST=debug
### log just the SQL
#log4j.logger.org.hibernate.SQL=debug
### log JDBC bind parameters ###
log4j.logger.org.hibernate.type=info
#log4j.logger.org.hibernate.type=debug
### log schema export/update ###
log4j.logger.org.hibernate.tool.hbm2ddl=debug
### log HQL parse trees
#log4j.logger.org.hibernate.hql=debug
### log cache activity ###
#log4j.logger.org.hibernate.cache=debug
### log transaction activity
#log4j.logger.org.hibernate.transaction=debug
### log JDBC resource acquisition
#log4j.logger.org.hibernate.jdbc=debug
### enable the following line if you want to track down connection ###
### leakages when using DriverManagerConnectionProvider ###
#log4j.logger.org.hibernate.connection.DriverManagerConnectionProvider=trace
2.4、Spring
加JAR包:
Spring 2.5.6
1,添加 ${SPRING_HOME}/dist/spring.jar
2,添加 ${SPRING_HOME}/lib/aspectj/*.jar(共2个)
3,添加${SPRING_HOME}/lib/cglib/cglib-nodep-2.1_3.jar
5,添加${SPRING_HOME}/lib/jakarta-commons/commons-logging.jar(1.1.1版)
6,添加工具jar包:commons-codec.jar,commons-lang.jar
配置文件:
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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 自动扫描与装配 -->
<context:component-scan base-package="com.fly.oa"></context:component-scan>
</beans>
3、整合SSH
3.1、Struts2与Spring整合
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、加一个JAR包:struts2-spring-plugin-2.1.8.1.jar
3.2、Spring与Hibernate整合
为什么要整合?好处:
(1)管理SessionFactory实例(只需要一个)
applicationContext.xml:
通过导入外部配置文件形式,${jdbcUrl}可以取得jdbc.properties的值。
<!-- 导入外部的配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- 配置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>
<!-- 其他配置 -->
<!-- 初始化时获取3个连接,取值应在minPoolSize与maxPoolSize之间。Default:3 -->
<property name="initialPoolSize" value="3"></property>
<!-- 连接池中保留的最大连接数 。Default:15 -->
<property name="maxPoolSize" value="5"></property>
<!-- 连接池中保留的最小连接数 。Default:3 -->
<property name="minPoolSize" value="3"></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>
jdbc.properties:
jdbcUrl=jdbc:oracle:thin:@127.0.0.1:1521:orcl
driverClass=oracle.jdbc.driver.OracleDriver
user=oa
password=oa
(2)声明式事务管理
applicationContext.xml:
<!-- 配置声明式事务管理(采用注解的方式) -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory"ref="sessionFactory"></property>
</bean>
<!-- 配置基于注解的事务支持-->
<tx:annotation-driven transaction-manager="txManager"/>