spring+hibernate 配置文件(applicationContext.xml)实例

 先贴段代码再慢慢解释:

appcationContext.xml

< beans >
    
< bean  id ="dataSource"
        class
="org.apache.commons.dbcp.BasicDataSource" >
        
< property  name ="driverClassName" >
            
< value > com.mysql.jdbc.Driver </ value >
        
</ property >
        
< property  name ="url" >
            
< value > jdbc:mysql://localhost/test?useUnicode=true &amp; characterEncoding=UTF-8 </ value >
        
</ property >
        
< property  name ="username" >
            
< value > root </ value >
        
</ property >
    
</ bean >
    
< bean  id ="exampleHibernateProperties"  class ="org.springframework.beans.factory.config.PropertiesFactoryBean" >
        
< property  name ="properties" >
            
< props >
                
< prop  key ="hibernate.dialect" > org.hibernate.dialect.MySQLDialect </ prop >
            
</ props >
        
</ property >
    
</ bean >
    
< bean  id ="sessionFactory"
        class
="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
        
< property  name ="dataSource" >
            
< ref  bean ="dataSource"   />
        
</ property >
              <property name="mappingResources">
               <list>
                  <value>Test.hbm.xml</value>
              </list>
          </property>

        
< property  name ="hibernateProperties" >
            
< ref  bean ="exampleHibernateProperties"   />
        
</ property >
    
</ bean >

    
< bean  id ="transactionManager"
        class
="org.springframework.orm.hibernate3.HibernateTransactionManager"
        abstract
="false"  singleton ="true"  lazy-init ="default"
        autowire
="default"  dependency-check ="default" >
        
< property  name ="sessionFactory" >
            
< ref  bean ="sessionFactory"   />
        
</ property >
    
</ bean >
    
< bean  id ="baseDao"  abstract ="true"  singleton ="false" >
        
< property  name ="sessionFactory"  ref ="sessionFactory" />
    
</ bean >
    
< bean  id ="testDao"  parent ="baseDao"  class ="ssh.naxj.dao.TestDao"  singleton ="true" />
    
< bean  id ="testBoTarget"  class ="ssh.naxj.bo.TestBo"  singleton ="true" >
        
< property  name ="testDao"  ref ="testDao" />
    
</ bean >
    
    
< bean  id ="testBo"
      class
="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" >
        
< property  name ="transactionManager" >< ref  local ="transactionManager" /></ property >
        
< property  name ="target" >< ref  local ="testBoTarget" /></ property >
        
< property  name ="transactionAttributes" >
            
< props >
                
< prop  key ="*" > PROPAGATION_REQUIRED </ prop >
            
</ props >
        
</ property >
    
</ bean >
    
</ beans >

第一部分 数据源

 <bean id="dataSource"
  class="org.apache.commons.dbcp.BasicDataSource">
  <property name="driverClassName">
   <value>com.mysql.jdbc.Driver</value>//驱动名称
  </property>
  <property name="url">
   <value>jdbc:mysql://localhost/test?useUnicode=true&amp;characterEncoding=UTF-8</value>//数据库地址
  </property>
  <property name="username">
   <value>root</value>//用户名
  </property>
  <property name="password">
   <value>root</value>//密码
  </property>
 </bean>

第二部分 hibernate的配置

 <bean id="exampleHibernateProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
  <property name="properties">
   <props>
    <prop key="hibernate.hbm2ddl.auto">update</prop>
    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>//对应所使用的数据库,我所用的是MYSQL
    <prop key="hibernate.query.substitutions">true '0', false '1'</prop>
    <prop key="hibernate.show_sql">true</prop>
    <prop key="hibernate.c3p0.minPoolSize">5</prop>
    <prop key="hibernate.c3p0.maxPoolSize">20</prop>
    <prop key="hibernate.c3p0.timeout">600</prop>
    <prop key="hibernate.c3p0.max_statement">50</prop>
    <prop key="hibernate.c3p0.testConnectionOnCheckout">false</prop>
   </props>
  </property>
 </bean>

第三部分 hibernate的sessionFactory 一个关联于特定数据库的全局工厂

 <bean id="sessionFactory"
  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="dataSource">
   <ref bean="dataSource" />//先前定义的数据源
  </property>
  <property name="mappingResources">
   <list>
          <value>Test.hbm.xml</value>//hibernate O/R map文件
      </list>
  </property>
  <property name="hibernateProperties">
   <ref bean="exampleHibernateProperties" />//先前定义的hibernate 配置
  </property>
 </bean>

第四部分 事务 transactionManager

 <bean id="transactionManager"
  class="org.springframework.orm.hibernate3.HibernateTransactionManager"
  abstract="false" singleton="true" lazy-init="default"
  autowire="default" dependency-check="default">
  <property name="sessionFactory">
   <ref bean="sessionFactory" />//先前定义的sessinFactory
  </property>
 </bean>

第五部分 Dao 抽象

 <bean id="baseDao" abstract="true" singleton="false">
  <property name="sessionFactory" ref="sessionFactory"/>
 </bean>
定义baseDao的好处就是在以下的Dao中再不用每个bean 都要注入sessionFactory了

第六部分 Dao

<bean id="testDao" parent="baseDao"  //继承于先前定义的Dao 抽象
    class="ssh.naxj.dao.TestDao" singleton="true"/>

第七部分 定义 Target (BO)

 <bean id="testBoTarget" class="ssh.naxj.bo.TestBo" singleton="true">
  <property name="testDao" ref="testDao"/>//IOC 注入testDao
 </bean>

第八部分 使用事务的BO

    <bean id="testBo"
      class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
  <property name="transactionManager"><ref local="transactionManager"/></property>//transactionManager
  <property name="target"><ref local="testBoTarget"/></property>//Target
  <property name="transactionAttributes">
   <props>
    <prop key="*">PROPAGATION_REQUIRED</prop>//“*”为所有的方法使用事务
   </props>
  </property>
 </bean>

OK,大功告成...

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值