Spring与Hibernate整合(配置模式)
2017-09-14 20:25
369 查看
Spring与Hibernate整合的作用:
1、使用Spring的IOC容器管理SessionFactory对象
2、让Hibernate使用上Spring的声明式事务
Hibernate和Spring整合的时候的整合的关键点:SessionFactory
整合步骤:
(1)、添加jar包
Hibernate核心功能包
Spring的相关包
Spring和Hibernate整合的包
数据库驱动的相关包
c3p0连接池的相关包
(2)、在web.xml中配置Spring
(3)、在我们的src/config下面添加配置文件
bean-base.xml
hibernate.cfg.xml
第二种模式:
将Hibernate.cfg.xml放入bean-base.xml中
1、使用Spring的IOC容器管理SessionFactory对象
2、让Hibernate使用上Spring的声明式事务
Hibernate和Spring整合的时候的整合的关键点:SessionFactory
整合步骤:
(1)、添加jar包
Hibernate核心功能包
Spring的相关包
Spring和Hibernate整合的包
数据库驱动的相关包
c3p0连接池的相关包
(2)、在web.xml中配置Spring
<!--Spring的相关配置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/config/bean-*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
(3)、在我们的src/config下面添加配置文件
bean-base.xml
<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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--配置dao --> <bean id="userDao" class="com.wc.dao.impl.UserDao" p:sessionFactory-ref="sessionFactory"></bean> <!--配置Service --> <bean id="userService" class="com.wc.service.impl.UserService" p:userDao-ref="userDao"></bean> <!--将Hibernate中的连接数据库的相关信息配置到Spring中来 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="acquireIncrement" value="2"></property> <property name="maxPoolSize" value="100"></property> <property name="minPoolSize" value="2"></property> <property name="maxStatements" value="100"></property> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql:///test01"></property> <property name="user" value="root"></property> <property name="password" value="123456"></property> </bean> <!--Spring和Hibernate整合的关键点是SessionFactory的创建问题 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="configLocations" value="classpath:config/hibernate.cfg.xml"></property> </bean> <!--配置事务 --> <bean id="txManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <property name="dataSource" ref="dataSource"></property> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!--配置的是事物增强 --> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="*" read-only="false" /> </tx:attributes> </tx:advice> <!--配置的是aop --> <aop:config> <aop:pointcut expression="(execution (* com.wc.service.impl.*.*(..)))" id="pt" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="pt" /> </aop:config> </beans>
hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <!--下面这个是用来配置hibernate的 --> <hibernate-configuration> <!--配置的是一个会话的工厂 name:一般不配置--> <session-factory> <!--配置数据库的方言--> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property> <!--自动建表--> <property name="hibernate.hbm2ddl.auto">update</property> <!--展示sql--> <property name="hibernate.show_sql">true</property> <!--格式化sql--> <property name="hibernate.format_sql">true</property> <!--配置映射--> <mapping resource="com/wc/pojo/User.hbm.xml"/> </session-factory> </hibernate-configuration>
第二种模式:
将Hibernate.cfg.xml放入bean-base.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: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"> <!-- 配置dao --> <bean id="userDao" class="com.wc.dao.impl.UserDao" p:sessionFactory-ref="sessionFactory"> </bean> <!-- 配置Service --> <bean id="userService" class="com.wc.service.impl.UserService" p:userDao-ref="userDao"> </bean> <!-- 将Hibernate中的连接数据库的相关信息配置到Spring中来 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="acquireIncrement" value="2"></property> <property name="maxPoolSize" value="100"></property> <property name="minPoolSize" value="2"></property> <property name="maxStatements" value="100"></property> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql:///test01"></property> <property name="user" value="root"></property> <property name="password" value="123456"></property> </bean> <!--Spring和Hibernate整合的关键点是SessionFactory的创建问题 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <!--下面我要将hibernate的配置文件写到这个里面来--> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> <prop key="hibernaye.hbm2ddl.auto" >update</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> </props> </property> <!-- 编写hibernate的映射配置 --> <property name="mappingDirectoryLocations"> <list> <!-- 配置mapping映射文件的根路径就好了 --> <value>classpath:com/wc/pojo</value> </list> </property> </bean> <!-- 配置事务 --> <bean id="txManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <property name="dataSource" ref="dataSource"></property> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="*" read-only="false"/> </tx:attributes> </tx:advice> <!-- 配置AOp --> <aop:config> <aop:pointcut expression="(execution (* com.wc.service.impl.*.*(..)))" id="pt"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/> </aop:config> </beans>
相关文章推荐
- Hibernate与Spring整合,配置文件applicationContext.xml
- Spring 整合 Hibernate 的一处简化引入PO映射文件配置
- 关于搭建restful web service + springMVC + ibatis/hibernate的整合和XML配置意思和ssh+springMVC框架搭建
- spring+hibernate+jpa+Druid的配置文件,spring整合Druid
- spring整合hibernate配置文件
- SpringMVC+Apache Shiro+JPA(hibernate)整合配置
- Spring整合Struts2和Hibernate+Maven(二)之SSH的配置文件
- 自己动手写代码,整合Spring和Hibernate(二)之配置数据源
- SpringMVC+Apache Shiro+JPA(hibernate)案例教学(一)整合配置
- springmvc,spring,hibernate整合-分离配置参数
- Spring_hibernate_struts整合(集成配置)
- Spring 整合 Hibernate 的一处简化配置
- Hibernate 与Spring整合学习之bean.xml的配置
- Spring+springmvc+hibernate+redis整合配置文件
- Spring整合BoneCP+Hibernate配置数据连接池
- Spring整合Hibernate时,配置事务处理
- spring整合hibernate时配置文件错误
- Spring+Hibernate整合开发配置,完整实例
- Spring3.3 整合 Hibernate3、MyBatis3.2 配置多数据源/动态切换数据源 方法
- Spring和Hibernate使用maven整合时pom.xml的配置