<?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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--開啟自動掃描-->
<context:component-scan base-package="com.kaishengit"/>
<!--數(shù)據(jù)庫連接池-->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///mybatis?useSSL=false"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!--Hibernate的SessionFactory-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!--數(shù)據(jù)庫連接池-->
<property name="dataSource" ref="dataSource"/>
<!--hiberbate的相關(guān)配置-->
<property name="hibernateProperties">
<props>
<!--dialect 方言-->
<prop key="dialect">org.hibernate.dialect.MySQLDialect</prop>
<!--顯示Hibernate自動生成的SQL-->
<prop key="show_sql">true</prop>
</props>
</property>
<!--POJO類的注冊-->
<property name="packagesToScan" value="com.kaishengit.pojo"/>
</bean>
<!--事務(wù)管理器-->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
附加:沒有整合Spring時的Hibernate主配置文件
hiberate。
<?xml version="1.0" encoding="UTF-8" ?>
<!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>
<!--數(shù)據(jù)庫連接-->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql:///mybatis?useSSL=false</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<!--dialect 方言-->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!--數(shù)據(jù)庫連接池 C3P0 添加C3P0依賴-->
<property name="c3p0.max_size">2</property>
<property name="c3p0.min_size">2</property>
<property name="c3p0.timeout">5000</property>
<property name="c3p0.max_statements">100</property>
<property name="c3p0.idle_test_period">3000</property>
<property name="c3p0.acquire_increment">2</property>
<property name="c3p0.validate">false</property>
<!--顯示Hibernate自動生成的SQL-->
<property name="show_sql">true</property>
<property name="current_session_context_class">thread</property>
<!--開啟二級緩存 設(shè)置二級緩存的實現(xiàn)類-->
<property name="hibernate.cache.region.factory_class">
org.hibernate.cache.ehcache.EhCacheRegionFactory
</property>
<!--設(shè)置事務(wù)的隔離級別-->
<!--<property name="connection.isolation">4</property>-->
<!--映射文件的注冊-->
<mapping resource="hbm/User.hbm.xml"/>
<mapping resource="hbm/Address.hbm.xml"/>
<mapping resource="hbm/Teacher.hbm.xml"/>
<mapping resource="hbm/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>