OA学习笔记-006-SPRING2.5与hibernate3.5整合
一、为什么要整合
1,管理SessionFactory实例(只需要一个)
2,声明式事务管理
spirng的作用
IOC 管理对象..
AOP 事务管理..
二、整合步骤
1.整合sessionFactory
在applicationContext.xml添加
<!-- 导入外部的properties文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/> <!-- 配置SessionFactory -->
<!-- bean默认是单例的 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 指定hibernate的配置文件位置 -->
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<!-- 配置c3p0数据库连接池 -->
<property name="dataSource">
<!-- 因为连接信息只有连接池用,所以配置在匿名的bean中 -->
<bean class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 数据连接信息 -->
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<property name="driverClass" value="${driverClass}"></property>
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
<!-- 其他配置 -->
<!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialPoolSize" value="3"></property>
<!--连接池中保留的最小连接数。Default: 3 -->
<property name="minPoolSize" value="3"></property>
<!--连接池中保留的最大连接数。Default: 15 -->
<property name="maxPoolSize" value="5"></property>
<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
<property name="acquireIncrement" value="3"></property>
<!-- 控制数据源内加载的PreparedStatements数量。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 -->
<property name="maxStatements" value="8"></property>
<!--maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 -->
<property name="maxStatementsPerConnection" value="5"></property>
<!--最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime" value="1800"></property>
</bean>
</property>
</bean>
2.配置声明式事务管理
在applicationContext.xml添加
<!-- 配置声明式事务管理(采用注解的方式,方便)-->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
3.测试
TestService.java
@Service("testService")
public class TestService {
@Resource
private SessionFactory sessionFactory;
@Transactional
public void saveTwoUsers() {
Session session = sessionFactory.getCurrentSession();
session.save(new User("李白"));
//int a = 1 / 0; // 这行会抛异常
session.save(new User("杜甫"));
}
}
TestAction.java
//@Component("testAction")
//@Service
//@Repository
@Controller("testAction")
@Scope("prototype")
public class TestAction extends ActionSupport {
@Resource
private TestService testService;
@Override
public String execute() throws Exception {
System.out.println("---> TestAction.execute()");
testService.saveTwoUsers();
return "success";
}
}
3.SpringTest.java
public class SpringTest {
private ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
@Test
public void testBean() throws Exception {
TestAction testAction = (TestAction) ac.getBean("testAction");
System.out.println(testAction);
}
// 测试SessionFactory
@Test
public void testSessionFactory() throws Exception {
SessionFactory sessionFactory = (SessionFactory) ac.getBean("sessionFactory");
System.out.println(sessionFactory);
}
// 测试事务
@Test
public void testTransaction() throws Exception {
TestService testService = (TestService) ac.getBean("testService");
testService.saveTwoUsers();
}
}
4.User.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.itcast.oa.domain"> <class name="User" table="itcast_user">
<id name="id">
<generator class="native"/>
</id>
<property name="name" />
</class> </hibernate-mapping>
OA学习笔记-006-SPRING2.5与hibernate3.5整合的更多相关文章
- OA学习笔记-004-Spring2.5配置
一.jar包 (1)spring.jar (2)Aop包 aspectjrt.jaraspectjweaver.jar (3)动态代理 cglib-nodep-2.1_3.jar (4)日志 comm ...
- OA学习笔记-003-Hibernate3.6配置
一.jar包:核心包, 必须包, jpa, c3p0, jdbc antlr-2.7.6.jarc3p0-0.9.1.jarcommons-collections-3.1.jardom4j-1.6.1 ...
- OA学习笔记-009-岗位管理的CRUD
一.分析 Action->Service->Dao CRUD有功能已经抽取到BaseDaoImpl中实现,所以RoleDaoImpl没有CRUD的代码,直接从BaseDaoImpl中继承 ...
- OA学习笔记-008-岗位管理Action层实现
一.分析 1,设计实体/表 设计实体 --> JavaBean --> hbm.xml --> 建表 2,分析有几个功能,对应几个请求. 3,实现功能: 1,写Action类,写Ac ...
- OA学习笔记-005-Spring2.5与struts2.1整合
一.单独测试strust 1.action package cn.itcast.oa.test; import org.springframework.context.annotation.Scope ...
- OA学习笔记-001-项目介绍
基本知识 框架工具 解决方案(经典应用) 项目 12天 ========================================== OA项目, 12天 BBS 一.什么是OA? 辅助管理.提 ...
- OA学习笔记-010-Struts部分源码分析、Intercepter、ModelDriver、OGNL、EL
一.分析 二. 1.OGNL 在访问action前,要经过各种intercepter,其中ParameterFilterInterceptor会把各咱参数放到ValueStack里,从而使OGNL可以 ...
- OA学习笔记-007-Dao层设计
一. User, UserDao save(User user), update(), delete(), find(), ...Role, RoleDao save(Role role), upda ...
- OA学习笔记-002-Sruts2.1配置
一.jar commons-fileupload-1.2.1.jarcommons-io-1.3.2.jarfreemarker-2.3.15.jarognl-2.7.3.jarstruts2-cor ...
随机推荐
- verilog中的function用法与例子
函数的功能和任务的功能类似,但二者还存在很大的不同.在 Verilog HDL 语法中也存在函数的定义和调用. 1.函数的定义 函数通过关键词 function 和 endfunction 定义,不允 ...
- [转]前端开发必备 40款优秀CSS代码编写工具推荐
编写工具地址如下 英文地址:http://webtoolsdepot.com/40-css-tools-to-improve-your-productivity/ 中文地址:http://www.cs ...
- ios NSHashTable & NSMapTable
在ios开发中大家用到更多的集合类可能是像NSSet或者NSDictionary,NSArray这样的.这里要介绍的是更少人使用的两个类,一个是NSMapTable,另一个是NSHashTable. ...
- ios专题 - 委托模式实现
在ios中,委托模式非常常见,那委托模式是什么? 委托模式是把一个对象把请求给另一个对象处理. 下面见例子: #import <UIKit/UIKit.h> @protocol LQIPe ...
- mysql error笔记1
mysql视图问题: The user specified as a definer ('root'@'%') does not exist 原因:由于root用户对全局host无访问权限,给root ...
- eclipse中定位引用的源码
如图,在eclipse中,我想看BaseContoller是怎么实现的,将鼠标放上去,按住Ctrl单击左键就行了
- (转)linux多线程,线程的分离与结合
转自:http://www.cnblogs.com/mydomain/archive/2011/08/14/2138454.htm 线程的分离与结合 在任何一个时间点上,线程是可结合的(joi ...
- 记录一次mount问题
linux centos 6.5 _64 oracle 11.2g 今天接到一个客户电话说重启了服务器,数据库没有重启来,看了数据库的报错 没有找到control文件的路径,数据库启动到了 unmo ...
- 使用css3实现文章新闻列表排行榜(数字)
列举几个简单的文章排行榜数字效果 一:使用list-style来显示数字.圆点.字母或者图片 <style> li{width:300px; border-bottom: 1px dott ...
- postgresql info
DROP TYPE IF EXISTS info CASCADE;CREATE TYPE info AS (state int4,operator varchar,time TIMESTAMP(0) ...