spring配置hibernate的sessionFactory
之前用spring2+hibernate3+struts2開(kāi)發(fā)了一個(gè)彩信發(fā)布系統(tǒng),由于第一次使用此架構(gòu),造成applicationContext.xml中的配置非常冗長(zhǎng),而且經(jīng)常因?yàn)楦囊粋€(gè)小配置項(xiàng)(例:數(shù)據(jù)庫(kù)ip、用戶名、密碼等)將此文件作修改,這及不利于項(xiàng)目維護(hù),萬(wàn)一粗心造成其他地方變動(dòng),會(huì)對(duì)本來(lái)正常的項(xiàng)目造成bug
其實(shí)那個(gè)項(xiàng)目我最后做了分隔,將applicationContext.xml分隔成好幾段,但是我覺(jué)得其實(shí)對(duì)于數(shù)據(jù)庫(kù)方面的配置,完全可以通過(guò)加載hibernate.cfg.xml配置文件來(lái)配置項(xiàng)目的sessionFactory,所以這個(gè)新項(xiàng)目我決定使用此方式
這里介紹一下spring加載sessionFactory的這2種方式
1、通過(guò)配置dataSource來(lái)配置sessionFactory applicationContext.xml
<!-- 數(shù)據(jù)庫(kù)配置 -->
<bean id="mydataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://192.168.0.2:3306/tuke_mmsmsys"></property>
<property name="username" value="admin"></property>
<property name="password" value="richard"></property>
<!-- Connection Pooling Info -->
<property name="maxActive" value="20" />
<property name="maxIdle" value="5" />
<property name="maxWait" value="5000" />
<property name="validationQuery" value="select count(0) from admin" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="mydataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
使用 mappingDirectoryLocations 屬性可以指定某目錄下的 hbm 文件(“classpath*:”指向 WEB-INF/classes 目錄)
<property name="mappingDirectoryLocations">
<list>
<value>
classpath:com/tukechina/mms/pojos
</value>
</list>
</property>
補(bǔ)充:使用 mappingResources 屬性要一個(gè)一個(gè)寫(xiě) hbm 文件(“classpath*:”指向 WEB-INF/classes 目錄)
<property name="mappingResources">
<list>
<value>classpath*:/test/domain/MyBean.hbm.xml</value>
<value>classpath*:/test/domain/BasicBean.hbm.xml</value>
</list>
</property>
</bean>
2、通過(guò)加載hibernate.cfg.xml來(lái)配置sessionFactory
applicationContext.xml
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml">
</property>
</bean>
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory name="mysql">
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">1234</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/goodshool</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<!-- 最大連接數(shù) -->
<property name="hibernate.c3p0.max_size">20</property>
<!-- 最小連接數(shù) -->
<property name="hibernate.c3p0.min_size">5</property>
<!-- 獲得連接的超時(shí)時(shí)間,如果超過(guò)這個(gè)時(shí)間,會(huì)拋出異常,單位毫秒 -->
<property name="hibernate.c3p0.timeout">120</property>
<!-- 最大的PreparedStatement的數(shù)量 -->
<property name="hibernate.c3p0.max_statements">100</property>
<!-- 每隔120秒檢查連接池里的空閑連接 ,單位是秒-->
<property name="hibernate.c3p0.idle_test_period">120</property>
<!-- 當(dāng)連接池里面的連接用完的時(shí)候,C3P0一下獲取的新的連接數(shù) -->
<property name="hibernate.c3p0.acquire_increment">2</property>
<!-- 每次都驗(yàn)證連接是否可用 -->
<property name="hibernate.c3p0.validate">true</property>
<mapping resource="com/shangx/pojos/User.hbm.xml" />
</session-factory>
</hibernate-configuration>
對(duì)于第二種配置方案,找到的資料很少,大多數(shù)采用第一種,其實(shí)還有一種較好的配置
3、通過(guò)配置jdbc.properties文件分離數(shù)據(jù)庫(kù)的配置 jdbc.properties
Mysqljdbc.driverClassName=com.mysql.jdbc.Driver
Mysqljdbc.url=jdbc:mysql://localhost/goodshool
Mysqljdbc.username=root
Mysqljdbc.password=1234
# second cache statistics
hibernate.generate_statistics=true
# Property that determines the Hibernate dialect
# (only applied with "applicationContext-hibernate.xml")
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
applicationContext.xml
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties" />
</bean> <!-- 數(shù)據(jù)庫(kù)配置 -->
<bean id="mysqlDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass">
<value>${Mysqljdbc.driverClassName}</value>
</property>
<property name="jdbcUrl">
<value>${Mysqljdbc.url}</value>
</property>
<property name="user">
<value>${Mysqljdbc.username}</value>
</property>
<property name="password">
<value>${Mysqljdbc.password}</value>
</property>
<!-- 最小連接數(shù) -->
<property name="minPoolSize">
<value>5</value>
</property>
<!-- 達(dá)到最大連接數(shù)后可以增加的連接數(shù) 個(gè) -->
<property name="acquireIncrement">
<value>2</value>
</property>
<!-- 最大連接數(shù) -->
<property name="maxPoolSize">
<value>20</value>
</property>
<!-- 最大閑置時(shí)間 秒 -->
<property name="maxIdleTime">
<value>600</value>
</property>
<!-- 最大的PreparedStatement的數(shù)量 -->
<property name="maxStatements" value="100"></property>
<!-- 閑置的連接測(cè)試周期 (秒) -->
<property name="idleConnectionTestPeriod">
<value>120</value>
</property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!--
<property name="configLocation" value="classpath:hibernate.cfg.xml"
/> <property name="configurationClass"
value="org.hibernate.cfg.AnnotationConfiguration" />
-->
<property name="dataSource">
<ref bean="mysqlDataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.generate_statistics">
${hibernate.generate_statistics}
</prop>
<prop key="hibernate.dialect">
${hibernate.dialect}
</prop>
<prop key="hibernate.show_sql">
${hibernate.show_sql}
</prop>
</props>
</property>
<property name="mappingDirectoryLocations">
<list>
<value>
classpath:com/shangx/pojos
</value>
</list>
</property>
</bean>