您的位置:首页 > 其它

hibernate的beans.xml配置

2015-10-14 10:13 363 查看
hibernate使用c3p0连接池配置:

头:<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: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-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
1.创建数据源(c3p0)

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"destroy-method="close">

<property name="driverClass" value="数据库驱动包(oracle.jdbc.driver.OracleDriver)“ />

<property name="jdbcUrl" value="jdbc的URL(jdbc:oracle:thin:@127.0.0.1:1521:orcl)" />

<property name="user" value="用户名(klm)" />

<property name="password" value="用户密码(klm123)" />

</bean>

2.开启注解:

<context:annotation-config/>

3.设置扫描包

<context:component-scan base-package="扫描的包名(com.card.memberSystem)"/>

4.使用注解配置AspectJ,先注册aspectj

<aop:aspectj-autoproxy/>

5.使用注解配置事物 ,tx的transaction-manager属性默认值就是"transactionManager"

<tx:annotation-driven transaction-manager="txManager"/>

6.配置sessionfactory

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"

p:dataSource-ref="dataSource" p:configLocations="classpath:hibernate配置的文件名(hibernate.cfg.xml)"/>

7.实例化事务管理器

<bean id="txManager"

class="org.springframework.orm.hibernate4.HibernateTransactionManager">

<property name="sessionFactory" ref="sessionFactory"></property>

</bean>

8.配置事务的传播特性 ,指定事务管理器

<tx:advice id="txAdvice" transaction-manager="txManager">

9.配置详细的事务语义

<tx:advice id="txAdvice" transaction-manager="txManager">

<tx:attributes>

<tx:method name="find*" read-only="true" propagation="REQUIRED" />

<tx:method name="*" read-only="false" propagation="REQUIRED" />

</tx:attributes>

</tx:advice>

10.配置切面

<aop:config>

<aop:pointcut id="pc" expression="execution(* 要配置的包名.*.*(..))"/>

<aop:advisor advice-ref="txAdvice" pointcut-ref="pc"/>

</aop:config>

eg:

<aop:config>

<aop:pointcut id="pc" expression="execution(* com.card.memberSystem.business.systemSet.service.*.*(..))"/>

<aop:advisor advice-ref="txAdvice" pointcut-ref="pc"/>

</aop:config>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: