Spring与Hibernate整合(配置模式)
来源:互联网 发布:河南安全教育网络平台 编辑:程序博客网 时间:2024/06/05 08:43
Spring与Hibernate整合的作用:
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>
阅读全文
0 0
- Spring与Hibernate整合(配置模式)
- Spring与Hibernate整合配置
- Spring与Hibernate整合(注解模式)
- Spring与Hibernate整合的配置
- Spring与Hibernate整合中的数据源配置
- Spring与Hibernate 整合 xml配置
- Spring+Hibernate 整合配置
- Spring+Hibernate整合配置
- Spring+Hibernate整合配置
- Spring+Hibernate整合配置
- Spring整合hibernate配置
- spring与hibernate整合
- Spring与Hibernate整合
- hibernate 与spring 整合
- Spring与Hibernate整合
- spring与hibernate整合
- Spring 与Hibernate 整合
- Spring与Hibernate整合
- css3 圆角
- java原生序列化和Kryo(dubbo)序列化性能比较
- CODEVS 1011 数的计算 & 2001NOIP普及组T1
- 洛谷 P1944 最长括号匹配_NOI导刊2009提高(1)
- jsp九大类值对象四大作用域
- Spring与Hibernate整合(配置模式)
- HDU
- 树梅派 内存交换空间swap 更改
- 函数作用域链
- 数据结构与算法:图
- POJ 1696 Space Ant(凸包变形)
- Call to undefined function imagecreatefromjpeg()
- MOOC清华《面向对象程序设计》第7章:负载监视器的设计v2.0(采用基于模板的策略模式)
- zencart 批量删除无图片产品 优化网站