一、创建web项目工程 wzz

      Strut2 spring hibernate 整合

点击finish

 

2、添加spring Jar包   AOP,Core,Persistence Core ,web jar

     Strut2 spring hibernate 整合

 

点击next

 

Strut2 spring hibernate 整合

 点击Finish

3、配置Database Driver

     Strut2 spring hibernate 整合

     我使用的是JTDS jar包,jtds在配置url地址时与使用sql Seriver的url地址有点不太一样,然后直接点击Finish即可

 

4、添加hibernate 配置

 

     Strut2 spring hibernate 整合

 

    点击next

    

    Strut2 spring hibernate 整合

 

    选择Strut2 spring hibernate 整合使用Spring的applicationContext.xml,点击next

    Strut2 spring hibernate 整合

    选择Existing Spring configuration file  ,在填写SessionFactory ID ,点击next

    Strut2 spring hibernate 整合

    点击next

     Strut2 spring hibernate 整合

     不创建sessionFactory,使用spring的sessionFactory点击Finish

 

 

     完成后

[c-sharp] view plaincopy
  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 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">  
  5.   
  6.   
  7.     <bean id="dataSource"  
  8.         class="org.apache.commons.dbcp.BasicDataSource">  
  9.         <property name="driverClassName"  
  10.             value="net.sourceforge.jtds.jdbc.Driver">  
  11.         </property>  
  12.         <property name="url"  
  13.             value="jdbc:jtds:sqlserver://localhost:1433/wzdb">  
  14.         </property>  
  15.         <property name="username" value="sa"></property>  
  16.         <property name="password" value="123"></property>  
  17.     </bean>  
  18.     <bean id="sessionFactory"  
  19.         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
  20.         <property name="dataSource">  
  21.             <ref bean="dataSource" />  
  22.         </property>  
  23.         <property name="hibernateProperties">  
  24.             <props>  
  25.                 <prop key="hibernate.dialect">  
  26.                     org.hibernate.dialect.SQLServerDialect  
  27.                 </prop>  
  28.             </props>  
  29.         </property>  
  30.     </bean>  
  31. </beans>  

这是application.xml自动添加的文件配置,

    现在修改web.xml,

 

    <!-- spring的应用上下文 -->
   <context-param>
         <param-name>contextConfigLocation</param-name>
         <param-value>/WEB-INF/spring/applicationContext.xml</param-value>
   </context-param>
 
  <!-- spring的监听器,以便在启动时就自动加载spring的配置 -->
  <listener>
       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 <!-- 编码过滤 -->
 <filter>
     <filter-name>encodingFilter</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>
     <init-param>
       <param-name>forceEncoding</param-name>
       <param-value>true</param-value>
     </init-param>
 </filter>
 <filter-mapping>
  <filter-name>encodingFilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>

 

   ------------------------------------------------------------------------------------------------------------------------------

 

 用database Explorer视图,利用数据库中的表反射出实体类和实体类与数据库表的映射

 

  Strut2 spring hibernate 整合

 

  选择项目及实体类和映射文件存放路径,点击next

 

 Strut2 spring hibernate 整合

 

 在id Generator 中选择native ,主键生成方式为自动增长,点击finish,spring的配置文件applicationContext.xml中sessionFactory bean会发生变化,新增<property name="mappingResources"><list><value>com/aweb/entity/DicAnimal.hbm.xml</value>
</list></property>配置,把刚才反射时生成的实体类反射文件添加到sessionFactory的mappingResources中。

 

在applicationConext.xml中添加事务

 

<!-- 配置事务管理 -->
 <bean />
 </bean>

 <tx:advice />
  </tx:attributes>
 </tx:advice>

 

 

asm-2.2.3.jar 与asm.jar有冲突,可以创新下载asm Jar包或把asm-2.2.3.jar删除也可

 

 到此spring与hibernate集成已经完成。

 

配置strut2

 

添加struts2的Jar包  xwork-core-2.2.1.1.jar,struts2-core-2.2.1.1.jar,ognl-3.0.jar ,freemarker-2.3.16.jar

 

修改web.xml配置文件,增加struts2的过滤器配置

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

 

在src目录下新增struts.xml文件,struts2 spring hibernate配置完成

 

 

------------------------------------------------------------------------------------------------------------------------------------------------------

 

可以使用spring的HibernateDaoSupport类执行持久化操作:

 

package com.aweb.util;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class BaseDao<T> extends HibernateDaoSupport {
 public void save(T t){
  getHibernateTemplate().save(t);
 }
}

相关文章:

  • 2022-02-10
  • 2022-02-10
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-01-01
  • 2022-01-01
  • 2021-05-08
  • 2021-09-07
  • 2021-12-20
  • 2022-02-10
相关资源
相似解决方案