SSH框架整合

1.配置Spring 和Hibernate,structs的整合

1)通过MyEclipse开发工具构建Spring框架,然后构建Hibernate框架,最后构建Stucts框架。

2)除了配置文件之外,移除原本已经构建好的框架。

3)引入这三个框架整合之后的jar包。

4)按照不同分类,构建不同的applicationContext的配置文件


5)在applicationContext.xml配置文件里编写如下代码(引入其它的配置文件):


6)修改applicationContext-core.xml里的bean的配置。

<!-- 配置c3p0数据源 -->

   <bean id="dataSource"destroy-method="close" class="com.mchange.v2.c3p0.ComboPooledDataSource">

     <property name="driverClass"value="com.mysql.jdbc.Driver"/>

     <property name="jdbcUrl"value="jdbc:mysql://localhost:3306/shop"/>

     <property name="user"value="root"/>

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

     <property name="maxPoolSize"value="40"/>

     <property name="minPoolSize"value="1"/>

     <property name="initialPoolSize"value="1"/>

     <property name="maxIdleTime"value="60"/>

     <property name="checkoutTimeout"value="2000"/>

  </bean>

  <!-- 配置SessionFactory -->

  <bean id="sessionFactory"class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"destroy-method="destroy">

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

      <!-- 配置hibernate属性 -->

     <property name="hibernateProperties">

         <props>

             <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>

             <prop key="hibernate.show_sql">true</prop>

             <prop key="hibernate.format_sql">true</prop>

               <prop key="hibernate.hbm2ddl.auto">update</prop>

             <prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop>         

         </props>

     </property>

     <!-- 配置映射文件 -->

     <property name="mappingResources">

         <list>

             <value>cn/learningit/model/GuestBook.hbm.xml</value>

         </list>

     </property>

</bean>

 

7)在spring下构建对于事务操作的切面类(已经由Spring构建好的)

<bean id="transactionManager"class="org.springframework.orm.hibernate4.HibernateTransactionManager">

      <!-- 配置sessionFactory -->

      <property name="sessionFactory"ref="sessionFactory" />

</bean>

8)配置AOP增强

a.增强配置的命名空间:

xmlns:tx=http://www.springframework.org/schema/tx

b.增强的配置

<!-- 配置事务的AOP增强 -->

  <tx:advice id="txAdvice"transaction-manager="transactionManager">

     <tx:attributes>

        <tx:method name="*"/>

        <tx:method name="search*"read-only="true"></tx:method>

        <tx:method name="add*"rollback-for="EXCEPTION"/>

        <tx:method name="delete*"rollback-for="EXCEPTION"/>

        <tx:method name="insert*"rollback-for="EXCEPTION"/>

        <tx:method name="get*"propagation="NEVER"/>

        <tx:method name="set*"propagation="NEVER"/>

     </tx:attributes>

</tx:advice>

9)配置AOP的切面

1)引入aop的命名空间

xmlns:aop=http://www.springframework.org/schema/aop

2)配置切面(主要就是配置切面的表达式)

  <!-- 配置切面 -->

  <aop:config>

     <!-- 切面表达式 -->

     <aop:pointcut expression="execution(* com.gxa.bj.service..*(..))"id="txPointCut"/>

     <aop:advisor advice-ref="txAdvice"pointcut-ref="txPointCut"/>

</aop:config>

10)编写的dao层中注意对HibernateTemplate方法的引用。比如:

public class CateDao extends HibernateDaoSupport {

  public int page1=0;

  public int size1=0;

 

  public void addCate(Cate c){

     this.getHibernateTemplate().save(c);

  }

  public void deleteCate(Cate c){

     this.getHibernateTemplate().delete(c);

  }

  public Cate searchCate(int id){

    return  this.getHibernateTemplate().get(Cate.class, id);

  }

  

  public List<Cate> serachList(int pageIndex,int pageSize){

    

     this.page1= pageIndex;

     this.size1 = pageSize;

     List<Cate> list= this.getHibernateTemplate().execute(new HibernateCallback<List<Cate>>(){

 

     @Override

     public List<Cate> doInHibernate(Session arg0)throws HibernateException {

       // TODO Auto-generated method stub

       Stringhsql = "FromCate";

       Queryquery = arg0.createQuery(hsql);

       int startIndex = (CateDao.this.page1-1)*CateDao.this.size1;

       return (List<Cate>)query.setFirstResult(startIndex).setMaxResults(CateDao.this.size1).list();

      

     }

  });

     return list;

  }

}

备注:HibernateTemplate 的常用方法。

• void delete(Object entity): 删除指定持久化实例。

• deleteAll(Collection entities): 删除集合内全部持久化类实例。

• find(String queηString): 根据HQL 查询字符串来返回实例集合。

• findByNamedQuery(StringqueryName): 根据命名查询返回实例集合。

• get(Class entityClass,Serializable id): 根据主键加载特定持久化类的实例。

• save(Object entity): 保存新的实例。

• saveOrUpdate(Object entity): 根据实例状态,选择保存或者更新。

• update(Object entity): 更新实例的状态,要求entity 是持久状态。

• setMaxResults(int maxResults): 设置分页的大小。

11)在配置dao层的时候,需要注入sessionFactory:

<bean id="cateDao"class="com.gxa.bj.dao.CateDao">

      <property name="sessionFactory"ref="sessionFactory"></property>

</bean>

12)为了能够使用Hibernate的延迟加载,需要在web.xml文件里配置OSIV模式。代码如下图所示:


参考代码:

<filter>

    <filter-name>OpenSessionInViewFilter</filter-name>

    <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>

 </filter>

 <filter-mapping>

    <filter-name>OpenSessionInViewFilter</filter-name>

    <url-pattern>*.action</url-pattern>

  </filter-mapping>

13)配置Struts和Spring的集成

 a.添加必要的集成jar包(可直接从之前的所有的jar包中去拷贝).

 b.在web.xml文件中添加相应的监听器。

  <context-param>

   <param-name>contextConfigLocation</param-name>

   <param-value>classpath: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>

c.配置structsactionbean,注意其scope的值:

<bean id="cateAction"class="com.gxa.bj.action.CateAction" scope="prototype">

       <property name="cateService" ref="cateService"></property>

   </bean>

d.struts.xml文件里引入之前所配的actionbean的参考代码:

<action name="cate"class="cateAction" method="add">

            <result>/index.jsp</result>

</action>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值