OA学习笔记-006-SPRING2.5与hibernate3.5整合
2024-08-24 06:11:11
一、为什么要整合
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>
最新文章
- 织梦DedeCMS模板防盗的四种方法
- CUDA2.3-原理之任意长度的矢量求和与用事件来测量性能
- wpf 线程
- SimpleTemplate模板引擎开发
- hadoop2的automatic HA+Federation+Yarn配置的教程
- C++_快速排序
- NoSQL专家王涛访谈:为什么我们还要做一个NoSQL?
- iOS 苹果app提交 ITC.apps.validation.prerelease_build_missing
- XML解析之JAXP案例详解
- angular之指令
- Python 运维之路
- c# 未能加载文件或程序集
- Chemical table CodeForces - 1012B
- java Calendar日历类
- YQCB冲刺周第六天
- [RHEL] RHEL7.0 下 Postfix + Dovecot 实现邮件发送
- 连续取模-function
- iOS | 解决中文乱码
- 【sam复习】用sam实现后缀排序
- 对Storm ETL的初步思考