- pom
1.1 hibernate相关(5.2.12.Final)
hibernate-core
hibernate-c3p0(数据库连接池)
hibernate-ehcache
mysql-connector-java(5.1.44)
1.2 spring相关(5.0.1.RELEASE)
spring-context
spring-orm
spring-web
spring-aspects
注:创建spring的XML文件时,需要添加beans/aop/tx/context标签支持
各依赖说明:
spring-context
创建spring上下文
spring-orm
org.springframework.orm.hibernate5.LocalSessionFactoryBean
dataSource:指定数据源
hibernateProperties:指定hibernate相关属性,如:dialect/show_sql/format_sql
mappingResources:指定实体映射文件
注1:本章采用将hibernate配置全部交给spring管理,因此项目中不再有hibernate.cfg.xml文件
org.springframework.orm.hibernate5.HibernateTemplate
org.springframework.orm.hibernate5.support.HibernateDaoSupport
测试一:运行测试类Test1,测试hibernate与spring的集成
spring-web
org.springframework.web.context.ContextLoaderListener
contextConfigLocation:classpath:applicationContext-*.xml
org.springframework.web.context.support.WebApplicationContextUtils
测试二:访问test2.jsp,测试spring与WEB项目的集成
spring-aspects
spring的AspectJ依赖,解析事务切点
1.3 struts2相关(2.5.13)
struts2-core
struts2-spring-plugin
struts2与spring集成的插件
将action配置到spring中,struts的action标签的class属性填写spring中bean的名字或id
1.4 log配置
1.4.1 log4j(1.X版)
不建议使用
1.4.2 log4j2(2.9.1)
log4j-core
log4j-api
log4j-web
不建议使用
1.4.3 log4j2 + slf4j(使用ehcache开启hibernate二级缓存必须使用第二种方式配置日志)
注1:本章使用第三种方式配置日志,即log4j2 + slf4j
1.5 other
junit(4.12)
javax.servlet-api(4.0.0)
1.6 jstl
jstl(1.2)
standard(1.1.2)
1.7 jsp自定义标签依赖(必须与tomcat的版本一致)
tomcat-jsp-api
注1:如何解决项目中不支持EL表达式的问题?将web.xml的DTD的版本由2.3升级到3.0即可。因为web2.3是不支持EL表达式的
-
SSH集成
2.1 导入ehcache.xml
2.2 导入log4j2.xml
2.3 集成hibernate
2.3.1 注入数据库配置文件
2.3.2 配置c3p0连接池
2.3.3 注册LocalSessionFactoryBean
2.3.4 spring声明式事物声明式事务配置
-
必须先修改spring配置文件的声明部分,添加对aop标签和tx标签的支持
-
配置SessionFactory(spring和hibernate集成时已完成)
-
配置事务管理器
HibernateTransactionManager -
配置事务的传播特性(tx)
add
edit
del
load|list
事务传播特性(PROPAGATION_REQUIRED|readOnly) -
配置自动代理
a) 引用环绕通知txAdvice
b) 定义一个切入点
execution(* *…Biz.(…))
a:返回值不限 b:包名不限 c:接口名以Biz结尾 d:方法名及参数不限
c) 自动代理的原理
在spring上下文初始化完成以后,自动代理会逐个检查spring上下文中JavaBean实现的接口是否满足自动代理的条件,如果满足条件,则将此bean和通知结合生成一个代理对象,并以此代理对象替换spring上下文中的bean,之后你访问的就是代理对象了注1:狸猫换太子
2.3.5 注册HibernateTemplate
2.3.6 注册Base模块 -
-
程序代码的分层(base模块)
/base
/action
/biz
/dao
/entity
/book
/action
/biz
/dao
/entity用三层架构biz的作用
1.性能更好:(事物的开启及提交数据由多次减为一次)
- 2.为了事务的一致性
- 整合案例演示
4.1 entity
*.hbm.xml
4.2 dao
IXxxDAO
XxxDAOImpl(继承HibernateDaoSupport类,方便注入HibernateTemplate)
applicationContext-Xxx.xml
查询
this.getHibernateTemplate().execute(
4.3 biz
IXxxBiz
XxxBizImpl
applicationContext-Xxx.xml
4.4 test(junit4)
4.5 action
将action配置到applicationContext-Xxx.xml,
注:必须为多例模式(重要)
struts.xml配置注意事项:
1)
2)struts-Xxx.xml文件中的action的class属性类型填写spring中的id。
- web.xml配置
5.1 Spring上下文ApplicationContext.xml的加载
contextConfigLocation
classpath:applicationContext.xml
5.2 启动Web容器时,自动装配ApplicationContext.xml的配置信息
org.springframework.web.context.ContextLoaderListener
5.3 中文乱码过滤器
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
true
encoding
UTF-8
encodingFilter
/*
5.4 其他配置
5.4.1 防止内存泄露、缓存清除监听器
org.springframework.web.util.IntrospectorCleanupListener
5.4.2 用于支持3种Bean的作用域:request,session和globalSession
<listener>
<listener-class> org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
5.4.3 把session的关闭延迟到jsp页面显示之后,请配在struts2上面
<filter>
<filter-name>OpenSessionInView</filter-name>
<filter-class> org.springframework.orm.hibernate5.support.OpenSessionInViewFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInView</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
5.5 Struts2核心过滤器
知识点
<aop:config>
<!-- pointcut属性用来定义一个切入点,分成四个部分理解 [* ][*..][*Biz][.*(..)] -->
<!-- A: 返回类型,*表示返回类型不限 -->
<!-- B: 包名,*..表示包名不限 -->
<!-- C: 类或接口名,*Biz表示类或接口必须以Biz结尾 -->
<!-- D: 方法名和参数,*(..)表示方法名不限,参数类型和个数不限 -->
<aop:advisor advice-ref="txAdvice" pointcut="execution(* *..*Biz.*(..))" />
</aop:config>
<!-- 声明式事务配置结束 -->