struts+spring+hibernate

来源:互联网 发布:蓝桥物流软件价格 编辑:程序博客网 时间:2024/05/30 23:25

创建 Struts+Spring+Hibernate 步骤


1、写Hibernate映射文件创建表
 
   例:创建Userbak表

     User.hbm.xml
    


     <hibernate-mapping>
 <class name="com.accp.gb.User" table="Userbak">
 
 

        <id name="uid" column="userid" type="string">

        <generator class="uuid.hex">
         </generator>

        </id>

        <property name="uname" type="string">

            <column name="uname" />

        </property>
       
           <property name="uage" type="integer">

            <column name="uage" />

        </property>  
     
     </class>
    
     </hibernate-mapping>


2.用ant生成对象类和数据表

   改tools中build.xml的路径


3.创建一个 DAO 类 继承 HibernateDaoSupport 类 ,其中定义操作方法

  如:
 
  public class HibernateOtherDao extends HibernateDaoSupport {
 
 public void InsertUser(User user){
  
  this.getHibernateTemplate().save(user);
  
  
  
 }
 
 public void InsertOtherUser(User user){
  
  this.getHibernateTemplate().save(user);
  
  
  
 }

   }

4.创建一个接口类  定义一些操作方法
 
  例:

   HibernateServiceInter.java

 

  public interface HibernateServiceInter {
 
 
 public void testService(User user,User user1) ;

 public void testServiceother(User user,User user1);
   }


5.创建一个模板回调类 来实现接口 如:HibernateServiceInter.java ,其中实现方法中回调 DAO 类操作数据库

  例:
  
   HibernateServiceImpl
 
 
   public class HibernateServiceImpl implements HibernateServiceInter {
 
 
 private HibernateOtherDao dao;
 

 public void setDao(HibernateOtherDao dao) {
  this.dao = dao;
 }

 public void testService(User user, User user1) {
  // TODO Auto-generated method stub
  
  dao.InsertUser(user);
  
  dao.InsertOtherUser(user1);
  
  

 }

 public void testServiceother(User user, User user1) {
  // TODO Auto-generated method stub
  
 dao.InsertUser(user);
  
  dao.InsertOtherUser(user1);
  
 }

   }


6.配置 applicationContext.xml
 
  其中依次为:

   1>连接数据库
   
   例:
    
     <bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">

     <property name="driverClassName">
     <value>oracle.jdbc.driver.OracleDriver</value>
     </property>

     <property name="url">
     <value>jdbc:oracle:thin:@127.0.0.1:1521:yek</value>
     </property>

     <property name="username">
     <value>scott</value>
     </property>

     <property name="password">
     <value>tiger</value>
     </property>

     </bean>
  

    2>创建 sessionFactory 会话持久化管理器
  
    例:

     <bean id="mysessionfactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
   
    ---导入数据源---

    <property name="dataSource">
    <ref bean="datasource"/>
   </property>

    ---导入映射文件路径---

    <property name="mappingResources">
    <list>
    <value>com/accp/gb/User.hbm.xml</value>
    </list>
    </property>

    ---导入数据库方言---

    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">
     org.hibernate.dialect.Oracle9Dialect
    </prop>
    <prop key="hibernate.show_sql">true</prop>
    </props>
    </property>

    </bean>

   
   3>创建 DAO 类路径   导入 sessionFactory 会话持久化管理器类
 
   例:

    <bean id="hibernateotherdao" class="com.accp.gb.HibernateOtherDao">
    <property name="sessionFactory">
    <ref bean="mysessionfactory"/>
    </property>
    </bean>


   4>创建事务管理器  导入 sessionFactory 会话持久化管理器类


   例:

     <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
     <property name="sessionFactory">
     <ref bean="mysessionfactory"/>
     </property>
     </bean>


   5>创建模板回调类  导入Dao类

    例:

    <bean id="hibernateservice" class="com.accp.gb.HibernateServiceImpl">
    <property name="dao">
    <ref bean="hibernateotherdao"/>
    </property>
    </bean>


    6>创建事务代理接口  导入接口类  导入目标类(DAO 类)  导入宣称式事务  导入事务管理器


    例:


    <bean id="proxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
   

    ---导入接口类---

    <property name="proxyInterfaces">
    <value>com.accp.gb.HibernateServiceInter</value>
    </property>

    ---导入目标类(DAO 类)---

    <property name="target">
    <ref bean="hibernateservice"/>
    </property>

    ---导入宣称式事务---

    <property name="transactionAttributes">
    <props>
    <prop key="testService">PROPAGATION_REQUIRED,ISOLATION_DEFAULT,+MyException</prop>
    <prop key="testServiceother">PROPAGATION_REQUIRED,ISOLATION_DEFAULT</prop>
    </props>
    </property>
 
    ---导入事务管理器---

    <property name="transactionManager">
    <ref bean="transactionManager"/>
    </property>


    </bean>


7.创建struts-config.xml

 例:

  <struts-config>
  <data-sources />
  <form-beans />
  <global-exceptions />
  <global-forwards />
  <action-mappings >

  ------ action 配置------
    <action path="/login" type="org.springframework.web.struts.DelegatingActionProxy">
      <forward name="error" path="/error.jsp" />
      <forward name="sucess" path="/sucess.jsp" />
    </action>

  </action-mappings>

  <message-resources parameter="com.accp.gb.ApplicationResources" />
 

   ------导入 struts 插件------
      <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
        <set-property
         property="contextConfigLocation"
         value="/WEB-INF/action-servlet.xml" />       
    </plug-in>
 
  </struts-config>


8.创建action-servlet.xml

  例:

   <bean name="/login" class="com.accp.gb.action.LoginAction">
   <property name="hsin">
   <ref bean="proxy"/>
   </property>
   </bean>

9.导入 applicationContext.xml 路径  和 spring监听---(Web.xml)

   例:

   <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
      /WEB-INF/applicationContext.xml
    </param-value>
   </context-param>
 
 
   <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
   

  
10.导入数据库连接包