spring和hibernate的整合

一、回忆spring和struts的整合

  加入struts2-spring-plugin-2.3.24.jar插件包即可,配置文件不需要整合,整合过程比较简单。


修改  web.xml
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

    </listener>

   ContextLoaderListener实现ServletContextListener接口,在服务器启动时加载,
   默认自动载入置于WEB-INF目录下的Spring配置文件applicationContext.xml


二、现在来看hibernate和spring的整合

1、关于需要引入的jar包在之前的ssh基本开发环境搭建的博客中有说明,这里不一一说明

2、修改applicationContext中的配置

 (1)引入申明式事务支持的XML命名空间  tx,并且引入xsd约束格式文件

  xmlns:tx="http://www.springframework.org/schema/tx"

  http://www.springframework.org/schema/tx
  http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

(2)配置dataSource

<!-- 配置数据源 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <!-- 初始化连接数量; -->
        <property name="initialSize" value="0" />
        <!-- 最大并发连接数 -->
        <property name="maxActive" value="20" />
        <!-- 最大空闲连接数 -->
        <property name="maxIdle" value="20" />
        <!-- 最小空闲连接数 -->
        <property name="minIdle" value="0" />
        <!-- 最大等待时长 -->
        <property name="maxWait" value="60000" />
        <!-- 超过时间限制是否回收 -->
        <property name="removeAbandoned" value="true" />
        <!-- 超过时间限制多长; -->
        <property name="removeAbandonedTimeout" value="180"/>     
          
          
        <!-- 数据源连接参数配置; -->
        <property name="username" value="${db.username}"/>     
        <property name="url" value="${db.url}"/>
        <property name="password" value="${db.password}"/>
        <property name="driverClassName" value="${db.driverClassName}"/>
    </bean>


这里的${db.username}叫做占位符,需要在外部配置文件中去配置相关的值。这里是关于jdbc  MySQL数据库的参数设置


   (3)sessionFactory配置
    <!-- 配置SessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>    
        <property name="packagesToScan" value="net.xinqushi.model.model"/>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
                <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
            </props>
        </property>
    </bean>

(4)读取外部属性文件,获取数据源参数

    <context:property-placeholder location="classpath:dataSource.properties/>

dataSource.properties:

db.username=jack
db.url=jdbc:mysql://localhost:3306/album
db.password=12345678
db.driverClassName=com.mysql.jdbc.Driver


hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=update
hibernate.format_sql=true


(5)配置事务管理器
    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean> 


 (6)初始化hibernateTemplate
  <bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">
   <property name="sessionFactory" ref="sessionFactory"></property>
  </bean>


(7)配置事务管理
        
    <!-- 定义切面 -->
    <aop:config>
        <aop:pointcut expression="execution(* net.xinqushi.service.impl.*.* (..))" id="txPointCut"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
    </aop:config>
      
    <!-- 声明式事务 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">            
        <tx:attributes>
     <tx:method name="add*" propagation="REQUIRED"/>
     <tx:method name="get*" read-only="true" propagation="REQUIRED"/>
        </tx:attributes>        
    </tx:advice>

这样配置完成之后,就不需要hibernate.cfg.xml这个关于hibernate本身的配置文件了。而且在DAO具体实现类当中不用HibernateUtil类中的sessionFactory。像获取session,关闭session,事务的begin和提交都由spring框架完成。至于具体的对数据库中数据的操作可以参考文档    hibernateTemplate操作数据的方法:http://www.blogjava.net/tbwshc/articles/380014.html


完整的applicationContext.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:aop="http://www.springframework.org/schema/aop"   
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:jee="http://www.springframework.org/schema/jee"  
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="    
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
        http://www.springframework.org/schema/mvc   http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd  
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
		<context:annotation-config></context:annotation-config>
	<context:component-scan base-package="net.xinqushi.dao.impl,net.xinqushi.service.impl,net.xinqushi.aop"/>
	<aop:config>
		<aop:aspect ref="log">
		<aop:pointcut 
			expression="execution(* net.xinqushi.service.impl.*.*(..))" id="mypt"/>
		<aop:before method="mybefore" pointcut-ref="mypt"/>
		<aop:after method="myafter" pointcut-ref="mypt"/>
		</aop:aspect>
	</aop:config>
	<!-- 注解配置AOP才会用到的 -->
	<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
	
	
	<!-- 配置数据源 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <!-- 初始化连接数量; -->
        <property name="initialSize" value="0" />
        <!-- 最大并发连接数 -->
        <property name="maxActive" value="20" />
        <!-- 最大空闲连接数 -->
        <property name="maxIdle" value="20" />
        <!-- 最小空闲连接数 -->
        <property name="minIdle" value="0" />
        <!-- 最大等待时长 -->
        <property name="maxWait" value="60000" />
        <!-- 超过时间限制是否回收 -->
        <property name="removeAbandoned" value="true" />
        <!-- 超过时间限制多长; -->
        <property name="removeAbandonedTimeout" value="180"/>     
              
        <!-- 数据源连接参数配置; -->
        <property name="username" value="${db.username}"/>     
        <property name="url" value="${db.url}"/>
        <property name="password" value="${db.password}"/>
        <property name="driverClassName" value="${db.driverClassName}"/>
    </bean>
	
	<!-- 读取外部文件 -->
	<context:property-placeholder location="classpath:dataSource.properties"/>
	<!-- 配置SessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>    
        <property name="packagesToScan" value="net.xinqushi.model"/>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
                <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
            </props>
        </property>
    </bean>
    
        <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean> 
    <!--  初始化hibernateTemplate -->
      <bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">
   <property name="sessionFactory" ref="sessionFactory"></property>
  </bean>
  
  
      <!-- 定义切面 -->
    <aop:config>
        <aop:pointcut expression="execution(* net.xinqushi.service.impl.*.* (..))" id="txPointCut"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
    </aop:config>
      
    <!-- 声明式事务 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">            
        <tx:attributes>
	      <tx:method name="add*" propagation="REQUIRED"/>
	      <tx:method name="get*" read-only="true" propagation="REQUIRED"/>
	      <tx:method name="is*" read-only="true" propagation="REQUIRED"/>
	      
        </tx:attributes>        
    </tx:advice>
</beans>





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值