Spring配置SessionFactory

Spring的核心模块之一为依赖注入,普遍使用的为Set方式注入,那么下面将借助该方式配置数据源和SessionFactory

直接引用hibernate.cfg.xml配置文件

这种方式需要提供hibernate.cfg.xml配置文件,在其中构造sessionFactory,并且持久层类的映射也在改文件配置,这步操作和Spring没有任何关系。产生关系是在spring的配置文件中,通过set方式注入到org.springframework.orm.hibernate3.LocalSessionFactoryBean类中,其对应属性为:configLocation。配置如下:

<!-- 寻找hibernate.cfg.xml方式配置sessionfactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation"> <value>classpath:hibernate.cfg.xml</value> </property> </bean>


使用数据源配置SessionFactory

1、使用jdbc数据源

<!-- 配置jdbc数据源 --> <!-- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="username" value="root"/> <property name="password" value="root"/> <property name="url" value="jdbc:mysql://localhost:3306/iess?useUnicode=true&characterEncoding=UTF8"/> </bean> -->


2、使用c3p0数据源

<!-- 配置c3p0数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"/> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/iess?useUnicode=true&characterEncoding=UTF8"/> <property name="maxIdleTime" value="25000"/> <property name="properties"> <props> <prop key="user">root</prop> <prop key="password">root</prop> <prop key="c3p0.min_size">10</prop> <prop key="c3p0.max_size">100</prop> <prop key="c3p0.timeout">1800</prop> <prop key="c3p0.idle_test_period">3600</prop> </props> </property> </bean>

3、结合数据源配置SessionFactory

<!-- 根据数据源创建sessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="mappingResources"> <list> <value>com/indigopacific/iessportal/persistent/Seal.hbm.xml</value> <value>com/indigopacific/iessportal/persistent/SealModel.hbm.xml</value> <value>com/indigopacific/iessportal/persistent/BranchInfo.hbm.xml</value> <value>com/indigopacific/iessportal/persistent/SealData.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.jdbc.fetch_size">30</prop> <prop key="hibernate.jdbc.batch_size">0</prop> <prop key="hibernate.max_fetch_depth">10</prop> </props> </property> </bean>

spring配置事务

spring配置事务借助其AOP核心模块,其包括如下步骤:

1、配置事务管理器

2、配置Pointcut,定义事务作用范围

3、配置Advice,定义事务传播范围

其配置如下:

<!-- 配置事務管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref bean="sessionFactory"/> </property> </bean> <!-- 配置哪些类开启事务 --> <aop:config> <aop:pointcut id="allMethod" expression="execution(* com.indigopacific.iessportal.Manager.*.*(..))"/> <aop:advisor pointcut-ref="allMethod" advice-ref="txAdvice"/> </aop:config> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="del*" propagation="REQUIRED"/> <tx:method name="modify*" propagation="REQUIRED"/> <tx:method name="*" propagation="REQUIRED" read-only="true"/> </tx:attributes> </tx:advice>


整个配置文件

<?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: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.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <!-- 寻找hibernate.cfg.xml方式配置sessionfactory --> <!-- <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation"> <value>classpath:hibernate.cfg.xml</value> </property> </bean> --> <!-- 配置数据源方式获取sessionfactory --> <!-- 配置jdbc数据源 --> <!-- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="username" value="root"/> <property name="password" value="root"/> <property name="url" value="jdbc:mysql://localhost:3306/iess?useUnicode=true&characterEncoding=UTF8"/> </bean> --> <!-- 配置c3p0数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"/> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/iess?useUnicode=true&characterEncoding=UTF8"/> <property name="maxIdleTime" value="25000"/> <property name="properties"> <props> <prop key="user">root</prop> <prop key="password">root</prop> <prop key="c3p0.min_size">10</prop> <prop key="c3p0.max_size">100</prop> <prop key="c3p0.timeout">1800</prop> <prop key="c3p0.idle_test_period">3600</prop> </props> </property> </bean> <!-- 根据数据源创建sessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="mappingResources"> <list> <value>com/indigopacific/iessportal/persistent/Seal.hbm.xml</value> <value>com/indigopacific/iessportal/persistent/SealModel.hbm.xml</value> <value>com/indigopacific/iessportal/persistent/BranchInfo.hbm.xml</value> <value>com/indigopacific/iessportal/persistent/SealData.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.jdbc.fetch_size">30</prop> <prop key="hibernate.jdbc.batch_size">0</prop> <prop key="hibernate.max_fetch_depth">10</prop> </props> </property> </bean> <!-- 配置事務管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref bean="sessionFactory"/> </property> </bean> <!-- 配置哪些类开启事务 --> <aop:config> <aop:pointcut id="allMethod" expression="execution(* com.indigopacific.iessportal.Manager.*.*(..))"/> <aop:advisor pointcut-ref="allMethod" advice-ref="txAdvice"/> </aop:config> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="del*" propagation="REQUIRED"/> <tx:method name="modify*" propagation="REQUIRED"/> <tx:method name="*" propagation="REQUIRED" read-only="true"/> </tx:attributes> </tx:advice> </beans>


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值