网上看到几种spring配置hibernate的sessionFactory的方法,这里做下总结,同时自己也归纳了一种比较符合自己习惯的配置,以此记录,方便以后做配置的时候可以拿来用。
1.通过配置dataSource来配置sessionFactory
applicationContext.xml
<!-- 数据库配置 -->
<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>
<property name="mappingDirectoryLocations">
<list>
<value>
classpath:com/tukechina/mms/pojos
</value>
</list>
</property><span style="white-space:pre"> </span><!--补充:如果是使用 mappingResources 则属性要一个一个写 hbm 文件(“classpath*:”指向 WEB-INF/classes 目录)-->
<span style="white-space:pre"> </span><property name="mappingResources">
<list>
<value>classpath*:/test/domain/MyBean.hbm.xml</value>
<value>classpath*:/test/domain/BasicBean.hbm.xml</value>
</list>
<span style="white-space:pre"> </span></property><span style="white-space:pre"> </span><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14.0740737915039px; line-height: 26.0000019073486px;"></bean></span></span></span>
这种配置方式将hibernate的配置都集于spring配置中,所以spring的配置就会显示的比较长。
2.通过加载hibernate.cfg.xml来配置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>
<!-- 最大连接数 -->
<property name="hibernate.c3p0.max_size">20</property>
<!-- 最小连接数 -->
<property name="hibernate.c3p0.min_size">5</property>
<!-- 获得连接的超时时间,如果超过这个时间,会抛出异常,单位毫秒 -->
<property name="hibernate.c3p0.timeout">120</property>
<!-- 最大的PreparedStatement的数量 -->
<property name="hibernate.c3p0.max_statements">100</property>
<!-- 每隔120秒检查连接池里的空闲连接 ,单位是秒-->
<property name="hibernate.c3p0.idle_test_period">120</property>
<!-- 当连接池里面的连接用完的时候,C3P0一下获取的新的连接数 -->
<property name="hibernate.c3p0.acquire_increment">2</property>
<!-- 每次都验证连接是否可用 -->
<property name="hibernate.c3p0.validate">true</property>
<!-- 配置hbm文件--><span style="white-space:pre"> </span><mapping resource="com/shangx/pojos/User.hbm.xml" />
</session-factory>
</hibernate-configuration>
这种方式将spring和hibernate的配置分离了,但是每次修改数据库还是要去更改hibernate的配置,所以第三种方法做了修改,把数据源的配置交给spring,然后spring再去读取jdbc.properties的数据库配置
3.通过配置jdbc.properties文件分离数据库的配置
jdbc.properties
Mysqljdbc.driverClassName=com.mysql.jdbc.Driver
Mysqljdbc.url=jdbc:mysql://localhost/goodshool
Mysqljdbc.username=root
Mysqljdbc.password=root
# 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
"#"是
properties的注释
applicationContext.xml
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<!---引入数据库配置文件-><span style="white-space:pre"> </span><property name="location" value="classpath:jdbc.properties" />
</bean>
<!-- 数据库配置 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass">
<span style="white-space:pre"> </span><!--使用$可以直接获取属性值--> <span style="white-space:pre"> </span><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>
<!-- 最小连接数 -->
<property name="minPoolSize">
<value>5</value>
</property>
<!-- 达到最大连接数后可以增加的连接数 个 -->
<property name="acquireIncrement">
<value>2</value>
</property>
<!-- 最大连接数 -->
<property name="maxPoolSize">
<value>20</value>
</property>
<!-- 最大闲置时间 秒 -->
<property name="maxIdleTime">
<value>600</value>
</property>
<!-- 最大的PreparedStatement的数量 -->
<property name="maxStatements" value="100"></property>
<!-- 闲置的连接测试周期 (秒) -->
<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"/> <span style="white-space:pre"> </span> <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>
这种方式把数据源配置单独放到了jdbc.properties文件,便于维护,,但还不是我喜欢,所以我总结了的第四种
db.properties
db.driverClassName=com.mysql.jdbc.Driver
db.url=jdbc\:mysql\://localhost\:3306/test
db.username=root
db.password=root
application.xml
<!-- 数据库连接池c3p0配置 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="jdbcUrl" value="${db.url}" />
<property name="driverClass" value="${db.driverClassName}" />
<property name="user" value="${db.username}" />
<property name="password" value="${db.password}" />
<property name="maxPoolSize" value="40" />
<property name="minPoolSize" value="1" />
<property name="initialPoolSize" value="1" />
<property name="maxIdleTime" value="20" />
</bean>
<!-- SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
<!-- 自动扫描注解方式配置的hibernate类文件 -->
<property name="packagesToScan">
<list>
<value>cn.edu.school</value>
</list>
</property>
</bean>
这里因为我的hibernate使用的是注解的方式,所以我使用了一个扫描注解的方式,注解相比hbm的繁琐配置,,见人见智吧。
然后这里我把hibernate的一些实用配置还是继续放在hibernate的核心配置文件,感觉这样就没那么乱。
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 方言配置 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 是否在控制台显示sql -->
<property name="hibernate.show_sql">true</property>
<!-- 是否自动提交事务 -->
<property name="hibernate.connection.autocommit">true</property>
<!-- 指定Hibernate在何时释放JDBC链接 -->
<property name="hibernate.connection.release_mode">auto</property>
<!-- 缓存配置 -->
<!-- 是否开启查询缓存 -->
<property name="hibernate.cache.use_query_cache">true</property>
<property name="hibernate.cache.provider_configuration_file_resource_path">/ehcache.xml</property>
<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
<!-- 数据库批量查询最大数 -->
<property name="hibernate.jdbc.fetch_size">50</property>
<!-- 数据库批量更新,添加,删除操作最大数 -->
<property name="hibernate.jdbc.batch_size">20</property>
<property name="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</property>
<!-- 设置自动创建|更新|验证数据库表结构 -->
<property name="hibernate.hbm2ddl.auto">update</property>
</session-factory>
</hibernate-configuration>
以上是我总结的方式,没有那种就很好,看自己的使用习惯吧~~~晚安