采用目前最新的struts-2.3.1.2、hibernate3.6.10.Final、spring-framework-3.1.1.RELEASE开发包,以及eclipse-jee-indigo-SR2-win32、apache-tomcat-7.0.27服务器、JDK6和mysql5做开发环境,参考了前辈的一篇相关文章,改正了其中的错误和疏漏,克服了公司分配的“新”机器每小时自动重启三次的困难,终于把环境给搭好了。整个过程中遵循的一个原则是,避免引入用不到的jar包,以求搭建一个最小的SSH运行环境。
首先创建一个Dynamic web project 输入任意名字如SSHBase。
第一步:加载Spring环境
我们需要引入的包有:
org.springframework.asm-3.1.1.RELEASE.jar org.springframework.beans-3.1.1.RELEASE.jar org.springframework.context-3.1.1.RELEASE.jar
org.springframework.core-3.1.1.RELEASE.jar org.springframework.expression-3.1.1.RELEASE.jar org.springframework.jdbc-3.1.1.RELEASE.jar
org.springframework.web-3.1.1.RELEASE.jar org.springframework.orm-3.1.1.RELEASE.jar
由于spring默认开启了日志,还需要加入commons-logging的jar包,否则会报错。
建议不要一次性加入 应该先加最核心的运行代码看缺少什么加什么,这样就不会加多余的包进来了,spring3已经把包按功能分开,不像以前一个包,这样更灵活,只要运行我们需要的功能,而没用到的就不用在硬性的添加进来。
包加好了之后我们开始创建spring应用,创建一个XML文件ApplicationContext.xml(放在src根目录),下面是基本结构
1 | <? xml version = "1.0" encoding = "UTF-8" ?> |
2 | < beans xmlns = "http://www.springframework.org/schema/beans" |
3 | xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" |
4 | xsi:schemaLocation="http://www.springframework.org/schema/beans |
5 | <a href = "http://www.springframework.org/schema/beans/spring-beans-3.0.xsd'%3E" >http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> </ a > |
写一个用例来测试一下spring搭建好了没有,创建一个Person类:
08 | public void setName(String name) { |
12 | public void setAge( int age) { |
16 | public String toString() { |
17 | return "name : " + name + " age : " + age; |
然后在配置文件,也就是AppliccationContext.xml里面添加bean相关配置,要注意类的包路径(这是值注入,还有接口和构造注入):
1 | < bean id = "nike" class = "Person" scope = "prototype" > |
3 | < property name = "name" value = "XXX" ></ property > |
5 | < property name = "age" value = "18" ></ property > |
再写一个测试类,来进行测试:
01 | import org.springframework.beans.factory.xml.XmlBeanFactory; |
02 | import org.springframework.core.io.FileSystemResource; |
04 | public class testSpring { |
06 | public static void main(String[] args) { |
08 | XmlBeanFactory beansfactory = new XmlBeanFactory( new FileSystemResource( "src/ApplicationContext.xml" )); |
09 | System.out.println(beansfactory); |
10 | Person nike = (Person)beansfactory.getBean( "nike" ); |
11 | System.out.println(nike); |
运行效果应该看到: name : XXX age : 18
这就证明搭建好了,beans已经自己创建。
第二步:整合hibernate
需要的包如下:
antlr-2.7.6.jar commons-collections-3.1.jar commons-logging-1.1.1.jar dom4j-1.6.1.jar
hibernate3.jar slf4j-api-1.6.1.jar hibernate-jpa-2.0-api-1.0.1.Final.jar
javassist-3.12.0.GA.jar jta-1.1.jar ejb3-persistence.jar
加好之后在spring里面配置hibernate(假定在数据库已经创建了一个叫做myTest的数据库)
01 | < bean id = "dataSource" class = "org.apache.commons.dbcp.BasicDataSource" destroy-method = "close" > |
02 | < property name = "driverClassName" value = "com.mysql.jdbc.Driver" /> |
03 | < property name = "url" value = "jdbc:mysql://localhost:3306/mytest?characterEncoding=UTF-8" /> |
04 | < property name = "username" value = "root" /> |
05 | < property name = "password" value = "root" /> |
09 | < bean id = "sessionFactory" |
10 | class = "org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" > |
11 | < property name = "dataSource" ref = "dataSource" /> |
12 | < property name = "annotatedClasses" > |
14 | < value >com.neareast.test.Person</ value > |
17 | < property name = "hibernateProperties" > |
19 | < prop key = "hibernate.dialect" >org.hibernate.dialect.MySQLDialect</ prop > |
20 | < prop key = "hibernate.show_sql" >true</ prop > |
21 | < prop key = "hiberante.format_sql" >true</ prop > |
22 | < prop key = "hibernate.hbm2ddl.auto" >update</ prop > |
28 | < bean id = "personDao" class = "com.neareast.test.PersonDao" > |
29 | < property name = "sessionFactory" ref = "sessionFactory" ></ property > |
然后修改一下刚才的Person类,并加上注解,就可以用了:
01 | import javax.persistence.Column; |
02 | import javax.persistence.Entity; |
03 | import javax.persistence.Id; |
04 | import javax.persistence.Table; |
10 | @Column (unique= true ,name= "id" ,nullable= false ) |
12 | @Column (name= "name" ,length= 20 ) |
17 | public void setName(String name){ |
20 | public void setId( int id ){ |
23 | public void setAge( int age){ |
27 | public String toString() { |
28 | return "name : " + name + " age : " + age; |
DAO接口中定义了四个简单的方法:
1 | public interface IPersonDao { |
2 | public void createPerson(Person person); |
3 | public List<Serializable> getAll(); |
5 | public void drop(Person person); |
其实现如下:
01 | import org.hibernate.Query; |
02 | import org.hibernate.Session; |
03 | import org.hibernate.SessionFactory; |
05 | public class PersonDao implements IPersonDao { |
07 | private SessionFactory sessionFactory; |
10 | public void createPerson(Person person) { |
11 | Session session = sessionFactory.openSession(); |
12 | session.beginTransaction(); |
14 | session.beginTransaction().commit(); |
18 | public List<Serializable> getAll() { |
19 | Session session = sessionFactory.openSession(); |
20 | String sql = "from Person" ; |
21 | session.beginTransaction(); |
22 | Query query = session.createQuery(sql); |
28 | Session session = sessionFactory.openSession(); |
29 | String sql = "from Person" ; |
30 | session.beginTransaction(); |
31 | Query query = session.createQuery(sql); |
32 | return query.list().size(); |
36 | public void drop(Person person) { |
37 | Session session = sessionFactory.openSession(); |
38 | session.beginTransaction(); |
39 | session.delete(person); |
40 | session.beginTransaction().commit(); |
44 | public void setSessionFactory(SessionFactory sessionFactory) { |
45 | this .sessionFactory = sessionFactory; |
然后再写一个简单的用例来测试:
01 | import org.springframework.beans.factory.xml.XmlBeanFactory; |
02 | import org.springframework.core.io.FileSystemResource; |
04 | public class TestHibernate { |
05 | public static void main(String[] args) { |
06 | XmlBeanFactory beansfactory = new XmlBeanFactory( |
07 | new FileSystemResource( "src/ApplicationContext.xml" )); |
08 | PersonDao persondao = (PersonDao) beansfactory.getBean( "personDao" ); |
09 | Person person = new Person(); |
11 | person.setName( "哈哈哈" ); |
12 | persondao.createPerson(person); |
13 | System.out.println(persondao.getAll().size()); |
由于数据表设置成自动更新,在没有表的情况下,hibernate会帮我们自动创建表和他们之间的关系。打开mysql客户端 输入:
use mytest;
select * from person;
如果看到我们的数据就证明OK了!
在这我们就已经将hibernate 基本整合好了!
第三步:整合Struts2
引入下列jar包:
struts2-core-2.3.1.2.jar struts2-spring-plugin-2.3.1.2.jar xwork-core-2.3.1.2.jar commons-io-2.0.1.jar commons-lang-2.5.jar
ognl-3.0.4.jar freemarker-2.3.18.jar commons-logging-api-1.1.jar commons-fileupload-1.2.2.jar
在WEB-INF目录下,创建并编辑web.xml文件:
01 | <? xml version = "1.0" encoding = "UTF-8" ?> |
02 | < web-app xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" |
03 | xmlns = "http://java.sun.com/xml/ns/javaee" xmlns:web = "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" |
04 | xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" |
05 | id = "WebApp_ID" version = "3.0" > |
07 | < display-name >SSHBase1.0</ display-name > |
10 | < welcome-file >index.jsp</ welcome-file > |
14 | < param-name >contextConfigLocation</ param-name > |
15 | < param-value >/WEB-INF/classes/ApplicationContext.xml</ param-value > |
18 | < listener-class >org.springframework.web.context.ContextLoaderListener</ listener-class > |
22 | < filter-name >struts2</ filter-name > |
23 | < filter-class >org.apache.struts2.dispatcher.FilterDispatcher</ filter-class > |
27 | < filter-name >struts2</ filter-name > |
28 | < url-pattern >/*</ url-pattern > |
在src下创建配置文件struts.xml:
01 | <? xml version = "1.0" encoding = "UTF-8" ?> |
02 | <!DOCTYPE struts PUBLIC |
03 | "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" |
04 | "http://struts.apache.org/dtds/struts-2.0.dtd"> |
07 | < constant name = "struts.devMode" value = "true" /> |
09 | < constant name = "struts.objectFactory" value = "spring" ></ constant > |
11 | < package name = "myPackage" extends = "struts-default" > |
13 | < result >/index.jsp</ result > |
16 | < action name = "helloAction" class = "hello" method = "getPerson" > |
17 | < result name = "SUCCESS" >/hello.jsp</ result > |
接下来,创建相应的Action,(注意此处可能需要手动引入服务器的Server Runtime库),例如:
01 | import javax.servlet.http.HttpServletRequest; |
03 | import org.apache.struts2.ServletActionContext; |
05 | import com.opensymphony.xwork2.ActionSupport; |
06 | public class HelloAction extends ActionSupport { |
08 | private static final long serialVersionUID = 1L; |
09 | private IPersonDao dao; |
14 | public String getPerson() { |
15 | System.out.println(dao); |
16 | HttpServletRequest request = ServletActionContext.getRequest(); |
17 | request.getSession().setAttribute( "personlist" , dao.getAll()); |
21 | public IPersonDao getDao() { |
25 | public void setDao(IPersonDao dao) { |
然后在spring的配置文件里,配置相应的class bean:
1 | < bean id = "hello" class = "com.neareast.test.HelloAction" > |
2 | < property name = "dao" ref = "personDao" ></ property > |
接下来创建个页面来进行测试,可在WebContent下面创建一个hello.jsp页面:
01 | <%@ page language="java" contentType="text/html; charset=utf-8" |
02 | pageEncoding="utf-8"%> |
03 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> |
06 | < meta http-equiv = "Content-Type" content = "text/html; charset=utf-8" > |
13 | < label >${personlist}</ label > |
最后,启动服务器,在浏览器中输入: http://localhost:8080/SSHBase/helloAction,就可以看到我们数据库里的数据了。
至此,最基本的SSH已经整合完毕!
整个项目的代码我已经打包上传,欢迎下载,链接如下: http://download.csdn.net/detail/neareast/4223692
转自:http://blog.csdn.net/neareast/article/details/7457618