spring 配置hibernate

来源:互联网 发布:中国联合网络通信电话 编辑:程序博客网 时间:2024/05/24 01:30
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
原创粉丝点击