1. 程式人生 > >Spring+Hibernate配置檔案-applicationContext.xml設定

Spring+Hibernate配置檔案-applicationContext.xml設定

搭建完整個工程之後,我們開啟applicationContext.xml檔案,在這個配置檔案中主要涉及的有Spring的相關配置以及Hibernate的相關配置。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:p="http://www.springframework.org/schema/p"
	 xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">


<!-- 定義使用C3P0連線池的資料來源 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<!-- 指定連線資料庫的JDBC驅動 -->
		<property name="driverClass">
		 	<value>com.mysql.jdbc.Driver</value>
		 </property>
		<!-- 連線資料庫所用的URL -->
		<property name="jdbcUrl">
 			<value>jdbc:mysql://115.28.91.201:3306/xiudu?useUnicode=true&characterEncoding=utf-8</value>
 	    </property>
		<!-- 連線資料庫的使用者名稱 -->
		<property name="user">
			<value>root</value>
		</property>
		<!-- 連線資料庫的密碼 -->
		<property name="password">
			<value>root</value>
		</property>
		<!-- 設定資料庫連線池的最大連線數 -->
		<property name="maxPoolSize">
			<value>20</value>
		</property>
		<!-- 設定資料庫連線池的最小連線數 -->
		<property name="minPoolSize">
			<value>2</value>
		</property>
		<!-- 設定資料庫連線池的初始化連線數 -->
		<property name="initialPoolSize">
			<value>2</value>
		</property>
		<!-- 設定資料庫連線池的連線的最大空閒時間,單位為秒 -->
		<property name="maxIdleTime">
			<value>20</value>
		</property>
	</bean>
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
			</props>
		</property>
	</bean></beans>

初始工程中application主要是由資料庫的連線,這裡使用C3PO進行資料庫的連線,C3PO連線池是一個開源的JDBC連線池。其在Spring中的配置如上圖。

下面是關於C3PO的配置,轉自http://blog.csdn.net/caihaijiang/article/details/6843496