1、SSH 整合整合以 spring 为容器,管理 hibernate 的 DAO 和 Struts2 的 Action。一、 准备工作Struts2.06+spring2.5+hibernate3.2+jdk6.0+myeclipse6.0+tomcat5.5+mysql5.0以上是整合的原料。下面以一个注册登陆的例子来开始我们的整合过程。这个例子很简单,下面是它的 sql 脚本内容:Create TABLE user (userid int(11) NOT NULL AUTO_INCREMENT,username varchar(20) NOT NULL,password varchar(16
2、) NOT NULL,email varchar(30) NOT NULL,PRIMARY KEY (userid) ENGINE=InnoDB DEFAULT CHARSET=utf-8;二、 开始行动包结构可以参考下图图一图二图三1) Struts 部分 :建立 struts.xml 和 struts.propertiesStruts.xml 内容如下:/error.jsp/success.jsp /login.jsp/success.jsp /regist.jspStruts.properties 内容如下:struts.devMode=falsestruts.enable.Dynami
3、cMethodInvocation=truestruts.i18n.reload=truestruts.ui.theme=simplestruts.locale=zh_CNstruts.i18n.encoding=UTF-8struts.objectFactory=springstruts.objectFactory.spring.autoWire=namestruts.serve.static.browserCache=falsestruts.url.includeParams=none2) 建立 User.java 和 User.hbm.xml、jdbc.properties:User.j
4、ava 内容如下:/* */* * * author flustar* version 1.0 * Creation date: Dec 23, 2007 1:55:28 PM*/package com.firstssh.model;import java.io.Serializable;public class User implements Serializable private int id;private String username;private String password;private String email;public int getId() return id;
5、public void setId(int id) this.id = id;public String getUsername() return username;public void setUsername(String username) this.username = username;public String getPassword() return password;public void setPassword(String password) this.password = password;public String getEmail() return email;pub
6、lic void setEmail(String email) this.email = email;User.hbm.xml 内容:jdbc.properties 内容如下:datasource.type=mysqldatasource.driverClassName=com.mysql.jdbc.Driverdatasource.url=jdbc:mysql:/localhost:3306/test?useUnicode=true&characterEncoding=UTF-8datasource.username=rootdatasource.password=123456datasou
7、rce.maxActive=10datasource.maxIdle=2datasource.maxWait=120000datasource.whenExhaustedAction=1datasource.validationQuery=select 1 from dualdatasource.testOnBorrow=truedatasource.testOnReturn=falsec3p0.acquireIncrement=3c3p0.initialPoolSize=3c3p0.idleConnectionTestPeriod=900c3p0.minPoolSize=2c3p0.maxP
8、oolSize=50c3p0.maxStatements=100c3p0.numHelperThreads=10c3p0.maxIdleTime=600hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect#hibernate.dialect=org.hibernate.dialect.MySQLMyISAMDialecthibernate.jdbc.batch_size=25hibernate.jdbc.fetch_size=50hibernate.show_sql=truehibernate.connection.release
9、_mode=after_transaction3) Spirng 部分:为了清晰把 Spring 的配置文件拆分成以下几部分 applicationContext-dao.xml、 appliationContext-service.xml、applicationContext-hibernate.xml、action- servlet.xml。applicationContext-hibernate.xml 内容:WEB-INF/mail.propertiesWEB-INF/jdbc.propertiesWEB-INF/oscache.properties$datasource.driver
10、ClassName$datasource.url$datasource.username$datasource.password$c3p0.acquireIncrement$c3p0.initialPoolSize$c3p0.minPoolSize$c3p0.maxPoolSize$c3p0.maxIdleTime$c3p0.idleConnectionTestPeriod$c3p0.maxStatements$c3p0.numHelperThreadscom/firstssh/model/User.hbm.xml$hibernate.dialect$hibernate.show_sql$hi
11、bernate.jdbc.fetch_size$hibernate.jdbc.batch_sizePROPAGATION_REQUIREDPROPAGATION_REQUIREDPROPAGATION_REQUIREDPROPAGATION_REQUIREDPROPAGATION_REQUIREDPROPAGATION_REQUIREDPROPAGATION_REQUIREDPROPAGATION_REQUIRED,readOnlyPROPAGATION_REQUIRED,readOnlyPROPAGATION_REQUIRED,readOnlyPROPAGATION_REQUIREDPROPAGATION_REQUIRED,readOnlyuserServicetransactionInterceptorapplicationContext-dao.xml 内容:applicationContext-service.xml 内容: