SSH 壞境搭建(2)_整合Hibernate和Spring

整合Hibernate和Spring

1、管理SessionFactory 實例

2、聲明式管理



整合:

1、修改applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
				http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
				http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

	<!-- 自動掃描與裝配bean -->
	<context:component-scan base-package="cn.ssh.test"></context:component-scan>

	
	<!-- 導入外部的properties文件 -->
	<context:property-placeholder location="classpath:jdbc.properties"/>
	
	<!-- 配置SessionFactory -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<!-- 指定hibernate的配置文件位置 -->
		<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
		<!-- 配置c3p0數據庫連接池 -->
		<property name="dataSource">
			<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>


	<!-- 配置聲明式事務管理(採用註解的方式) -->
	<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<tx:annotation-driven transaction-manager="txManager"/>

</beans>

其中

jdbc.properties

內容爲:

jdbcUrl		= jdbc:mysql://localhost:3306/ItcastOA
driverClass	= com.mysql.jdbc.Driver
user		= root
password	=123

註釋掉hibernate.cfg.xml中的數據庫連接池對象

wKioL1OEdsyh_-IQAAE3jnrJz2w318.jpg2、在testSping中添加

	//測試sessionFactory
	@Test
	public void testSessionFactory() throws Exception {
		SessionFactory sessionFactory =(SessionFactory) ac.getBean("sessionFactory"); 
		System.out.println(sessionFactory);
	}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章