Spring整合Hibernate

Spring整合Hibernate
一、整合目标
1.由IoC容器管理Hibernate的SessionFactory
2.让Hibernate使用Spring的声明式事务

二、整合步骤
先加入Hibernat,再加入Spring,再进行整合。
第一步:配置Hibernate
1.加入Hibernate相关的包
Hibernate的必需包
c3p0包和数据库驱动包
AspectJWeaver.jar
数据库驱动包

2.添加Hibernate的配置文件hibernate.cfg.xml
a.Hibernate的数据源配置可以拿到Spring中去配置,所以无需在hibernate.cfg.xml中配置。
b.关联的.hbm.xml文件也可以在Spring配置文件中配置SessionFactory时进行配置。
c.在hibernate.cfg.xml中可以配置sql方言,sql显示,自动生成表,二级缓存等内容

3.编写实体类和对应的hbm.xml映射文件。

第二步:加入Spring
1.加入Spring包。
Spring的jar包
aspectjweaver.jar
2.加入Spring的配置文件。
a.配置数据源
建立db.properties的资源文件,配置数据源的连接信息。
在Spring配置文件中导入db.properties <context:property-placehoder/>
配置实体化c3p0的数据源ComboPooledDataSource
(测试数据源配置成功)

<!--加载资源对象 -->
    <context:property-placeholder location="classpath:db.properties"/>
    <!-- 实例化c3p0对象 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user"  value="${user}"></property>
        <property name="password"  value="${password}"></property>
        <property name="jdbcUrl"  value="${jdbcUrl}"></property>
        <property name="driverClass" value="${driverClass}"></property>
        <property name="initialPoolSize" value="${initialPoolSize}"></property>
        <property name="minPoolSize"  value="${minPoolSize}"></property>
        <property name="maxPoolSize" value="${maxPoolSize}"></property>
    </bean>

b.配置Hibernate的SessionFactory——通过Spring提供的LocalSessionFactoryBean来配置
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!--配置数据源属性-->
<property name="dataSource" ref="dataSource"></property>
<!--配置Hibernate配置文件的位置-->
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<!--配置Hibernate映射文件的位置,可以使用通配符-->
<property name="mappingLocation" value="classpath:com/itnba/entities/*.hbm.xml"></property>
</bean>

<!-- 配置Hibernate的SessionFactory -->
    <bean class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" id="sessionFactory">
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
        <property name="mappingLocations" value="com/itnba/maya/bean/*.hbm.xml"></property>
    </bean>
    


c.配置Spring的声明式事务
配置事务管理器 -- HibernateTransactionManager
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
配置事务属性 -- 导入tx命名空间
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="*" />
</tx:attributes>
</tx:advice>
配置事务切点,并把切点和事务属性关联起来。--导入aop命名空间
<aop:config>
<aop:pointcut expression="execution(* com.itnba.service.*.*(..))" id="pointCut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
</aop:config>

<!-- 配置spring的声明性事务 -->
    <bean class="org.springframework.orm.hibernate5.HibernateTransactionManager" id="transactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <!-- 配置事务属性 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>
    <!-- 配置事务切入点 -->
    <aop:config>
        <aop:pointcut expression="execution(* com.itnba.maya.dao.*.*(..))" id="pointCut"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pointCut"/>
        
    </aop:config>

第三步:编写代码
1.在Spring配置文件中配置自动扫描的包
<context:componet-scan base-package="com.itnba.dao"></context:componet-scan>
2.dao类如下封装
@Repository
public class InfoDao implements IInfoDao{
@Autowired
private SessionFactory sessionFactory;

public Session getSession(){
sessionFactory.getCurrentSession();
}

public void select(String code){
Session session =getSession();
Info data = session.load(Info.class,"p001");
///.......
}
}
3.service类如下封装
@Service
public class InfoService implements IInfoService{
@Autowired
private InfoDao infoDao;

public void add(Info data){
//...
}
}

Spring整合Hibernate,可以不使用 Hibernate的配置文件,把Hibernate配置文件中的内容放在Spring的配置文件中。
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
....
</props>
</property>

Spring配置文件(总)

<?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/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
        
    <context:component-scan base-package="com.itnba.maya.daoImpl,com.itnba.maya.dao"></context:component-scan>
    <!--加载资源对象 -->
    <context:property-placeholder location="classpath:db.properties"/>
    <!-- 实例化c3p0对象 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user"  value="${user}"></property>
        <property name="password"  value="${password}"></property>
        <property name="jdbcUrl"  value="${jdbcUrl}"></property>
        <property name="driverClass" value="${driverClass}"></property>
        <property name="initialPoolSize" value="${initialPoolSize}"></property>
        <property name="minPoolSize"  value="${minPoolSize}"></property>
        <property name="maxPoolSize" value="${maxPoolSize}"></property>
    </bean>
    <!-- 配置Hibernate的SessionFactory -->
    <bean class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" id="sessionFactory">
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
        <property name="mappingLocations" value="com/itnba/maya/bean/*.hbm.xml"></property>
    </bean>
    
    <!-- 配置spring的声明性事务 -->
    <bean class="org.springframework.orm.hibernate5.HibernateTransactionManager" id="transactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <!-- 配置事务属性 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>
    <!-- 配置事务切入点 -->
    <aop:config>
        <aop:pointcut expression="execution(* com.itnba.maya.dao.*.*(..))" id="pointCut"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pointCut"/>
        
    </aop:config>
</beans>
原文地址:https://www.cnblogs.com/ermeng/p/6669541.html