OA学习笔记-006-SPRING2.5与hibernate3.5整合
生活随笔
收集整理的這篇文章主要介紹了
OA学习笔记-006-SPRING2.5与hibernate3.5整合
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一、為什么要整合
1,管理SessionFactory實(shí)例(只需要一個(gè))
2,聲明式事務(wù)管理
spirng的作用
IOC 管理對(duì)象..
AOP 事務(wù)管理..
二、整合步驟
1.整合sessionFactory
在applicationContext.xml添加
1 <!-- 導(dǎo)入外部的properties文件 --> 2 <context:property-placeholder location="classpath:jdbc.properties"/> 3 4 <!-- 配置SessionFactory --> 5 <!-- bean默認(rèn)是單例的 --> 6 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 7 <!-- 指定hibernate的配置文件位置 --> 8 <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> 9 <!-- 配置c3p0數(shù)據(jù)庫(kù)連接池 --> 10 <property name="dataSource"> 11 <!-- 因?yàn)檫B接信息只有連接池用,所以配置在匿名的bean中 --> 12 <bean class="com.mchange.v2.c3p0.ComboPooledDataSource"> 13 <!-- 數(shù)據(jù)連接信息 --> 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 <!--初始化時(shí)獲取三個(gè)連接,取值應(yīng)在minPoolSize與maxPoolSize之間。Default: 3 --> 20 <property name="initialPoolSize" value="3"></property> 21 <!--連接池中保留的最小連接數(shù)。Default: 3 --> 22 <property name="minPoolSize" value="3"></property> 23 <!--連接池中保留的最大連接數(shù)。Default: 15 --> 24 <property name="maxPoolSize" value="5"></property> 25 <!--當(dāng)連接池中的連接耗盡的時(shí)候c3p0一次同時(shí)獲取的連接數(shù)。Default: 3 --> 26 <property name="acquireIncrement" value="3"></property> 27 <!-- 控制數(shù)據(jù)源內(nèi)加載的PreparedStatements數(shù)量。如果maxStatements與maxStatementsPerConnection均為0,則緩存被關(guān)閉。Default: 0 --> 28 <property name="maxStatements" value="8"></property> 29 <!--maxStatementsPerConnection定義了連接池內(nèi)單個(gè)連接所擁有的最大緩存statements數(shù)。Default: 0 --> 30 <property name="maxStatementsPerConnection" value="5"></property> 31 <!--最大空閑時(shí)間,1800秒內(nèi)未使用則連接被丟棄。若為0則永不丟棄。Default: 0 --> 32 <property name="maxIdleTime" value="1800"></property> 33 </bean> 34 </property> 35 </bean>?
2.配置聲明式事務(wù)管理
在applicationContext.xml添加
1 <!-- 配置聲明式事務(wù)管理(采用注解的方式,方便)--> 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.測(cè)試
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; // 這行會(huì)拋異常 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 // 測(cè)試SessionFactory 12 @Test 13 public void testSessionFactory() throws Exception { 14 SessionFactory sessionFactory = (SessionFactory) ac.getBean("sessionFactory"); 15 System.out.println(sessionFactory); 16 } 17 18 // 測(cè)試事務(wù) 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>?
轉(zhuǎn)載于:https://www.cnblogs.com/shamgod/p/5225454.html
總結(jié)
以上是生活随笔為你收集整理的OA学习笔记-006-SPRING2.5与hibernate3.5整合的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Mac OS X下安装nvm的方法
- 下一篇: 冒泡排序算法。