Spring整合hibernate需要整合些什么?
- 由IOC容器来生成hibernate的sessionFactory.
- 让hibernate使用spring的声明式事务
整合步骤:
- 加入hibernate:
1.导jar包
2.配置hibernate的配置文件,hibernate.cfg.xml.(其实可以省略这个配置文件,但还是加上比较舒服):
<?xml version="1.0"?><!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> <!-- 配置hibernate的基本属性 --> <!-- 配置数据源 ,因为需要把数据源配置到IOC容器中,所以这里不需要再配置--> <!-- 配置数据库方言 --> <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> <!-- 显示并格式化sql语句 --> <property name="show_sql">true</property> <property name="format_sql">true</property> <!-- 自动生成数据库表结构 --> <property name="hbm2ddl.auto">update</property> <!-- 使用getCurrentSession方式打开会话 --> <property name="hibernate.current_session_context_class">thread</property> <!-- 关联的*.hbm.xml也在容器配置sessionFactory实例时在进行配置,这里也不再配置 --> </session-factory></hibernate-configuration>3.编写持久化类以及对应的hbm.xml.
- 加入Spring
1.导入jar包.
2.配置spring配置文件(重点,详见注释):
beans.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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"> <!-- 导入资源文件 --> <context:property-placeholder location="db.properties" ignore-resource-not-found="false" /> <!-- 配置c3p0连接池 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="user" value="${jdbc.user}"></property> <property name="password" value="${jdbc.password}"></property> <property name="driverClass" value="${jdbc.driverClass}"></property> <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property> <property name="initialPoolSize" value="${jdbc.initialPoolSize}"></property> <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property> </bean> <!--配置hibernate的SessionFactory实例,通过Spring提供的LocalSessionFactorybean进行配置 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <!-- 配置数据源属性 --> <property name="dataSource" ref="dataSource"></property> <!-- 配置hibernate.cfg.xml的位置及名称--> <property name="configLocation" value="hibernate.cfg.xml"></property> <!-- 配置hibernate映射文件的位置及名称,可以使用通配符--> <property name="mappingLocations" value="com/wang/entity/*.hbm.xml"></property> </bean> <!-- 配置Spring的声明式事务 --> <!-- 1.配置事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 2.配置事务属性,需要事务管理器 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="*"/> <tx:method name="get*" read-only="true"/> </tx:attributes> </tx:advice> <!-- 3.配置事务切点,并把切点和事务属性关联起来 --> <aop:config> <aop:pointcut expression="execution(* com.wang.service.*.*(..))" id="pointcut"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/> </aop:config></beans>到此,配置工作就完成了,接下来可以测试一下代码,看看自动生成的数据库表.
如果你选择不使用hibernate.cfg.xml,那么可以在上面的beans.xml中,删掉<property name="configLocation" value="hibernate.cfg.xml"></property>,同时添加以下代码:
<property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property>
版权声明
本文仅代表作者观点,不代表百度立场。
本文系作者授权百度百家发表,未经许可,不得转载。


