Struts2+Spring2.5+JPA(Hibernate3)环境搭建,使用注解

也可参考Struts2官方文档

依赖Jar包如下:

Jar

From

xwork-2.0.3.jar

Struts 2

struts2-core-2.0.8.jar

Struts 2

struts2-spring-plugin-2.0.8.jar

Struts 2

ognl-2.6.11.jar

Struts 2

freemarker-2.3.8.jar

Struts 2

commons-logging-api-1.1.jar

Struts 2

mysql-connector-java.jar

MySql JDBC Driver

spring.jar

Sping 2.0

antlr.jar

Hibernate Core

asm.jar

Hibernate Core

asm-attrs.jar

Hibernate Core

cglib.jar

Hibernate Core

dom4j.jar

Hibernate Core

jdbc2_0-stdext.jar

Hibernate Core

ehcache.jar

Hibernate Core

hibernate3.jar

Hibernate Core

xml-apis.jar

Hibernate Core

commons-collections.jar

Hibernate Core

ejb3-persistence.jar

Hibernate Annotations

jta.jar

Hibernate Annotations

hibernate-annotations.jar

Hibernate Annotations

hibernate-entitymanager.jar

Hibernate Entity Manager

javassist.jar

Hibernate Entity Manager

jboss-archive-browsing.jar

Hibernate Entity Manager

1.新建Web工程

2.在classpath下建立META-INF目录,并在其中创建persistence.xml文件:

< persistence  xmlns ="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation
="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
    version
="1.0" >
    
< persistence-unit  name ="oa"  transaction-type ="RESOURCE_LOCAL" >
        
< provider > org.hibernate.ejb.HibernatePersistence </ provider >
        
< properties >
            
< property  name ="hibernate.dialect"  value ="org.hibernate.dialect.MySQL5Dialect"   />
            
< property  name ="hibernate.connection.driver_class"  value ="com.mysql.jdbc.Driver"   />
            
< property  name ="hibernate.connection.url"  value ="jdbc:mysql://localhost:3306/oa"   />
            
< property  name ="hibernate.connection.username"  value ="root"   />
            
< property  name ="hibernate.connection.password"  value ="root"   />
            
< property  name ="hibernate.show_sql"  value ="true"   />
            
< property  name ="hibernate.hbm2ddl.auto"  value ="update" />
        
</ properties >
    
</ persistence-unit >
</ persistence >

 

3.在WEB-INF目录下建立applicationContext.xml文件:

<? xml version="1.0" encoding="UTF-8" ?>
<!--  Spring上下文配置,包括jpa,dataSource,sessionFactory,Transaction等  -->
< 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:context
="http://www.springframework.org/schema/context"  xmlns:tx ="http://www.springframework.org/schema/tx"
    xsi:schemaLocation
="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-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/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"
>

    
<!--  配置JPA注解支持 -->
    
< bean
        
class ="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" >
    
</ bean >  
    
    
<!--  配置实体管理器工厂  -->
    
< bean  id ="entityManagerFactory"  class ="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" >
        
< property  name ="persistenceUnitName"  value ="oa"   />
     </ bean >
    
    
<!--  配置基于注解的声明式事务管理器  -->
    
< bean  id ="transactionManager"  class ="org.springframework.orm.jpa.JpaTransactionManager" >
        
< property  name ="entityManagerFactory"  ref ="entityManagerFactory"   />
    
</ bean >
    
< tx:annotation-driven  transaction-manager ="transactionManager"   />
    
    
<!--  使用自动扫描方式管理bean -->
    
< context:component-scan  base-package ="cn.edu.swu.oa"   />
</ beans >

 

4.在web.xml文件中配置Spring和Struts2:

<? xml version="1.0" encoding="UTF-8" ?>
< web-app  id ="WebApp_ID"  version ="2.4"
    xmlns
="http://java.sun.com/xml/ns/j2ee"  xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation
="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
    
< display-name > oa </ display-name >
    
< welcome-file-list >
        
< welcome-file > index.html </ welcome-file >
        
< welcome-file > index.htm </ welcome-file >
        
< welcome-file > index.jsp </ welcome-file >
        
< welcome-file > default.html </ welcome-file >
        
< welcome-file > default.htm </ welcome-file >
        
< welcome-file > default.jsp </ welcome-file >
    
</ welcome-file-list >

    
<!--  配置Spring应用上下文文件的位置  -->
    
< context-param >
        
< param-name > contextConfigLocation </ param-name >
        
< param-value > /WEB-INF/applicationContext*.xml </ param-value >
    
</ context-param >

    
<!--  配置Spring监听器  -->
    
< listener >
        
< listener-class > org.springframework.web.context.ContextLoaderListener </ listener-class >
    
</ listener >

    
<!--  配置Struts2  -->
    
< filter >
        
< filter-name > struts2 </ filter-name >
        
< filter-class > org.apache.struts2.dispatcher.FilterDispatcher </ filter-class >
    
</ filter >
    
< filter-mapping >
        
< filter-name > struts2 </ filter-name >
        
< url-pattern > /* </ url-pattern >
    
</ filter-mapping >

    
<!--  配置字符编码过滤器  -->
    
< filter >
        
< filter-name > encoding </ filter-name >
        
< filter-class > org.springframework.web.filter.CharacterEncodingFilter </ filter-class >
        
< init-param >
            
< param-name > encoding </ param-name >
            
< param-value > UTF-8 </ param-value >
        
</ init-param >
    
</ filter >
    
< filter-mapping >
        
< filter-name > encoding </ filter-name >
        
< url-pattern > /* </ url-pattern >
    
</ filter-mapping >
</ web-app >

 

5.在classpath下建立struts.xml文件,将Struts2委托Spring管理:

<? 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 >

    
<!--  将struts2委托Spring管理  -->
    
< constant  name ="struts.objectFactory"  value ="spring" ></ constant >
    
< package  name ="test"  extends ="struts-default" >
    
</ package >
</ struts >

 

配置完成,项目文件已打包上传:/Files/chenguanwei/oa.rar

你可能感兴趣的:(hibernate3)