SSH框架整合_版本二(Spring管理)

struts+spirng+hibernate集成
与之前最大的区别就是使用连接池
[spring完全管理hibernate,使用连接池
1、struts
(1)创建JavaWebProject
(2)添加struts类库(基本类库6)
commons-fileupload-1.2.1.jar
commons-io-1.3.2.jar
freemarker-2.3.15.jar
ognl-2.7.3.jar
struts2-core-2.1.8.1.jar
xwork-core-2.1.6.jar
(3)在web.xml文件中添加过滤器
<filter>
<filter-name>struts</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
(4)添加src下添加struts.xml配置文件
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
(5)启动web服务器测试
http://127.0.0.1:8080/ssh/
(6)完成页面跳转
(a)创建index.jsp
/WEB-INF/jsp/index.jsp
(b)创建MainAction
(c)在struts.xml中配置action
<package name="ssh-default" extends="struts-default">
<default-action-ref name="mainAction" />
<action name="mainAction"
class="com.tarena.web.action.MainAction">
<result name="index">/WEB-INF/jsp/index.jsp</result>
</action>
</package>
(d)测试跳转是否成功
(7)项目分层
(1)在src下创建一个类库
config.struts.struts.xml
(2)修改web.xml配置文件
<filter>
....
<init-param>
<param-name>config</param-name>
<param-value>struts-default.xml,struts-plugin.xml,config/struts/struts.xml</param-
value>
</init-param>
</filter>
(3)测试
(4)在struts.xml文件中添加常量
使用的时候用这里
<constant name="struts.ui.theme" value="simple"/>
<constant name="struts.i18n.encoding" value="UTF-8"/>
<constant name="struts.devMode" value="true"/>
<constant name="struts.configuration.xml.reload" value="true"/>
<constant name="struts.serve.static.browserCache" value="false"/>
理解的时候用这里
<!-- 定义常量 -->
<!-- 不使用struts2提供标签模板 -->
<constant name="struts.ui.theme" value="simple"/>
<!-- 设置统一字符集默认:UTF-8 -->
<constant name="struts.i18n.encoding" value="UTF-8"/>
<!-- 设置开发模式 默认:false
开发模式:true
生产模式:false
-->
<constant name="struts.devMode" value="true"/>
<!-- 加载配置文件struts.xml 默认 false
开发模式:true 每次请求都会自动加载struts.xml文件
生产模式:false 只加载一次
-->
<constant name="struts.configuration.xml.reload" value="true"/>
<!--
浏览器缓存:默认 true
开发模式:false 不使用浏览器缓存
生产模式:true 使用浏览器缓存
-->
<constant name="struts.serve.static.browserCache" value="false"/>

(5)在config/struts/struts-user.xml
在struts-user.xml引用struts-user.xml
<include file="config/struts/struts-user.xml"/>
(6)提取通用标签文件
/common/taglab.jsp
(7)在index.jsp添加标签
<%@ include file="/common/taglab.jsp"%>
编写注册页面
(8)创建SaveUserAction.java
(9)在config/struts-user.xml中配置SaveUserAction
(10)编写index.jsp--->ok.jsp
(11)测试
(12)创建实体类
com.tarena.entity.User.java
(13)添log4j配置文件和类库
在src下添加log4j.properties
commons-logging.jar
log4j-1.2.15.jar
(14)测试参数是否传递成功

2、struts+spring集成
(1)添加spring类库
spring.jar
dom4j-1.6.1.jar
cglib-nodep-2.1_3.jar
(2)strut2集成sping类库
struts2-spring-plugin-2.1.8.1.jar
(3)添加spring配置文件
config/spring/applicationContext.xml
作用:spring公共信息

(4) 在web.xml文件中添加spring监听器
<!-- 添加spring监听器 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:config/spring/applicationContext.xml
</param-value>
</context-param>
(5)测试
(6)spring管理struts2中action
(a)在struts.xml文件添加struts对象spring支持
<constant name="struts.objectFactory" value="spring"/>
(b)spring管理struts2中Action,在spring文件中配置
<!-- new MainAction -->
<bean id="mainAction" class="com.tarena.web.action.MainAction"/>
<!-- new SaveUserAction -->
<bean id="saveUserAction" class="com.tarena.web.action.SaveUserAction">

</bean>
(c)修改struts*文件信息
struts.xml文件中
<action class="com.tarena.web.action.MainAction">
class="mainAction"换成spring配置文件中<bean id="mainAction">
</action>
struts-user.xml文件中
同样
(d)测试
3、struts+spring+hibernate
(1)添加hibernate类库
slf4j-nop-1.5.8.jar
slf4j-api-1.5.8.jar
jta-1.1.jar
javassist-3.9.0.GA.jar
hibernate3.jar
commons-collections-3.1.jar
antlr-2.7.6.jar
(2)添加hibernate配置文件
******************************************
* config.hibernate.cfg/hibernate.cfg.xml *
* spring管理hibernate的数据源 *
******************************************
config.hibernate.hbm/User.hbm.xml

(5)spirng管理hibernate
理解的时候使用
(a)配置spring数据源
hibernate推荐: c3p0连接池
spring官方推荐使用连接池:DBCP连接池
(b)添加DBCP连接池类库
commons-pool.jar
commons-dbcp.jar
(c)连接池类:
BasicDataSource.java
(d)在spring配置文件中配置连接池
initialSize :连接池启动时创建的初始化连接数量
(默认值initialSize :连接池启动时
创建的初始化连接数量(默认值为0)
maxActive :连接池中可同时连接的最大的连接数
(默认值为8 ,调整为20,高峰单机器
在20并发左右,自己根据应用场景定)
maxIdle: 连接池中最大的空闲的连接数,超过的
空闲连接将被释放,如果设置为负数
表示不限制(默认为8个)
minIdle:连接池 中最小的空闲的连接数,低于这个数量
会被创建新的连接(默认为0,调整为5,该参数越
接近maxIdle,性能越好,因为连接的创建和销
毁,都是需要消耗资源的,但是不能太大,因为在
机器很空闲的时候,也会创建低于minidle个数的连接)
maxWait :最大等待时间,当没有可用 连接时,连接池等待
连接释放的最大时间,超过该时间限制会抛出异常,
如果设 置-1表示无限等待(默认为无限,
调整为60000ms,避免因线程池不够用,
而导致 请求被无限制挂起)
removeAbandonedTimeout :超过时间限制,
回收没有用(废弃)的连接 (默认为 300秒,调整为180)
removeAbandoned :超过 removeAbandonedTimeout时间后,
是否进行没用连接(废弃)的回收(默认为 false,调整为true)

注意:连接池的大小需要根据压力测试报告信息来设置

在spring配置里完全管理hibernate配置文件:
删除hibernate.cfg.xml文件
使用的时候使用
<!-- 配置连接池 -->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${driverClassName}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
<!-- 配置连接池参数 -->
<!-- 初始化连接数:默认0
使用完不去关闭,而是放回到连接池中,等待下次调用
-->
<property name="initialSize" value="${initialSize}" />
<!-- 最大并发数:默认8个 -->
<property name="maxActive" value="${maxActive}" />
<!-- 最大等待连接数 默认为8 负数表示无限制连接数,不建议使用 -->
<property name="maxIdle" value="${maxIdle}" />
<property name="minIdle" value="${minIdle}" />
<!-- 等待时常默认为无限期,建议不能使用,一般是60000ms -->
<property name="maxWait" value="${maxWait}" />
<property name="removeAbandoned" value="${removeAbandoned}" />
<property name="removeAbandonedTimeout" value="${removeAbandonedTimeout}" />
</bean>
spring管理hibernate,删除hibernate.cfg.xml
<!-- spring管理hibernate
名字必须使用:sessionFactory-->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- spring管理数据源 -->
<property name="dataSource" ref="dataSource" />
<!-- spring管理hibernate属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<!-- 管理orm映射文件 -->
<property name="mappingLocations">
<list>
<value>
classpath:config/hibernate/hbm/User.hbm.xml
</value>
</list>
</property>
</bean>

(6)编写测试spring配置文件工具类
test.spring.TestSpring

(7)编写com.tarena.Dao
(a)com.tarena.dao.IUserDao
(b)com.tarena.dao.impl.UserDaoImpl
(c)UserDaoImpl
extends HibernateDaoSupport
implements IUserDao
(d)在spring配置文件添加Dao(UserDaoImpl)管理
<!-- new UserDaoImpl() -->
<bean id="UserDaoImpl" class="com.tarena.dao.impl.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
(e)编写getUserById方法
(f)测试(junit4.4)
添加junit4.4类库 junit-4.4.jar
insert into t_user(username,password)values('java','1234');

(8)配置声明事务管理
web biz dao

转账{
开启事务
加钱 加钱(){}
减钱 减钱(){}
提交事务
}
存钱(){
加钱(){}
}

一个转账功能必须让加钱、 减钱 运行在一个事务中,
spring管理的数据源可以自动提交事务,先让 dao(加钱\减钱)
的事务挂起,让他运行在biz(转账)层 事务中!改变事务规则
使用规则(PROPAGATION_REQUIRES_NEW)

事务一般配置在那个层:在业务层
添加spring事务类库

aspectjrt.jar
aspectjweaver.jar

<tx:>、<aop:>类库添加标记类库:

<tx:>标记实现事务通知\
使用的时候使用
<!-- 配置spirng声明事务管理 -->
<!-- spring事务事务管理 -->
<bean id="hibernateTransactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!--事务通知-->
<tx:advice id="logAdvice" transaction-manager="hibernateTransactionManager">
<!--定义规则*统配 -->
<tx:attributes>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="get*" propagation="NOT_SUPPORTED"/>
<tx:method name="find*" propagation="NOT_SUPPORTED"/>
</tx:attributes>
</tx:advice>

<aop:config>
<!--定义一个切入点-->
<aop:pointcut id="tranPointCut" expression="execution(* com.tarena.biz.*.*(..))"/>
<!--定义方面: 通知织入到切面-->
<aop:advisor advice-ref="logAdvice" pointcut-ref="tranPointCut"/>
</aop:config>


声明式事务管理:
了解事务的几种传播特性
(1)PROPAGATION_REQUIRED: 如果存在一个事务,则支持当前事务。如果没有事务则开启。
(2)PROPAGATION_SUPPORTS: 如果存在一个事务,支持当前事务。如果没有事务,则非事务的执行。
(3)PROPAGATION_MANDATORY: 如果已经存在一个事务,支持当前事务。如果没有一个活动的事务,则抛出异常。
(4)PROPAGATION_REQUIRES_NEW: 总是开启一个新的事务。如果一个事务已经存在,则将这个存在的事务挂起。
(5)PROPAGATION_NOT_SUPPORTED: 总是非事务地执行,并挂起任何存在的事务。
(6)PROPAGATION_NEVER: 总是非事务地执行,如果存在一个活动事务,则抛出异常。
(7)PROPAGATION_NESTED:如果一个活动的事务存在,则运行在一个嵌套的事务中.
如果没有活动事务, 则按TransactionDefinition.PROPAGATION_REQUIRED 属性执行

(9)在业务层调用数据访问层
(a)创建接口com.tarena.biz.IUserServ
(b)创建实现类com.tarena.biz.impl.UserServImpl
private IUserDao iuserDao;
set(){
}
(c)在spring配置文件中完成依赖关系配置
<!-- biz -->
<bean id="userServImpl" class="com.tarena.biz.impl.UserServImpl">
<!--IUserDao iuserDao = new UserDaoImpl()-->
<property name="iuserDao" ref="userDaoImpl"/>
</bean>
(10)使用junit测试业务层
(11)在表示层调用业务层
(1)添加属性
private IUserServ iuserServ;
set(){
}

(2)在spring 配置文件中配置

<bean id="saveUserAction"
class="com.tarena.web.action.SaveUserAction">
<!--IUserServ iuserServ = new UserServImpl()-->
<property name="iuserServ" ref="userServImpl"/>
</bean>


(12)整体测试项目(一定OK)


(13)spring Test 使用注解方式
(a)添加spring 测试类库
spring-test.jar
junit-4.4.jar
注意: 本版本只支持spring2.5.6
(b)参看UserDaoImplTest.java类
//添加测试类
@RunWith(SpringJUnit4ClassRunner.class)
//加载spring容器中的对象
@ContextConfiguration(
locations = {
"classpath:config/spring/applicationContext.xml"
})
//set注入:相当于显示提供set方法
@Autowired
private IUserDao iuserDao;
注解优势:编写方便
缺点:注解依赖代码类,后期维护需要修改代码,还需要
重新对java类进行编译和大量测试
注解目前不成熟和稳定,版本变化比较快,存在兼容性
问题

建议:在测试代码中可以使用注解

14、分层管理spring配置文件

applicationContext.xml:spring公共信息
applicationContext-web.xml管理struts2的action
applicationContext-biz.xml管理业务层的Bean
applicationContext-dao.xml管理数据访问层Bean
方式一引入:
在applicationContext.xml中引入其他spring配置文件
<import resource="applicationContext-web.xml"/>
<import resource="applicationContext-biz.xml"/>
<import resource="applicationContext-dao.xml"/>
方式二引入:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
可以使用统配符号*
classpath:config/spring/applicationContext*.xml
</param-value>
</context-param>
15、spring如何解决hibernate延迟问题
以前解决延迟:
(1)Lazy不使用延迟,让hibernate立即加载
(2)自定一个拦截器或者过滤器
(3)可以使用spring内部提供openSessionInViewFilter来解决
懒加载问题
(a)在web.xml文件中配置过滤器
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
<init-param>
<!--openSessionInViewFilter默认数据源名字叫sessionFactory
如果数据源名字不叫sessionFactory,必须制定
sessionFactoryBeanName的value值
-->
<param-name>sessionFactoryBeanName</param-name>
<param-value>sessionFactory</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

//load
16、spring中文过滤器
(1)自定一个过滤器
(2)struts2
<constant name="struts.i18n.encoding" value="UTF-8"/>
(3)其他框架(除struts2框架)
使用spring过滤器,在web.xml文件中配置filter
<filter>
<filter-name>characterEncodingFilter</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>

<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

17、spring读取属性文件
(1)注入spring读取属性类(appliactionContext.xml)
<bean id="propertyPlaceholderConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:config/props/db.properties</value>
</list>
</property>
</bean>
(2)创建config/props/db.properties文件
(3)通过key==属性文件中(value)执行
${key}=value


【如果没有jar包,请联系756326005(雨一直下),希望可以帮到你】
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值