開始前先閱讀下文:
http://blog.csdn.net/frankcheng5143/article/details/50634487
1. hibernate.cfg.xml不在需要數(shù)據(jù)庫連接的配置婆瓜,刪掉
刪除前:
<?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>
<!-- 配置Hibernate的基本屬性 -->
<!-- 1.數(shù)據(jù)源配置到IOC容器中 -->
<!-- 2.關(guān)聯(lián)的.hbm.xml也在IOC容器配置SessionFactory實例 -->
<!-- 3.配置Hibernate的基本屬性:方言蔚叨,SQL顯示及格式化哟玷,生成數(shù)據(jù)表的策略以及二級緩存 -->
<property name="connection.url">jdbc:mysql://xxx.xxx.xxx.xxxx:3306/xxxxx</property>
<property name="connection.username">root</property>
<property name="connection.password">xxxxxxxx</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- 數(shù)據(jù)庫連接池的大小 -->
<property name="connection.pool_size">5</property>
<!-- 每次從數(shù)據(jù)庫中取出并放到JDBC的Statement中的記錄條數(shù)。Fetch Size設(shè)的越大,讀數(shù)據(jù)庫的次數(shù)越少沮尿,速度越快蕾羊,F(xiàn)etch Size越小,讀數(shù)據(jù)庫的次數(shù)越多避矢,速度越慢-->
<property name="jdbc.fetch_size">50 </property>
<!--批量插入,刪除和更新時每次操作的記錄數(shù)悼瘾。Batch Size越大,批量操作的向數(shù)據(jù)庫發(fā)送Sql的次數(shù)越少审胸,速度就越快亥宿,同樣耗用內(nèi)存就越大-->
<property name="jdbc.batch_size">23 </property>
<!-- SQL 方言 -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- 在控制臺輸出sql語句 -->
<property name="show_sql">true</property>
<!-- 在啟動時根據(jù)配置更新數(shù)據(jù)庫 -->
<property name="hbm2ddl.auto">update</property>
<mapping class="com.xxxx.fabu.model.CheckItem"/>
<mapping class="com.xxxx.fabu.model.User"/>
<mapping class="com.xxxx.fabu.model.CheckInfo"/>
</session-factory>
</hibernate-configuration>
修改后:
<?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>
<!-- 數(shù)據(jù)庫連接池的大小 -->
<property name="connection.pool_size">5</property>
<!-- 每次從數(shù)據(jù)庫中取出并放到JDBC的Statement中的記錄條數(shù)。Fetch Size設(shè)的越大砂沛,讀數(shù)據(jù)庫的次數(shù)越少烫扼,速度越快,F(xiàn)etch Size越小碍庵,讀數(shù)據(jù)庫的次數(shù)越多映企,速度越慢-->
<property name="jdbc.fetch_size">50 </property>
<!--批量插入,刪除和更新時每次操作的記錄數(shù)。Batch Size越大怎抛,批量操作的向數(shù)據(jù)庫發(fā)送Sql的次數(shù)越少卑吭,速度就越快,同樣耗用內(nèi)存就越大-->
<property name="jdbc.batch_size">23 </property>
<!-- SQL 方言 -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- 在控制臺輸出sql語句 -->
<property name="show_sql">true</property>
<!-- 在啟動時根據(jù)配置更新數(shù)據(jù)庫 -->
<property name="hbm2ddl.auto">update</property>
<mapping class="com.xxxx.fabu.model.CheckItem"/>
<mapping class="com.xxxx.fabu.model.User"/>
<mapping class="com.xxxx.fabu.model.CheckInfo"/>
</session-factory>
</hibernate-configuration>
2. 創(chuàng)建database.properties文件
##在applicationContext.xml文件之中引入此配置的資源文件
db.url=jdbc:mysql://xxx.xxx.xxx.xxx:3306/xxxx
db.user=root
db.password=xxxxxx
pool.max=100
pool.min=20
pool.init=10
pool.idle=100
3. 在繼續(xù)配置前先添加必要的命名空間
<?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"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context/spring-context.xsd">
<!--在當前的配置之中采用Annotation的注解方式進行定義-->
<context:annotation-config/>
<!--定義掃描包马绝,此包中定義的所有的程序類支持Annotation-->
<context:component-scan base-package="com.xxxx"/>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"/>
</bean>
</beans>
4.配置數(shù)據(jù)庫連接池
在applicationContext.xml文件中添加上文的database.properties配置文件
<context:property-placeholder location="classpath:database.properties" />
配置dataSource豆赏、sessionFactory,最后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"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context/spring-context.xsd">
<!--在當前的配置之中采用Annotation的注解方式進行定義-->
<context:annotation-config/>
<!--定義掃描包富稻,此包中定義的所有的程序類支持Annotation-->
<context:component-scan base-package="com.xxxx"/>
<!--設(shè)置要導(dǎo)入的資源文件路徑掷邦,直接通過classpath加載-->
<context:property-placeholder location="classpath:database.properties"/>
<!--配置數(shù)據(jù)庫連接池-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${db.driver}"/>
<property name="jdbcUrl" value="${db.url}"/>
<property name="user" value="${db.user}"/>
<property name="password" value="${db.password}"/>
<property name="maxPoolSize" value="${pool.max}"/>
<property name="minPoolSize" value="${pool.min}"/>
<property name="initialPoolSize" value="${pool.init}"/>
<property name="maxIdleTime" value="${pool.idle}"/>
</bean>
<!--配置hibernate中的SessionFactory程序類,此時將通過spring負責(zé)管理-->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!--配置hibernate.cfg.xml文件的引用路徑椭赋,通過classpath引用-->
<property name="configLocation" value="classpath:hibernate.cfg.xml"/>
<!--SessionFactory的執(zhí)行需要dataSource支持抚岗,將之前的c3p0的數(shù)據(jù)源配置進來-->
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
5.事務(wù)
<!--配置Hibernate的事務(wù)控制,同時可以實現(xiàn)數(shù)據(jù)庫的關(guān)閉處理-->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>