18.01.25,web学习第四十六天,还有半年,努力吧青年 Spring第四天 整合三大框架ssh

46.SSH整合

Transactional 事务,交易 propagation事务传播、传递

1. 三大框架整合 41

1)导包:

Hibernate

  Hibernate/required/所有 + lib/jpa/java persist api持久化规范(都   是接口)+数据库驱动

Struts2

  Struts2 2.3.24/apps/struts2-blank.war/web-inf/lib/所有

  注意:javassist-3.18.1-GA.jarhibernate包重复,删除版本低的。

  Struts2整合Spring插件包:(这个包一旦导入,只要启动struts2就会寻找spring容器,如果spring没启动就报错)。

  18.01.25,web学习第四十六天,还有半年,努力吧青年 Spring第四天 整合三大框架ssh

Spring

  基本4+2core+beans+context+expression+logging+log4j

  整合web:spring-web

  整合aop4个:spring-aop+spring-aspect+aop联盟+aopweaving

  整合hibernate+事务4个:spring-jdbc+spring-tx+c3p0+spring-orm(整合 hibernate)

  整合junit4测试包:

  Spring-test  

标签库:

  Jstl+standard

2. 单独整合Springweb项目

1)导约束(命名空间)-4

   按使用顺序beans+context+aop+tx

2)配置一个Action

3)配置Spring容器随项目的启动而启动——web.XML配置监听器

 18.01.25,web学习第四十六天,还有半年,努力吧青年 Spring第四天 整合三大框架ssh

3. 单独整合struts2web项目

1)配置struts2主配置文件

 18.01.25,web学习第四十六天,还有半年,努力吧青年 Spring第四天 整合三大框架ssh

2)配置struts2核心过滤器到web.xml

 18.01.25,web学习第四十六天,还有半年,努力吧青年 Spring第四天 整合三大框架ssh

4. 整合struts2spring

1)原理,struts2springAction

2)导入必备包:(已导入)

 18.01.25,web学习第四十六天,还有半年,努力吧青年 Spring第四天 整合三大框架ssh

3)配置常量

 18.01.25,web学习第四十六天,还有半年,努力吧青年 Spring第四天 整合三大框架ssh

<constant name="struts.objectFactory" value="spring"></constant>

  将action的创建交给spring容器。

  autoWirespring负责装配action依赖属性。

4)整合方案1(不推荐)

  Struts2自己创建actionspring负责注入属性。

  整合方案二:(重点)

  Struts.xml文件中actionclass属性填写springaction  

  对象的BeanName,完全由spring管理action的生命周期,包

  括 action的创建。在springaction的属性该怎么配就怎么

  配。注意scopeprototype

5.单独配置hibernateweb项目

1)导入实体类与orm元数据

2)配置主配置文件,删除以下俩属性:spring有自己的管理事务的机制。会冲突。

 18.01.25,web学习第四十六天,还有半年,努力吧青年 Spring第四天 整合三大框架ssh

6. 整合hibernatespring

1)原理是将sessionFactory交给Spring管理。

 18.01.25,web学习第四十六天,还有半年,努力吧青年 Spring第四天 整合三大框架ssh

这个sessionFactorySpring封装了hibernatesessionFactory,注意版本选择。

Configuration本身并无用,根据配置信息加载sessionFactory

2)配置方案1

   将sessionFactory配置入Spring,仍然使用外部hibernate.cfg.xml 

   文件。

3)配置方案2

   在Spring配置中放置hibernate配置信息。

 18.01.25,web学习第四十六天,还有半年,努力吧青年 Spring第四天 整合三大框架ssh

   指定orm元数据(如果元素只有一个<property>元素不必展开直接使用value属性)。

 18.01.25,web学习第四十六天,还有半年,努力吧青年 Spring第四天 整合三大框架ssh

7.整合springc3p0连接池

2)配置db.properties

   18.01.25,web学习第四十六天,还有半年,努力吧青年 Spring第四天 整合三大框架ssh

3) 引入连接池到spring

 18.01.25,web学习第四十六天,还有半年,努力吧青年 Spring第四天 整合三大框架ssh

4) 将连接池注入给sessionFactory

 18.01.25,web学习第四十六天,还有半年,努力吧青年 Spring第四天 整合三大框架ssh

   删除以下配置,hibernate不需要自己获得连接了

 18.01.25,web学习第四十六天,还有半年,努力吧青年 Spring第四天 整合三大框架ssh

   此时,hibernate通过连接池获得连接的事务将不自动提交。

8.使用hibernatetemplate模板操作数据库

1dao类继承HibernateDaoSupport

 18.01.25,web学习第四十六天,还有半年,努力吧青年 Spring第四天 整合三大框架ssh

2hibernate模板操作

   execute方法()

 18.01.25,web学习第四十六天,还有半年,努力吧青年 Spring第四天 整合三大框架ssh

   findByCriteria

 18.01.25,web学习第四十六天,还有半年,努力吧青年 Spring第四天 整合三大框架ssh

3)spring中配置dao:注入sessionFactory

 18.01.25,web学习第四十六天,还有半年,努力吧青年 Spring第四天 整合三大框架ssh

9.spring中整合aop事务

1)准备工作:在spring中玩aop必须配置核心事务管理器

 18.01.25,web学习第四十六天,还有半年,努力吧青年 Spring第四天 整合三大框架ssh

2)配置通知并织入目标对象

 18.01.25,web学习第四十六天,还有半年,努力吧青年 Spring第四天 整合三大框架ssh

补充hibernate的保存(session.save())的方法在spring模板中体现:getHibernateTemplate().save(obj);

3)使用注解配置事务

第一步:开启注解配置事务isolation(事务隔离)

 18.01.25,web学习第四十六天,还有半年,努力吧青年 Spring第四天 整合三大框架ssh

第二步:在类上配置注解(readOnly默认true

 18.01.25,web学习第四十六天,还有半年,努力吧青年 Spring第四天 整合三大框架ssh

如果某个方法是改动数据库,那么在方法上配置为false

 18.01.25,web学习第四十六天,还有半年,努力吧青年 Spring第四天 整合三大框架ssh

10.扩大session的作用范围

1)避免使用懒加载时出现的no-session问题;配置过滤器filter

 18.01.25,web学习第四十六天,还有半年,努力吧青年 Spring第四天 整合三大框架ssh

 18.01.25,web学习第四十六天,还有半年,努力吧青年 Spring第四天 整合三大框架ssh

注意任何filter应在struts的过滤器之前,他不会放行

11. 自此,hibernate.cfg.xml无用了

  回忆struts2的报错跳页

Throw new RuntimeException(“密码错误”);

配:

 18.01.25,web学习第四十六天,还有半年,努力吧青年 Spring第四天 整合三大框架ssh

全域匹配可酌情况配,如:登录时报错就配在相对应的action下,其它再配全域、