Spring整合Hibernate,便是將Hibernate的SessionFactory交由Spring的IOC容器管理,同時也讓Hibernate使用上Spring的聲明式事務。mysql
須要如下配置:spring
一、在Spring配置文件applicationContext.xml中添加sessionFactory的配置。sql
二、配置聲明式事務管理數據庫
Spring整合Hibernate能夠分別使用各自的配置文件也能夠直接在Spring配置文件中配置Hibernate,棄用Hibernate配置文件(hibernate.cfg.xml)express
<?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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" 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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 配置c3p0數據庫鏈接池 --> <bean id="DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8"></property> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="user" value="root"></property> <property name="password" value="root"></property> <property name="initialPoolSize" value="10"></property> <property name="maxPoolSize" value="20"></property> <property name="maxIdleTime" value="1000"></property> </bean> <!-- 配置hibernate的sessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="DataSource"></property> <!-- spring整合hibernate能夠分別使用各自的配置文件 也能夠直接在spring配置文件中配置hibernate,棄用 hibernate配置文件(hibernate.cfg.xml) --> <!-- 指定hibernate的配置文件位置 --> <!-- <property name="configLocation" value="classpath:hibernate.cfg.xml"> </property> --> <!-- hibernate的配置 --> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> <!-- 映射配置 --> <property name="mappingResources"> <list> <value>cn/iborder/springlogin/Entity/User.hbm.xml</value> </list> </property> </bean> <!-- 事務管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <tx:annotation-driven transaction-manager="transactionManager" /> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="login" read-only="true"/> </tx:attributes> </tx:advice> <!-- 5.3 Aop配置: 攔截哪些方法(切入點表表達式) + 應用上面的事務加強配置 --> <aop:config> <aop:pointcut expression="execution(* cn.iborder.springlogin.Service.impl.UserService.*(..))" id="pt"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/> </aop:config> <bean id="userDAO" class="cn.iborder.springlogin.Dao.UserDAO"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean id="userService" class="cn.iborder.springlogin.Service.impl.UserService"> <property name="userDAO" ref="userDAO"></property> </bean> <bean id="userServlet" class="cn.iborder.springlogin.Servlet.UserServlet"> <property name="userService" ref="userService"></property> </bean> </beans>