spring 与 hibernate 整合(事务)

参考:第 9 章 事务管理 - Spring Framework reference 2.0.5 参考手册中文版
http://doc.javanb.com/spring-framework-reference-zh-2-0-5/ch09.html

先从配置文件开始:
源码: springAop.rar

需要jar
<? xml version="1.0" encoding="UTF-8" ?>
< classpath >
    
< classpathentry  kind ="src"  path ="java" />
    
< classpathentry  kind ="con"  path ="org.eclipse.jdt.launching.JRE_CONTAINER" />
    
< classpathentry  kind ="lib"  path ="lib/aspectjrt.jar" />
    
< classpathentry  kind ="lib"  path ="lib/aspectjweaver.jar" />
    
< classpathentry  kind ="lib"  path ="lib/spring.jar" />
    
< classpathentry  kind ="lib"  path ="lib/spring-sources.jar" />
    
< classpathentry  kind ="lib"  path ="lib/commons-logging-1.0.4.jar" />
    
< classpathentry  kind ="lib"  path ="lib/cglib-nodep-2.1_3.jar" />
    
< classpathentry  kind ="lib"  path ="lib/hibernate3.jar" />
    
< classpathentry  kind ="lib"  path ="lib/log4j-1.2.11.jar" />
    
< classpathentry  kind ="con"  path ="org.eclipse.jdt.junit.JUNIT_CONTAINER/4" />
    
< classpathentry  kind ="lib"  path ="lib/dom4j-1.6.1.jar" />
    
< classpathentry  kind ="lib"  path ="lib/commons-collections-2.1.1.jar" />
    
< classpathentry  kind ="lib"  path ="lib/mysql.jar" />
    
< classpathentry  kind ="lib"  path ="lib/jta.jar" />
    
< classpathentry  kind ="lib"  path ="lib/antlr-2.7.6.jar" />
    
< classpathentry  kind ="output"  path ="bin" />
</ classpath >


spring 配置
<? 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"
    xsi:schemaLocation
="http://www.springframework.org/schema/beans 
  http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
  http://www.springframework.org/schema/aop 
  http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
  http://www.springframework.org/schema/tx 
  http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"
>

    
<!--  demo start  -->
    
< import  resource ="demo_spring.xml"   />
    
<!--  demo end  -->

    
< bean  id ="dataSource"
        class
="org.springframework.jdbc.datasource.DriverManagerDataSource" >
        
< property  name ="driverClassName"
            value
="com.mysql.jdbc.Driver" >
        
</ property >
        
< property  name ="url"
            value
="jdbc:mysql://localhost:3306/aop?characterEncoding=utf8" >
        
</ property >
        
< property  name ="username"  value ="root" ></ property >
        
< property  name ="password"  value ="" ></ property >
    
</ bean >

    
<!--  hibernate3 sessionFactory   -->
    
< bean  id ="sessionFactory"
        class
="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
        
<!--  此次  为 spring 事务需要使用 dataSource ;为空事务由Hibernian自己维护  -->
        
< property  name ="dataSource"  ref ="dataSource"   />
        
< property  name ="configLocation"
            value
="classpath:hibernate.cfg.xml"   />
    
</ bean >



    
<!--  事务适配器  -->
    
< bean  id ="txManager"
        class
="org.springframework.orm.hibernate3.HibernateTransactionManager" >
        
< property  name ="sessionFactory"  ref ="sessionFactory"   />
    
</ bean >






    
<!--  aop 与事务联系 aopBean<->txAdvice   -->
    
< aop:config >
        
<!--  逻辑拦截  -->
        
< aop:pointcut  id ="demoAopBean"
            expression
="execution(* demo*.*.*(..))"   />
        
< aop:advisor  advice-ref ="demoTxAdvice"
            pointcut-ref
="demoAopBean"   />
    
</ aop:config >

    
<!--  事务原子 具体方法进行什么事务  -->
    
< tx:advice  id ="demoTxAdvice"  transaction-manager ="txManager" >
        
< tx:attributes >
            
< tx:method  name ="get*"  propagation ="SUPPORTS"
                read-only
="true"  rollback-for ="NoProductInStockException"   />
            
< tx:method  name ="save*"  propagation ="REQUIRED"
                rollback-for
="NoProductInStockException"   />
            
< tx:method  name ="update*"  propagation ="REQUIRED"
                rollback-for
="NoProductInStockException"   />
            
< tx:method  name ="remove*"  propagation ="REQUIRED"
                rollback-for
="NoProductInStockException"   />
            
< tx:method  name ="*"  propagation ="SUPPORTS"
                rollback-for
="NoProductInStockException"   />
        
</ tx:attributes >
    
</ tx:advice >



    
<!--  daoCalss : extends HibernateDaoSupport implements BeanDao  -->
    
< bean  id ="beanDao"
        class
="demo.springHibernate.dao.imp.BeanDaoImp" >
        
< property  name ="sessionFactory" >
            
< ref  bean ="sessionFactory" ></ ref >
        
</ property >
    
</ bean >


    
< bean  id ="helloAction"  class ="demo.struts2Spring.HelloWorld"
        scope
="prototype" >
        
< property  name ="bds"  ref ="beanDao" ></ property >
    
</ bean >
    
</ beans >


hibernate 配置
<? xml version="1.0" encoding="UTF-8" ?>
<! DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"
>
< hibernate-configuration >
< session-factory  name ="asdf" >
    
< property  name ="hibernate.dialect" > mysql </ property >
    
< property  name ="myeclipse.connection.profile" >
        com.mysql.jdbc.Driver
    
</ property >
    
< property  name ="connection.url" >
        jdbc:mysql://localhost/aop
    
</ property >
    
< property  name ="show_sql" > true </ property >
    
    
< property  name ="connection.username" > root </ property >
    
< property  name ="connection.password" ></ property >
    
< property  name ="connection.driver_class" >
        com.mysql.jdbc.Driver
    
</ property >
    
< property  name ="dialect" >
        org.hibernate.dialect.MySQLDialect
    
</ property >
    
    
< mapping  resource ="bean/UnitBean.hbm.xml"   />
    
</ session-factory >
</ hibernate-configuration >


dao 类(接口)
package  dao.imp;

import  java.util.List;

import  org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import  bean.UnitBean;

import  dao.BeanDao;

public   class  BeanDaoImp  extends  HibernateDaoSupport  implements  BeanDao{
    
public   void  addBean(UnitBean unitBean) {
        
this .getHibernateTemplate().save(unitBean);
    }

    
public  List < UnitBean >  getBeanByAll() {
        
return   this .getHibernateTemplate().find( "  from  " + UnitBean. class .getName());
    }

    
public   void  removeBean( long  beanId) {
        
this .getHibernateTemplate().delete(
                getHibernateTemplate().get(UnitBean.
class , beanId)
            );
    }
    
}

Main 类
package  unit;

import  org.springframework.context.ApplicationContext;
import  org.springframework.context.support.ClassPathXmlApplicationContext;

import  dao.BeanDao;
import  bean.UnitBean;

public   class  Main {
    
public   static   void  main(String[] args) {
           ApplicationContext ctx 
=   new  ClassPathXmlApplicationContext( " beans.xml " );
           BeanDao dao 
=  (BeanDao) ctx.getBean( " beanDao " );
           UnitBean bean 
=   new  UnitBean();
           bean.setName(
" xx " );
           bean.setPass(
" 11 " );
           dao.addBean(bean);
           
           
for (UnitBean unitBean : dao.getBeanByAll() ){
               System.out.println( unitBean.getId() );
           }
           
           dao.removeBean(bean.getId());
           
    }
}
结果:
Hibernate: insert into bean (name, pass) values (?, ?)
Hibernate: select unitbean0_.id as id0_, unitbean0_.name as name0_, unitbean0_.pass as pass0_ from bean unitbean0_
1
Hibernate: select unitbean0_.id as id0_0_, unitbean0_.name as name0_0_, unitbean0_.pass as pass0_0_ from bean unitbean0_ where unitbean0_.id=?
Hibernate: delete from bean where id=?






你可能感兴趣的:(spring 与 hibernate 整合(事务))