前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >hibernate与spring的初步整合

hibernate与spring的初步整合

作者头像
yawn
发布2018-03-14 11:03:50
5530
发布2018-03-14 11:03:50
举报
<?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:tx="http://www.springframework.org/schema/tx"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
  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.xsd
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

 <!-- 
 
  配置文件:
  1.配置数据库连接:
   ① dataSourse
   ② sessionFactory
  2.事务的配置:
   ① transactionManager
   ② tx:advice
   ③ aop:pointcut
   
 
  -->

 <context:property-placeholder location="classpath:db.properties"/>
 
 <bean id="dataSourse" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  <property name="user" value="${jdbc.user}"></property>
  <property name="password" value="${jdbc.password}"></property>
  <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
  <property name="driverClass" value="${jdbc.driverClass}"></property>
 </bean>

 <bean id="sessionFactory"
  class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
  <property name="dataSource" ref="dataSourse"></property>
  
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
    <prop key="hibernate.show_sql">true</prop>
    <prop key="hibernate.format_sql">true</prop>
    <prop key="hibernate.hbm2ddl.auto">update</prop>
   </props>
  </property>
 
  <property name="mappingLocations" value="classpath:com/yawn/entity/Acount.hbm.xml"></property> 
  
  <!-- 
   以上部分配置来自于 hibernate 的配置文件 hibernate.cfg.xml 
   如果引入此文件就不需再配置,引入方法如下:
   <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
   -->
 </bean>
 
 
 
 <!-- 
 
  配置事务:
  1.事务管理器
  2.事务属性
  3.事务切点
 
  -->
 
 <bean id="transactionManager"
  class="org.springframework.orm.hibernate4.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactory" />
 </bean>
 
 <tx:advice id="txAdvice">
  <tx:attributes>
   <tx:method name="get*" read-only="true"/>
   <tx:method name="*" />
  </tx:attributes>
 </tx:advice>
 
 <aop:config>
  <aop:pointcut expression="execution(* com.yawn.service.Service1.transfer(..))" id="txPointcut"/>
  <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
 </aop:config>
 
 
 <!-- spring 中 bean 的基本配置和属性注入 -->
 <bean class="com.yawn.dao.impl.AcountDaoImpl" id="acountDaoImpl">
  <property name="sessionFactory" ref="sessionFactory"></property>
 </bean>
 <bean class="com.yawn.service.Service1" id="service">
  <property name="acountDaoImpl" ref="acountDaoImpl"></property>
 </bean>
 
 
 
 <!-- 
 <tx:annotation-driven transaction-manager="transactionManager" />
  -->
</beans>

以上为整合的配置文件,而整合步骤如下:

  1. 配置数据源(dataSourse,javax.sql.DataSourse)
  2. 配置sessionFactory
  3. 配置事务管理器
  4. 配置事务属性(tx:advice -> tx:attributs)
  5. 配置事务切点(包括关联事务属性和事务切点)
  6. 配置其他业务所需的bean等
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档