1. jdbc.properties
#sqlserver
driverClass = net.sourceforge.jtds.jdbc.Driver
jdbcUrl = jdbc:jtds:sqlserver://localhost:1433/wyglxt
user = sa
password = 123456
hibernate.dialect = org.hibernate.dialect.SQLServerDialect
#mysql
#driverClass = com.mysql.jdbc.Driver
#jdbcUrl = jdbc:mysql://localhost:3306/wyglxt
#user = root
#password = root
#hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
2. applicationContext.xml(注入PropertyPlaceholderConfigurer这个Bean,同时配置properties文件的路径)
<?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:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!-- 开启spring的Annotation注解处理器 -->
<context:annotation-config />
<!-- 开启spring的Bean自动扫描机制来查找和管理Bean实例 -->
<context:component-scan base-package="com.wyglxt" />
<!-- 读取properties文件 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>
<!-- 配置数据源bean,使用C3P0数据源实现 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass">
<value>${driverClass}</value>
</property>
<property name="jdbcUrl">
<value>${jdbcUrl}</value>
</property>
<property name="user">
<value>${user}</value>
</property>
<property name="password">
<value>${password}</value>
</property>
<!-- 指定数据库连接池的最大连接数 -->
<property name="maxPoolSize" value="40"></property>
<!-- 指定数据库连接池的最小连接数 -->
<property name="minPoolSize" value="1"></property>
<!-- 指定数据库连接池的初始化连接数 -->
<property name="initialPoolSize" value="1"></property>
<!-- 指定连接数据库连接池的连接的最大空闲时间 -->
<property name="maxIdleTime" value="20"></property>
</bean>
<!-- 定义了Hibernate的SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="mappingResources">
<list>
<value>com/wyglxt/entity/Owner.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.jdbc.fetch.size">50</prop>
<prop key="hibernate.batch_size">50</prop>
<!-- 链接释放策略 on_close | after_transaction | after_statement | auto -->
<prop key="hibernate.connection.release_mode">after_transaction</prop>
</props>
</property>
</bean>
</beans>