ssh整合
环境
- struts2+spring4+hibernate4
- jdk1.7
- myeclipse
一、导包
1.struts2中的包
struts-2.3.30\apps\struts2-blank\WEB-INF的lib中的全部;
struts-2.3.30\lib中的:
aopalliance-1.0.jar
struts2-spring-plugin-2.3.30.jar
2.spring中的包
spring-framework-4.3.5.RELEASE-dist\spring-framework-4.3.5.RELEASE\libs中的全部(并不是全部都必须)
3.hibernate中的包
\hibernate-release-4.3.11.Final\lib中
required下的全部;
jpa下的全部;
optional\c3p0下的全部;
4.loging包
commons-logging-1.1.1.jar
5.mysql连接器
mysql-connector-java-5.1.7-bin.jar
6.json转换工具
fastjson-1.2.9.jar
7.aop相关包
aspectjweaver.jar
aopalliance-1.0.jar
二、配置xml
1 .web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>sshDemo1</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- struts的核心过滤器,把所有符合要求的请求都用struts进行处理 -->
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<!-- 配置spring的监听器 ,加载applicationContext.xml -->
<!-- 用初始化参数指定spring配置文件的位置 -->
<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>
</web-app>
2.struts.xml
在src根目录新建一个struts.xml
文档约束来自web app libraries下的struts2-core-2.3.30.jar/struts-2.3.dtd文件中
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.devMode" value="true"></constant>
<package name="my" namespace="/" extends="struts-default">
<!-- class的值可以是ioc容器中bean的name -->
<action name="user_*" class="userAction" method="{1}">
<result name="ok">/ok.jsp</result>
</action>
</package>
</struts>
3.applicationContext.xml
文档约束可以从spring包的文档中查找;添加后保存重新打开,xml的NameSpaces视图中可以选择其他约束;
<?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" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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">
<!-- 自动扫描与装配bean -->
<context:component-scan base-package="com"></context:component-scan>
<!-- 加载外部的properties配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- 配置数据库连接池(c3p0) -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 基本信息 -->
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<property name="driverClass" value="${driverClass}"></property>
<property name="user" value="${username}"></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>
<!-- 配置SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 整合hibernate方式1:指定hibernage的配置文件 -->
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<!-- 整合hibernate方式2:配置hibernate中需要的参数 -->
<!-- -->
<property name="hibernateProperties">
<props>
<prop key=""></prop>
</props>
</property>
</bean>
<!-- 配置声明式的事务管理(采用基于注解的方式) -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 若action继承了自定义父类则必须开启自动代理 -->
<aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
4.jdbc.properties数据库信息配置文件
jdbcUrl=jdbc:mysql://localhost:3306/c19
driverClass=com.mysql.jdbc.Driver
username=root
password=root
5.hibernate.cfg.xml
若使用applicationContext.xml来配置sessionFactory信息则可以省略此配置文件.
<?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>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<mapping resource="com/xingxue/model/User.hbm.xml" />
</session-factory>
</hibernate-configuration>