Struts1.x spring3 hibernate4 初步整合

1、先导入Spring3.0框架,再导入hibernate框架,多余删除。

 2、hibernate的核心,被spring接管了。 

  配置数据源   <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">   托管给Spring

配置SessionFactory   <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  托管给Spring,注入数据源等。

我们编写domain对象和映射文件Employee.hbm.xml,注入到sessionFactory

        可以测试SH是否合并成功,
ApplicationContext ac = new ClassPathXmlApplicationContext(
"applicationContext.xml");

SessionFactory sf=(SessionFactory) ac.getBean("sessionFactory");

Session s=sf.openSession();

Transaction ts=s.beginTransaction();

s.save(employee);

3、编写service类,先写接口;

    service类托管给Spring,可注入属性sessionFactory,即可实现持久化,但每次都需提交事务,麻烦,aop思想(类似环绕通知),配置

<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">(应该底层像环绕通知)

<!-- 启用事务注解 -->

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

在service 上@Transactional 将类中事务管理托管给Spring

4、整合struct,struts开发包,创建struts-config.xml,可图形设计完成页面设计  Action使用分派的,Form一个够用了。

  web.xml中   <!-- 初始ACTION --> struts的大action 随猫启动

web.xml中初始化的spring容器:

<!-- 指定spring的配置文件,默认从web根目录寻找配置文件, 我们可以通过spring提供的classpath:前缀指定从类路径下寻找 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 对Spring容器进行实例化 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

这样,猫启动,Spring容器也创建好了,就可以WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(this.getServlet().getServletContext());

EmployeeServiceInter employeeServiceInter = (EmployeeServiceInter) ctx.getBean("employeeService");   调用bean了(注意hibernate与Struts中antlr-2.7.7.ja冲突,留一个就行r)


5、spring接管我们的struts(action控件)

struts-config.xml文件中添加如下代码配置:

<!-- 配置代理请求处理DelegatingRequestProcessor , 告诉大ACTion 此处托管到Spring, -->

<controller>

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

</controller>

<!-- 管理Action   取消单态-->
<bean id="/login" class="com.yanlei.struts.action.LoginAction" scope="prototype">
<property name="employeeServiceInter" ref="employeeService"></property>
</bean> 补充:注解注入,@resource属性,@control整个action @service UserService  自动完成组装

Spring中启动<context:annotation-config/>或者<contest:component-san base-package="com">扫描com包

  xxx/login.do-------action中path   ----Spring中生成LoginAction(设置成prototype,防止单态带来的麻烦),并且可以注入各种service,不用WebApplicationContext取了,省事。


6、解决中文乱码问题

思路: 自己配置过滤器

步骤: (1) 开发一个Filter

(2) web.xml中配置过滤器

<!-- 配置自己写的过滤器 -->

<filter>

<filter-name>MyFilter</filter-name>

<filter-class>com.hsp.web.filter.MyFilter</filter-class>

</filter>

<filter-mapping>

<filter-name>MyFilter</filter-name>

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

</filter-mapping>

 

思路2:使用spring框架提供的处理中文乱码的过滤器

<filter>

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

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

</filter-mapping>

7、ssh整合的特别说明:

spring可以启用注解的方式来配置属性.

重新这样配置bean

<bean id="employeeService" class="com.hsp.service.imp.EmployeeService"/>

  不用写 <property name="sessionFactory" ref="sessionFactory"></property>,注解通过BYNAME注入了

在EmployeeService 的属性sessionFactory中添加一个注解@Resource

applicationContext.xml中启用注解

<context:annotation-config/>


8、spring专门提供了opensessioninview的方法来解决懒加载Session生命周期延长,解决懒加载问题, ${loginUser.department.name } 可用

需要在web.xml文件中添加如下配置:

<filter>

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

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

</filter>

<filter-mapping>

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

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

</filter-mapping>

9基本配置完毕,只需管理好Action


10、jsp 到另一个jsp 也需要进过一个action ,比如flag=addUI

         一个action只做一个事情,例如登录到主界面,一个action 负责验证,验证完后进入下一个action,mapping.forward("/login.do?flag=next")

  一个action 负责准备下个页面的数据并跳转。


补充:2-3之间:

1、开启二级缓存,hibernate4余hibernate3不同class

<property name="hibernateProperties">
   <value>
        hibernate.dialect=org.hibernate.dialect.OracleDialect
        hibernate.hbm2ddl.auto=update
hibernate.show_sql=false
hibernate.cache.use_second_level_cache=true
        hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
                  hibernate.generate_statistics=true   
 
</value>
</property>

2、二级开发包  ehcache-core-2.4.3.jar  hibernate-ehcache-4.1.4.Final.jar 注意版本

3、ehcache.xml

4、<class name="Department" table="department">
  <cache usage="read-write"/>

5、测试:
SessionFactory sf = (SessionFactory) ac.getBean("sessionFactory");
Session s = sf.openSession();

Transaction ts= s.beginTransaction();
Employee employee = (Employee) s.get(Employee.class, 1);
System.out.println(employee.getEmail());
Employee employee2 = (Employee) s.get(Employee.class, 1);
System.out.println(employee2.getEmail());
ts.commit();
s.close();//同一个session ,一个sql 证明一级缓存

Session s1 = sf.openSession();
Transaction ts1= s1.beginTransaction();
Employee employee1 = (Employee) s1.get(Employee.class, 1);
System.out.println(employee1.getEmail());
ts1.commit();
s1.close();//同一个sessionFactory ,一个sql 证明二级缓存


2、注解扫描

BasicService中

@Resource

private SessionFactory sessionFactory;   就可以在application中不用,以为service继承了BasicService,sessionFactory;在BasicService中完成注入了。

<bean id="departmentService" class="com.yanlei.service.impl.DepartmentService">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

但:app中 <context:annotation-config/> 启用注解扫描。可能没有context标签,在


<beans xmlns="http://www.springframework.org/schema/beans" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"
       xmlns:tx="http://www.springframework.org/schema/tx"xmlns:context="http://www.springframework.org/schema/context" >


















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值