Struts2、spring、hibernate框架整合
一、整合原理
三大框架架构
二、导入依赖包
2.1 hibernate包
2.1.1 hibernate/lib/required
2.1.2 hibernate/lib/jpa | java persist api java的持久化规范(接口)
2.1.3 数据库驱动
2.2 Struts包
2.2.1 struts-blank.war/WEB-INF/lib/*
注意:javassist-3.18.1-GA.jar包与hibernate中的重复
2.2.2 struts整合spring插件包
注意:这个包一旦导入,那么struts2在启动时就会寻找spring容器.找不到将会抛出异常
2.3 spring包
2.3.1 基本:4+2
core|beans|context|expression|logging|log4j
2.3.2 整合web:web包
spring-web
2.3.3 整合aop:4个
spring-aop|spring-aspect|aop联盟|aopweaving
2.3.4 整合Hibernate和事务:4个
spring-jdbc|spring-tx|c3p0|spring-orm
2.3.5 整合junit4测试:test包
spring-test
2.4 标签库
standard.jar
jstl-1.2.jar
三、单独配置spring容器
3.1 创建配置文件,并导入约束(4个)beans|context|aop|tx
3.2 配置spring随项目启动
web.xml配置
四、单独配置Struts
4.1 配置struts2主配置文件
4.2 配置struts2核心过滤器到web.xml
五、struts2与spring整合
5.1 配置常量
在struts.xml中配置常量
常量值所在位置
配置常量
5.2 整合方案1:struts2自己创建action,spring负责组装依赖属性(不使用)
不推荐理由:最好由spring完整管理action的生命周期.spring中功能才应用到Action上.
5.3 整合方案2:spring负责创建action以及组装.
applicationContext.xml
struts.xml
六、单独配置hibernate
6.1 导入实体类&orm元数据
6.2 配置主配置文件(可以跳过,在与spring整合的时候会进行配置)
hibernate.cfg.xml
七、spring整合hibernate
7.1 整合原理
将sessionFactory对象交给spring容器管理
7.2 在spring中配置sessionFactory
7.2.1 配置方案一(不推荐)
applicationContext.xml
7.2.2 配置方案二
applicationContext.xml
八、spring整合c3p0连接池
8.1 配置db.properties
8.2 引入连接池到spring中
applicationContext.xml
8.3 将连接池注入给SessionFactory
applicationContext.xml
九、spring整合hibernate环境操作数据库
9.1 Dao类创建:继承HibernateDaoSupport
9.2 hibernate模板的操作
9.2.1 execute
9.2.2 findByCriteria
9.3 spring中配置dao
十、spring的aop事务
10.1 配置核心事务管理器
10.2 xml方式配置aop事务
10.2.1 配置通知
10.2.2 配置织入
10.3 注解方式配置aop事务
10.3.1 开启注解事务
10.3.2 Service类中使用注解
十一、扩大session的作用范围
为了避免使用懒加载时出现no-session问题.需要扩大session的作用范围
在web.xml中配置filter