首页 > 代码库 > spring配置hibernate的sessionFactory

spring配置hibernate的sessionFactory

之前用spring2+hibernate3+struts2开发了一个彩信发布系统,由于第一次使用此架构,造成applicationContext.xml中的配置非常冗长,而且经常因为更改一个小配置项(例:数据库ip、用户名、密码等)将此文件作修改,这及不利于项目维护,万一粗心造成其他地方变动,会对本来正常的项目造成bug 
    其实那个项目我最后做了分隔,将applicationContext.xml分隔成好几段,但是我觉得其实对于数据库方面的配置,完全可以通过加载hibernate.cfg.xml配置文件来配置项目的sessionFactory,所以这个新项目我决定使用此方式 
    这里介绍一下spring加载sessionFactory的这2种方式 
    1、通过配置dataSource来配置sessionFactory 
    applicationContext.xml 

 1     <!-- 数据库配置 -->
 2     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
 3         destroy-method="close">
 4         <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
 5         <property name="url" value="jdbc:mysql://192.168.0.2:3306/tuke_mmsmsys"></property>
 6         <property name="username" value="admin"></property>
 7         <property name="password" value="richard"></property>
 8 
 9         <!-- Connection Pooling Info -->
10         <property name="maxActive" value="20" />
11         <property name="maxIdle" value="5" />
12         <property name="maxWait" value="5000" />
13         <property name="validationQuery" value="select count(0) from admin" />
14     </bean>
15     <bean id="sessionFactory"
16         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
17         <property name="dataSource">
18             <ref bean="dataSource" />
19         </property>
20         <property name="hibernateProperties">
21             <props>
22                 <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
23                 <prop key="hibernate.show_sql">true</prop>
24             </props>
25         </property>
26         <property name="mappingDirectoryLocations">
27             <list>
28                 <value>
29                     classpath:com/tukechina/mms/pojos       
30                 </value>
31             </list>
32         </property>
33     </bean>

  2、通过加载hibernate.cfg.xml来配置sessionFactory 
    applicationContext.xml 

1     <bean id="sessionFactory"
2         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
3         <property name="configLocation" value="classpath:hibernate.cfg.xml">
4         </property>
5     </bean>

hibernate.cfg.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 3                                          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
 4 <hibernate-configuration>
 5     <session-factory name="mysql">
 6         <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
 7         <property name="hibernate.connection.password">1234</property>
 8         <property name="hibernate.connection.url">jdbc:mysql://localhost/goodshool</property>
 9         <property name="hibernate.connection.username">root</property>
10         <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
11         <property name="hibernate.show_sql">true</property>
12 
13         <!-- 最大连接数 -->
14         <property name="hibernate.c3p0.max_size">20</property>
15         <!-- 最小连接数 -->
16         <property name="hibernate.c3p0.min_size">5</property>
17         <!-- 获得连接的超时时间,如果超过这个时间,会抛出异常,单位毫秒 -->
18         <property name="hibernate.c3p0.timeout">120</property>
19         <!-- 最大的PreparedStatement的数量 -->
20         <property name="hibernate.c3p0.max_statements">100</property>
21         <!-- 每隔120秒检查连接池里的空闲连接 ,单位是秒-->
22         <property name="hibernate.c3p0.idle_test_period">120</property>
23         <!-- 当连接池里面的连接用完的时候,C3P0一下获取的新的连接数 -->
24         <property name="hibernate.c3p0.acquire_increment">2</property>
25 
26         <!-- 每次都验证连接是否可用 -->
27         <property name="hibernate.c3p0.validate">true</property>
28         <mapping resource="com/shangx/pojos/User.hbm.xml" />
29     </session-factory>
30 </hibernate-configuration>

对于第二种配置方案,找到的资料很少,大多数采用第一种,其实还有一种较好的配置 
    3、通过配置jdbc.properties文件分离数据库的配置

 jdbc.properties

 1 Mysqljdbc.driverClassName=com.mysql.jdbc.Driver
 2 Mysqljdbc.url=jdbc:mysql://localhost/goodshool
 3 Mysqljdbc.username=root
 4 Mysqljdbc.password=1234
 5 
 6 # second cache statistics
 7 hibernate.generate_statistics=true
 8 
 9 # Property that determines the Hibernate dialect
10 # (only applied with "applicationContext-hibernate.xml")
11 hibernate.dialect=org.hibernate.dialect.MySQLDialect
12 hibernate.show_sql=true

 applicationContext.xml

 1     <bean id="propertyConfigurer"
 2         class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
 3         <property name="location" value="classpath:jdbc.properties" />
 4     </bean>
 5 
 6     <!-- 数据库配置 -->
 7     <bean id="mysqlDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
 8         destroy-method="close">
 9         <property name="driverClass">
10             <value>${Mysqljdbc.driverClassName}</value>
11         </property>
12         <property name="jdbcUrl">
13             <value>${Mysqljdbc.url}</value>
14         </property>
15         <property name="user">
16             <value>${Mysqljdbc.username}</value>
17         </property>
18         <property name="password">
19             <value>${Mysqljdbc.password}</value>
20         </property>
21 
22         <!-- 最小连接数 -->
23         <property name="minPoolSize">
24             <value>5</value>
25         </property>
26         <!-- 达到最大连接数后可以增加的连接数 个 -->
27         <property name="acquireIncrement">
28             <value>2</value>
29         </property>
30         <!-- 最大连接数 -->
31         <property name="maxPoolSize">
32             <value>20</value>
33         </property>
34         <!-- 最大闲置时间 秒 -->
35         <property name="maxIdleTime">
36             <value>600</value>
37         </property>
38         <!-- 最大的PreparedStatement的数量 -->
39         <property name="maxStatements" value="100"></property>
40         <!-- 闲置的连接测试周期 (秒) -->
41         <property name="idleConnectionTestPeriod">
42             <value>120</value>
43         </property>
44     </bean>
45 
46     <bean id="sessionFactory"
47         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
48         <!--
49             <property name="configLocation" value="http://www.mamicode.com/classpath:hibernate.cfg.xml"
50             /> <property name="configurationClass"
51             value="http://www.mamicode.com/org.hibernate.cfg.AnnotationConfiguration" />
52         -->
53         <property name="dataSource">
54             <ref bean="mysqlDataSource" />
55         </property>
56         <property name="hibernateProperties">
57             <props>
58                 <prop key="hibernate.generate_statistics">
59                     ${hibernate.generate_statistics}
60                     </prop>
61                 <prop key="hibernate.dialect">
62                     ${hibernate.dialect}
63                 </prop>
64                 <prop key="hibernate.show_sql">
65                     ${hibernate.show_sql}
66                 </prop>
67             </props>
68         </property>
69         <property name="mappingDirectoryLocations">
70             <list>
71                 <value>
72                     classpath:com/shangx/pojos 
73                 </value>
74             </list>
75         </property>
76     </bean>

该文章原地址:http://james-t.iteye.com/blog/681136