Struts2.2+Spring3.0+Hibernate3.6整合

来源:互联网 发布:淘宝直通车教程视频 编辑:程序博客网 时间:2024/05/14 14:03

开发环境:JDK7,Oracle10g,Tomcat7

开发工具:MyEclipse9


1.创建web项目SSH



点击Finish后得到项目结构如下:



2.加入相应Strut2.2,Spring3.0,Hibernate3.6的jar包,以及一些SSH整合所需要的第三方jar包,并将它们都添加到组建路径中去(即Add to Build Path)




将jar包加入Build Path之后,得到项目所依赖的jar如下图所示



3.创建MVC每层package和相应的类



Person类代码如下:

package org.xjh.domain;import java.io.Serializable;public class Person implements Serializable{private Long pid;private String pname;public Long getPid() {return pid;}public void setPid(Long pid) {this.pid = pid;}public String getPname() {return pname;}public void setPname(String pname) {this.pname = pname;}}


创建JavaBean和数据库表的映射文件Person.hbm.xml

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping><class name="org.xjh.domain.Person"><idname="pid" type="java.lang.Long" length="10"><generator class="increment"/></id><property name="pname" type="java.lang.String" length="16" column="pname" /></class></hibernate-mapping>


创建hibernate的配置文件hibernate.cfg.xml


配置内容如下:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration><session-factory><property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property><property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property><property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:SSH</property><property name="hibernate.connection.username">scott</property><property name="hibernate.connection.password">tigger</property><property name="hibernate.show_sql">true</property><property name="hibernate.hbm2ddl.auto">update</property><property name="javax.persistence.validation.mode">none</property><mapping resource="org/xjh/domain/Person.hbm.xml" /></session-factory></hibernate-configuration>


创建PersonDao接口

package org.xjh.dao;import org.xjh.domain.Person;public interface PersonDao {public void savePerson(Person person);}

创建PersonDao接口的实现类PersonDaoImpl

package org.xjh.dao.impl;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import org.xjh.dao.PersonDao;import org.xjh.domain.Person;public class PersonDaoImpl extends HibernateDaoSupport implements PersonDao {@Overridepublic void savePerson(Person person) {this.getHibernateTemplate().save(person);}}

创建PersonService接口

package org.xjh.service;import org.xjh.domain.Person;public interface PersonService {public void savePerson(Person person);}

创建PersonService接口的实现类PersonServiceImpl

package org.xjh.service.impl;import org.xjh.dao.PersonDao;import org.xjh.domain.Person;import org.xjh.service.PersonService;public class PersonServiceImpl implements PersonService {private PersonDao personDao;public PersonDao getPersonDao() {return personDao;}public void setPersonDao(PersonDao personDao) {this.personDao = personDao;}@Overridepublic void savePerson(Person person) {personDao.savePerson(person);}}

创建Spring配置文件



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:aop="http://www.springframework.org/schema/aop"xmlns:p="http://www.springframework.org/schema/p"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <import resource="applicationContext-db.xml" ></import><import resource="applicationContext-person.xml" ></import></beans>


applicationContext-db.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:aop="http://www.springframework.org/schema/aop"xmlns:p="http://www.springframework.org/schema/p"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"><!-- 配置 SessionFactory --><bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" ><property name="configLocation"><value>classpath:hibernate/hibernate.cfg.xml</value></property></bean><!-- 配置aop --><!-- 定义事务管理器 --><bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory"><ref bean="sessionFactory"/></property></bean><!-- 定义预知 --><tx:advice id="tx" transaction-manager="transactionManager"><tx:attributes><tx:method name="save*" read-only="false"/><tx:method name="delete*" read-only="false"/><tx:method name="update*" read-only="false"/><tx:method name="*" read-only="true"/></tx:attributes></tx:advice><!-- 配置aop --><aop:config ><aop:pointcut expression="execution(* org.xjh.service.impl.*.*(..))" id="perform"/><aop:advisor advice-ref="tx" pointcut-ref="perform"/></aop:config></beans>

applicationContext-person.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:aop="http://www.springframework.org/schema/aop"xmlns:p="http://www.springframework.org/schema/p"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <bean id="personDao" class="org.xjh.dao.impl.PersonDaoImpl" ><property name="sessionFactory"><ref bean="sessionFactory"/></property></bean><bean id="personService" class="org.xjh.service.impl.PersonServiceImpl" ><property name="personDao"><ref bean="personDao"/></property></bean><bean id="personAction" class="org.xjh.struts.action.PersonAction"><property name="personService"><ref bean="personService"/></property></bean></beans>

创建测试Spring和Hibernate集成是否成功的用例



BaseSpring类代码如下

package org.xjh.test;import org.junit.Before;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class BaseSpring {public static ApplicationContext context;@Beforepublic void startSpring(){context = new ClassPathXmlApplicationContext("spring/applicationContext.xml");}}
SpringIntegrHibernateTest类代码如下:

package org.xjh.test;import org.hibernate.SessionFactory;import org.junit.Test;/** * Spring和Hibernate整合测试 * @author xjh * */public class SpringIntegrHibernateTest extends BaseSpring{@Testpublic void testPersonService(){PersonService personService = (PersonService) context.getBean("personService");Person person = new Person();person.setPname("zhangsan");personService.savePerson(person);}}
在testSessionFactory方法上方点击右键,Run As ->  JUnit  Test

如果一切正常,在JUnit窗口将得到正确的绿条测试结果,并且使用PLSQL Developer连接SSH数据库可以看到Person表已经创建好并且有了zhangsan这条记录。


下一步就是要进行Struts+Spring+Hibernate的整合了。


在src中创建PersonAction类

package org.xjh.struts.action;import org.xjh.domain.Person;import org.xjh.service.PersonService;import com.opensymphony.xwork2.ActionContext;public class PersonAction {private PersonService personService;public PersonService getPersonService() {return personService;}public void setPersonService(PersonService personService) {this.personService = personService;}public String savePerson(){Person person = new Person();person.setPname("测试");personService.savePerson(person);ActionContext.getContext().put("message", "Person保存成功!");return "success";}}

在config源目录下创建struts配置文件struts.xml

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"    "http://struts.apache.org/dtds/struts-2.0.dtd"><struts><!-- 开启开发模式,修改了配置文件也不用重新部署 --><constant name="struts.devMode" value="true" /><!-- 让Spring给struts创建action实例, 请求还是由struts处理--><constant name="struts.objectFactory" value="spring" /><include file="struts/struts-person.xml"></include></struts>

在config目录下创建名称为struts的package,并在struts下创建struts-person.xml

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"    "http://struts.apache.org/dtds/struts-2.0.dtd"><struts><package name="person" namespace="/" extends="struts-default"><action name="personAction_*" class="personAction" method="{1}"><result name="success">/message.jsp</result></action></package></struts>

具体目录结构如下:



配置web.xml文件,让Strut以过滤器形式过滤各种请求(处理各种请求),让Spring以监听器的形式监听web各种请求(负责实例化各种bean)

<?xml version="1.0" encoding="UTF-8"?><web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring/applicationContext.xml</param-value></context-param>    <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>        <welcome-file-list>        <welcome-file>            index.jsp        </welcome-file>    </welcome-file-list></web-app>

在WebRoot目录下创建消息提示页,message.jsp

<%@ page import="java.util.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <title>提示信息</title>  </head>    <body>  ${message}  </body></html>


4.测试运行

将整个项目部署到Tomcat中


直到出现successfully deployed字样,点击ok


启动Tomcat7服务器



看到服务器启动成功



打开IE,在地址栏中输入:http://localhost:8080/SSH/personAction_savePerson.action 得到如下结果



再看控制台,将会看到hibernate的hql提示信息



这样,表名Person记录插入成功!(如果还不信,那就去SSH数据库中查看PERSON表是否加入了一条新记录)


OK,Strut2.2+Spring3.0+Hibernate3.6整合成功了!


0 0