spring 配置hibernate

spring配置hibernate的配置文件

<?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:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	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.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">


	<!-- 为了系统识别相应的注解 -->
	<!--也是用于激活那些已经在spring容器里注册过的bean(无论是通过xml的方式 -->
	<!-- 还是通过package sanning的方式)上面的注解。 -->
	<context:annotation-config />


	<!--配置扫描包的路径,包含其子包 -->
	<context:component-scan base-package="com.XXX"></context:component-scan>


	<!--读取外部属性文件 -->
	<context:property-placeholder location="classpath:db.properties" />
	
	<!-- 使用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.initPoolSize}"></property>
		<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
	</bean>
	
	
	<!--用Spring帮你创建hibernate的SessionFactory(注解方式) -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<!-- 方式一
		<property name="annotatedClasses">
			<list>
				<value>com.XXX</value>
				<value>com.XXX</value>
			</list>
		</property>
		 -->
		 
		 <!--方式二、 可以自动扫描list中指定的包来加载实体类,不用一 一指明   --> 
		 <property name="packagesToScan">
			<list>
				<value>com.XXX</value>
			</list>
		</property>
		
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.MySQLDialect
				</prop>
				<prop key="hibernate.show_sql">true</prop>
			</props>
		</property>
		
	</bean>
	
	
	
	<!--hibernateTemplate封装了一系列的方法,比如save,update,等 -->
	<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	
	
	<!-- 定义事务管理器 -->
	<bean id="txManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	
	
	<!--  配置参与事务的类 -->
	<aop:config>
	
		<!-- 切面配置,aop:pointcut标签配置参与事务的类,由于是在Service中进行数据库业务操作, -->
		<!-- 配的应该是包含那些作为事务的方法的Service类。 -->
		<aop:pointcut id="bussinessService"
			expression="execution(public * com.XXX.*.*(..))" />
		
		<!--aop:advisor标签就是把上面我们所配置的事务管理两部分属性整合起来作为整个事务管理。 -->
		<aop:advisor pointcut-ref="bussinessService"
			advice-ref="txAdvice" />
	
	</aop:config>
	
	
	
	<!-- 事务的传播特性 -->
	<tx:advice id="txAdvice" transaction-manager="txManager">
		
		<!--tx:attribute标签所配置的是作为事务的方法的命名类型 -->
		<tx:attributes>
			<tx:method name="XXX" read-only="true" />
		
			<!--propagation="REQUIRED"代表支持当前事务,如果当前没有事务,就新建一个事务 -->
			<tx:method name="XXX" propagation="REQUIRED"/>
		</tx:attributes>
	</tx:advice>
	
</beans>

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值