hibernate+spring+struts1

19 篇文章 0 订阅
11 篇文章 0 订阅

ssh整合的步骤

对于ssh的整合,先加载hibernate ,然后加入spring,然后整合hibernate+spring , 测试sh的整合,如果整合通过,就加入struts, 再整合spring + struts , 通过页面测试,然后再增强ssh框架的健壮性:如加入二级缓存,中文字符过滤器,openSessionInview过滤器等

步骤:

1,添加hibernate支持,用myEclipse自带的功能(用copy,不要映射)

·在添加hibernate的时候,不要建立DAODAO层是有spring来建立的

·在hibernate.cfg.xml里面的设置show_sql 为 true ,用来调试,在测试的时候显示hql语句

2,添加spring的支持

·在添加包的时候,只要添加 spring2.5 AOP , spring 2.5Core, spring2.5 persistence Core ,spring 2.5 web ,spring2.5 misc ,为了防止包的冲突,因为有些包在hibernate,或者struts里面已经存在了

·添加包后,千万要记得赶紧把asm.2.2.3.jar的这个包删掉,因为里面已经有了asm.jar这个包,为了防止包的冲突

3,用反转数据库,生成pojoDAO的方法在同一个文件夹,所以要建立一个hwt.DAO,把dao的方法放到dao包里面

4,建立一个service包,并建立一个简单的service层的实现类,用来简单的测试一下

public class ArticleService {

//在applicationContext.xml里面已经配置了sessionFatory,可以这样注入

@Resource private SessionFactory factory ; 

@Resource private ArticlesDAO articlesDAO ;

public List<Articles> getAllArticles(){

return articlesDAO.findAll();

}

public void saveArticle(Articles articles){

//在service层可以通过factory.getCurrentSession()来获得当前线程的session

Session session = factory.getCurrentSession();

}

}

5,在配置文件中注册一下这个service,和配置一下事务

<!-- 事务 -->

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

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

</bean>

<!-- ==========注解方式================ -->

<!-- <tx:annotation-driven transaction-manager="transactionManager"/> -->

<!-- xml -->

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

<tx:attributes>

<tx:method name="get*" propagation="NOT_SUPPORTED" read-only="true"/>

<tx:method name="*"/>

</tx:attributes>

</tx:advice>

<aop:config>

<aop:pointcut id="mypt" expression="execution(* hwt.service.*.*(..))"/>

<aop:advisor advice-ref="mytx" pointcut-ref="mypt"/>

</aop:config>

<!-- ========扫描service层============ -->

<!-- <context:annotation-config/> 使用注解方式  -->

<!-- xml方式的注入 -->

<!-- 配置了下面的这个,就不要配置上面的那个,因为这里面已经包括了上面的那个的所有的处理器

scan是针对@service @control @Repository,所以,类里面的@resource @autowired这些

也就不要管了,scan已经注册了处理器

 -->

<context:component-scan base-package="hwt.service"/>

 6,写一个测试类来测试一下spring和hibernate的整合

测试一下sh整合了没有,事务成功了吗

public static void main(String[] args) {

ApplicationContext act = new ClassPathXmlApplicationContext("/applicationContext.xml");

ArticleService service = (ArticleService) act.getBean("articleService");

//List<Articles> list = service.getAllArticles();

//for (Articles articles : list) {

//System.out.println(articles.getAtitle());

//}

//增加一个articles来测试一下事务

Articles articles = new Articles();

articles.setAtitle("titile");

articles.setAcontent("dd");

service.saveArticle(articles);

}

7,添加struts1的包,在WEB-INF下面建立struts-config.xml的位置文件,同时建立一个action包和一个简单的类来测试

简单的实现:

public class ArticleAction extends DispatchAction {

@Resource private ArticleService service;

public ActionForward showAllArticles(ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response)

throws Exception {

List<Articles> articleList = service.getAllArticles();

request.setAttribute("list", articleList);

return mapping.findForward("showPage");

}

}

配置文件:

<struts-config>

  <form-beans />

  <global-exceptions />

  <global-forwards />

  <action-mappings>

   <action path="/article" parameter="method" validate="false">

   <forward name="showPage" path="/WEB-INF/page/show.jsp"/><!-- 为了保护jsp页面,可以放在WEB-INF下面,客户端访问不到 -->

   </action>

  </action-mappings>

  

  <!-- 这里是struts和spring的关键步骤,这是一个控制器,当请求过来的时候,会把请求转到spring的容器中

  spring去找bean中的name中的值跟请求的值是否相同,然后有spring来实例化,所以在上面的<action>

  里面不需要写名type的值了

   -->

  <controller>

   <set-property property="processorClass" value="org.springframework.web.struts.DelegatingRequestProcessor"/>

  </controller>

  

  <message-resources parameter="hwt.properties.ApplicationResources" />

</struts-config>

建立一个页面,struts要在页面测试

然后要到spring里面注册一下ArticleAction,因为这个类不是有struts实例化了,而是有Spring实例化了

<!-- 扫描Action -->

<!-- 注意这里是用name,而不是id,因为name才能有特殊符号:如:‘/’,这里name的名字必须和

struts里面的<action>里面的path的值是一样的 -->

<bean name="/article" class="hwt.action.ArticleAction"/>

8,配置web.xml

<!-- struts的中心控制器 -->

  <servlet>

    <servlet-name>action</servlet-name>

    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>

    <init-param>

      <param-name>config</param-name>

<!-- 这里可以指定多个xml文件,用‘,’隔开 -->

      <param-value>/WEB-INF/struts-config.xml</param-value>

    </init-param>

    <init-param>

      <param-name>debug</param-name>

      <param-value>3</param-value>

    </init-param>

    <init-param>

      <param-name>detail</param-name>

      <param-value>3</param-value>

    </init-param>

    <load-on-startup>0</load-on-startup>

  </servlet>

  <servlet-mapping>

    <servlet-name>action</servlet-name>

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

  </servlet-mapping>

  <!-- 监听器,当服务启动的时候实例化spring的容器 -->

  <listener>

   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

  </listener>

  

  <!-- 指定spring的配置文件 -->

  <context-param>

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

   <param-value>classpath:app*.xml</param-value><!-- 放在类文件中 -->

  </context-param>

9,简单的测试过后,成功了的话,那么就要继续增强一下ssh框架的健壮性了

web.xml中配置Spring提供的中文字符处理

  <!-- 中文字符处理器 -->

  <filter>

   <filter-name>characterFilter</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>characterFilter</filter-name>

   <url-pattern>/*</url-pattern>

  </filter-mapping>

配置好了还要对其进行一下测试

!-- Session的控制 -->

 <filter>

  <filter-name>openSession</filter-name>

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

 </filter>

 <filter-mapping>

  <filter-name>openSession</filter-name>

  <url-pattern>/*</url-pattern>

 </filter-mapping>

二级缓存:

hibernate.cfg.xml中配置

<property name="cache.use_second_level_cache">true</property>

<property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>

然后要到需要的每个pojo类中开启缓存 <cache usage="read-only" region="包名+类名"/>

这样还不够,还要在类文件下面建立ehcache.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>

<!-- 

    defaultCache节点为缺省的缓存策略

     maxElementsInMemory 内存中最大允许存在的对象数量

     eternal 设置缓存中的对象是否永远不过期

     overflowToDisk 把溢出的对象存放到硬盘上

     timeToIdleSeconds 指定缓存对象空闲多长时间就过期,过期的对象会被清除掉

     timeToLiveSeconds 指定缓存对象总的存活时间

     diskPersistent 当jvm结束是是否持久化对象

     diskExpiryThreadIntervalSeconds 指定专门用于清除过期对象的监听线程的轮询时间

 -->

<ehcache>

    <diskStore path="D:\cache"/>

    <!-- 默认 -->

    <defaultCache maxElementsInMemory="1000" eternal="false" overflowToDisk="true"

        timeToIdleSeconds="120"

        timeToLiveSeconds="180"

        diskPersistent="false"

        diskExpiryThreadIntervalSeconds="60"/>

     <!-- 如果在bean中设置了<cache usage="read-only" region=""/> 可以通过region里面的值来设置特定的cache -->   

<cache name="这里面是<cache>里面的region里面的值maxElementsInMemory="100" eternal="false"

    overflowToDisk="true" timeToIdleSeconds="300" timeToLiveSeconds="600" diskPersistent="false"/>

</ehcache>

连接池:

 <property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>

<property name="hibernate.c3p0.max_size">50</property>

<property name="hibernate.c3p0.min_size">5</property>

<property name="hibernate.c3p0.idle_test_period">60</property>

<property name="hibernate.c3p0.max_statements">100</property>

<property name="c3p0.acquire_increment">5</property>

<property name="hibernate.c3p0.timeout">60</property>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值