码迷,mamicode.com
首页 > 编程语言 > 详细

OA学习笔记-006-SPRING2.5与hibernate3.5整合

时间:2016-02-28 21:13:39      阅读:354      评论:0      收藏:0      [点我收藏+]

标签:

一、为什么要整合

1,管理SessionFactory实例(只需要一个)
2,声明式事务管理

spirng的作用

IOC 管理对象..
AOP 事务管理..

二、整合步骤

1.整合sessionFactory

在applicationContext.xml添加

 1 <!-- 导入外部的properties文件 -->
 2     <context:property-placeholder location="classpath:jdbc.properties"/>
 3 
 4     <!-- 配置SessionFactory -->
 5     <!-- bean默认是单例的 -->
 6     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
 7         <!-- 指定hibernate的配置文件位置 -->
 8         <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
 9         <!-- 配置c3p0数据库连接池 -->
10         <property name="dataSource">
11             <!-- 因为连接信息只有连接池用,所以配置在匿名的bean中 -->
12             <bean class="com.mchange.v2.c3p0.ComboPooledDataSource">
13                 <!-- 数据连接信息 -->
14                 <property name="jdbcUrl" value="${jdbcUrl}"></property>
15                 <property name="driverClass" value="${driverClass}"></property>
16                 <property name="user" value="${user}"></property>
17                 <property name="password" value="${password}"></property>
18                 <!-- 其他配置 -->
19                 <!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
20                 <property name="initialPoolSize" value="3"></property>
21                 <!--连接池中保留的最小连接数。Default: 3 -->
22                 <property name="minPoolSize" value="3"></property>
23                 <!--连接池中保留的最大连接数。Default: 15 -->
24                 <property name="maxPoolSize" value="5"></property>
25                 <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
26                 <property name="acquireIncrement" value="3"></property>
27                 <!-- 控制数据源内加载的PreparedStatements数量。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 -->
28                 <property name="maxStatements" value="8"></property>
29                 <!--maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 -->
30                 <property name="maxStatementsPerConnection" value="5"></property>
31                 <!--最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
32                 <property name="maxIdleTime" value="1800"></property>
33             </bean>
34         </property>
35     </bean>

 

2.配置声明式事务管理

在applicationContext.xml添加

1     <!-- 配置声明式事务管理(采用注解的方式,方便)-->
2     <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
3         <property name="sessionFactory" ref="sessionFactory"></property>
4     </bean>
5     <tx:annotation-driven transaction-manager="txManager"/>

 

3.测试

TestService.java

 1 @Service("testService")
 2 public class TestService {
 3 
 4     @Resource
 5     private SessionFactory sessionFactory;
 6 
 7     @Transactional
 8     public void saveTwoUsers() {
 9         Session session = sessionFactory.getCurrentSession();
10 
11         session.save(new User("李白"));
12         //int a = 1 / 0; // 这行会抛异常
13         session.save(new User("杜甫"));
14     }
15 }

 

TestAction.java

 1 //@Component("testAction")
 2 //@Service
 3 //@Repository
 4 @Controller("testAction")
 5 @Scope("prototype")
 6 public class TestAction extends ActionSupport {
 7 
 8     @Resource
 9     private TestService testService;
10     
11     @Override
12     public String execute() throws Exception {
13         System.out.println("---> TestAction.execute()");
14         testService.saveTwoUsers();
15         return "success";
16     }
17 }

3.SpringTest.java

 1 public class SpringTest {
 2 
 3     private ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
 4 
 5     @Test
 6     public void testBean() throws Exception {
 7         TestAction testAction = (TestAction) ac.getBean("testAction");
 8         System.out.println(testAction);
 9     }
10 
11     // 测试SessionFactory
12     @Test
13     public void testSessionFactory() throws Exception {
14         SessionFactory sessionFactory = (SessionFactory) ac.getBean("sessionFactory");
15         System.out.println(sessionFactory);
16     }
17 
18     // 测试事务
19     @Test
20     public void testTransaction() throws Exception {
21         TestService testService = (TestService) ac.getBean("testService");
22         testService.saveTwoUsers();
23     }
24 }

 

4.User.hbm.xml  

 1 <?xml version="1.0"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC
 3         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4         "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 5 
 6 <hibernate-mapping package="cn.itcast.oa.domain">
 7 
 8     <class name="User" table="itcast_user">
 9         <id name="id">
10             <generator class="native"/>
11         </id>
12         <property name="name" />
13     </class>
14     
15 </hibernate-mapping>

 

OA学习笔记-006-SPRING2.5与hibernate3.5整合

标签:

原文地址:http://www.cnblogs.com/shamgod/p/5225454.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!