SSH框架整合

来源:互联网 发布:private在java 编辑:程序博客网 时间:2024/06/07 05:46

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

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

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

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

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


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


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

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

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

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

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

     <propertyname="user"value="root"/>

     <propertyname="password"value="123"/>

     <propertyname="maxPoolSize"value="40"/>

     <propertyname="minPoolSize"value="1"/>

     <propertyname="initialPoolSize"value="1"/>

     <propertyname="maxIdleTime"value="60"/>

     <propertyname="checkoutTimeout"value="2000"/>

  </bean>

  <!-- 配置SessionFactory -->

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

      <propertyname="dataSource"ref="dataSource"/>

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

     <propertyname="hibernateProperties">

         <props>

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

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

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

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

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

         </props>

     </property>

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

     <propertyname="mappingResources">

         <list>

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

         </list>

     </property>

</bean>

 

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

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

      <!-- 配置sessionFactory -->

      <propertyname="sessionFactory"ref="sessionFactory"/>

</bean>

8)配置AOP增强

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

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

b.增强的配置

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

  <tx:adviceid="txAdvice"transaction-manager="transactionManager">

     <tx:attributes>

        <tx:methodname="*"/>

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

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

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

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

        <tx:methodname="get*"propagation="NEVER"/>

        <tx:methodname="set*"propagation="NEVER"/>

     </tx:attributes>

</tx:advice>

9)配置AOP的切面

1)引入aop的命名空间

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

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

  <!-- 配置切面 -->

  <aop:config>

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

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

     <aop:advisoradvice-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:

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

      <propertyname="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的值:

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

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

   </bean>

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

<actionname="cate"class="cateAction"method="add">

            <result>/index.jsp</result>

</action>

0 0
原创粉丝点击