- 论坛徽章:
- 0
|
一、数据访问
1、Spring的数据访问设计思想 2、数据源配置:
方式一:Spring内置实现 DriverManagerDataSource
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/hibdb
root
windows
方式二:DBCP提供的BasicDataSource
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/hibdb
root
windows
方式三:JNDI数据源 JNDI数据源:(mysql5,tomcat5.5)
step1:
在server.xml中:
step2:
在context.xml中(conf\context.xml):
step3:
在beans-config.xml:
java:comp/env/jdbc/mydatasource
3、JDBC支持:
step1: 配置数据源
step2: 配置JdbcTemplate
step3:配置DAO
注意: 查询时,使用RowMapper
4、hibernate支持:
step1: 配置数据源
step2: 配置sessionfactory
lab6/Order.hbm.xml
org.hibernate.dialect.MySQLDialect
true
step3: 配置DAO
注意:以上配置是要求dao 继承HibernateDaoSupport
二、事务:
1、Spring事务机制
声明式事务、事务管理器
2、hibernate事务
step1: 配置数据源
step2:配置sessionfactory (同上)
step3:配置事务管理器
step4:创建事务服务代理
lab7.SaleService
PROPAGATION_REQUIRED
注:
事务属性描述格式:
传播行为,隔离级别,只读事务(readonly),回滚规则
在默认情况下,Spring的容器对于非受查异常(服务模块中抛出的非受查异常)
,会回滚事务。对于受查异常,会提交事务。
如果即使发生了某种受查异常,也要回滚事务,可以用 “- 异常类型“来声明。
同样,对于非受查异常,如果不要求回滚事务,可以用"+异常类型"来声明
3、简化事务配置
继承、自动代理
三、Spring与struts整合:
前提:
配置ContextLoaderPlugIn插件:负责装载spring应用上下文
方式一:通过Spring的ActionSupport类
ActionSupport类:
知道ApplicationContext的获得方式。
步骤:
1、Action直接继承ActionSupport
2、使用ApplicationContext ctx = getWebApplicationContext();取得Spring上下文
3、取得相应Bean
注意:有可能需要替换commons-attributes-compiler.jar包。
优点:
简单
缺点:
耦合高
违反IOC
无法使用多方法的Action
方式二:通过Spring的DelegatingActionProxy类
步骤:
1、Action中,使用IOC获得服务
2、配置struts-config.xml
3、在Spring配置文件中
注意,要用bean name命名。
/somepath:Action的path
优点:
不使用Spring api编写 Action
利用了IOC装配。
可以利用容器的scope="prototype"来保证每一个请求有一个单独的Action来处理,
避免struts中Action的线程安全问题。
缺点:
struts配置文件中,所有path都映射到同一个代理类
方式三:通过Spring的DelegatingRequestProcessor类
步骤:
1、Action中,使用IOC获得服务
2、配置struts-config.xml
3、在Spring配置文件中
小结:
Spring与Struts整合方式只有两种:
(1)由Spring容器来管理Action(方式二,方式三)
(2)Action处于容器之外(方式一)
注意:
中文问题:
设置过滤器,设置页面编码,数据库编码
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/67067/showart_573168.html |
|