zoukankan      html  css  js  c++  java
  • spring配合jdbc处理事务

    spring中实现aop的配置方式很多,在这里配置事务的时候推荐使用:
      1.tx前缀的事务标签和aop前缀的标签结合,将切面(事务管理器)织入到切入点上
      2.注解进行事务配置

    例如1:
    spring结合jdbc,事务配置在service层指定方法上,使用tx标签结合aop标签

    //使用jdbc实现dao层接口
    public class JDBCAccountDaoImpl implements AccountDao{
      private JdbcTemplate jdbcTemplate;
      //get/set方法

      //实现接口中的抽象方法
      .....
    }

    spring_jdbc.xml配置文件主要内容:
    //配置连接池对象


    jdbc_service1.xml配置文件主要内容:

    //配置

    <bean name="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
      <constructor-arg index="0" ref="dataSource"></constructor-arg>
    </bean>

    <bean id="accountDao" class="com.briup.tran.jdbc.JDBCAccountDaoImpl">
      <property name="jdbcTemplate" ref="jdbcTemplate"/>
    </bean>

    <!-- 配置service层对象 目标对象-->
    <bean name="service" class="com.briup.tran.service.AccountServiceImpl">
      <!-- 注入dao层对象 -->
      <property name="accountDao" ref="accountDao"></property>
    </bean>

    <!-- 配置jdbc的事务管理器 (实际上,事务管理器就是一个切面) -->
    <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
      <!-- 注入数据源,spring的jdbc事务管理器在管理事务时,依赖于JDBC的事务管理机制 -->
      <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 配置事务拦截器 ,transaction-manager:表示通知织入哪个切面-->
    <tx:advice id="transactionInterceptor" transaction-manager="transactionManager">
      <tx:attributes>
      <!-- *代表所有的方法 -->
      <tx:method name="*" propagation="REQUIRED" rollback-for="Throwable"/>
      </tx:attributes>
    </tx:advice>

    <!-- 配置aop -->
    <aop:config>
      <!-- 配置切入点, 之后可以在事务中在对方法进行细化 -->
      <aop:pointcut expression="execution(public * com.briup.tran.service.*.*(..))" id="myPointCut"/>
      <!-- 配置事务拦截器在哪一个切入点上起作用 -->
      <aop:advisor advice-ref="transactionInterceptor" pointcut-ref="myPointCut"/>
    </aop:config>


    //测试方法
    String path[] = {"com/briup/tran/jdbc/spring_jdbc.xml",
    "com/briup/tran/service/jdbc_service1.xml"};
    ApplicationContext container =
    new ClassPathXmlApplicationContext(path);

    AccountService service = (AccountService)container.getBean("service");

    service.add(new Account(1,"zs1",1000));


    例如2:
    spring结合jdbc,事务配置在service层指定方法上,使用注解进行事务配置
    和例子1中只有俩处不同
    第一处:
    service接口的实现类的上面使用@Transactional:
    @Transactional(propagation=Propagation.REQUIRED,rollbackFor=Throwable.class)
    public class AccountServiceImpl implements AccountService{
    .....
    }
    第二处:
    jdbc_service2.xml配置文件主要内容:
    <!-- 配置service层对象 目标对象-->
    <bean name="service" class="com.briup.tran.service.AccountServiceImpl">
    <!-- 注入dao层对象 -->
    <property name="accountDao" ref="accountDao"></property>
    </bean>

    <!-- 配置jdbc的事务管理器 (切面类)-->
    <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <!-- 注入数据源 -->
    <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 通知spring我们在目标对象中做了事务的注解,并指明使用哪一个事务管理器 -->
    <!-- 加入上这个标签后 去目标对象去加入相应的注解就可以了 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>


    例如3:
    spring结合mybatis,事务配置在service层指定方法上,使用tx标签结合aop标签

    AccountMapper.xml配置文件主要内容:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <!-- com.briup.tran.dao.AccountDao是我们定义接口的全限定名字 这样就可以使用接口调用映射的SQL语句了 这个名字一定要和接口对应上-->
    <mapper namespace="com.briup.tran.dao.AccountDao">

    <insert id="save" parameterType="Account">
    insert into
    t_account(id,name,balance)
    values(#{id},#{name},#{balance})
    </insert>

    <update id="update" parameterType="Account">
    update t_account
    set
    name=#{name},
    balance=#{balance}
    where id=#{id}
    </update>

    <delete id="delete" parameterType="Account">
    delete from t_account where id=#{id}
    </delete>

    </mapper>


    spring_mybatis.xml配置文件主要内容:
    <!-- 配置数据源 -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      <property name="driverClassName">
        <value>oracle.jdbc.driver.OracleDriver</value>
      </property>
    <property name="url">
    <value>jdbc:oracle:thin:@localhost:1521:XE</value>
    </property>
    <property name="username">
    <value>briup</value>
    </property>
    <property name="password">
    <value>briup</value>
    </property>
    </bean>

    <!-- 配置sqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="typeAliasesPackage" value="com.briup.tran"></property>
    <property name="configurationProperties">
    <props>
    <prop key="cacheEnabled">true</prop>
    </props>
    </property>
    <!-- 自动扫描mapping.xml文件 -->
    <property name="mapperLocations" value="classpath:com/briup/tran/mybatis/AccountMapper.xml" />
    </bean>

    <!-- 自动扫描映射接口所在的包 -->
    <!-- 将来可以通过接口的名字首字母小写作为beanName,从spring容器中拿出自动生成的该接口的实现类 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.briup.tran.dao" />
    </bean>


    mybatis_service1.xml配置文件主要内容:
    <!-- 配置service层对象 目标对象-->
    <bean name="service" class="com.briup.tran.service.AccountServiceImpl">
    <!-- 注入dao层对象 -->
    <property name="accountDao" ref="accountDao"></property>
    </bean>

    <!-- 配置jdbc的事务管理器 (切面类) 适用于mybatis-->
    <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <!-- 注入数据源 -->
    <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 配置事务拦截器 -->
    <tx:advice id="transactionInterceptor" transaction-manager="transactionManager">
    <tx:attributes>
    <!-- *代表所有的方法
    表示与事务属性关联的方法业务(方法名),对切入点进行细化。通配符(*)可以用来指定一批关联到相同的事务属性的方法。
    如:'get*'、'find*'、'on*Event'等等-->
    <tx:method name="*" propagation="REQUIRED" rollback-for="Throwable"/>
    </tx:attributes>
    </tx:advice>

    <!-- 配置aop -->
    <aop:config>
    <!-- 配置切入点 -->
    <aop:pointcut expression="execution(public * com.briup.tran.service.*.*(..))" id="myPointCut"/>
    <!-- 配置事务拦截器在哪一个切入点上起作用 -->
    <aop:advisor advice-ref="transactionInterceptor" pointcut-ref="myPointCut"/>
    </aop:config>


    测试方法:
    .....


    例如4:
    spring结合mybatis,事务配置在service层指定方法上,使用注解进行事务配置

    和例子3中只有俩处不同
    第一处:
    service接口的实现类的上面使用@Transactional:
    @Transactional(propagation=Propagation.REQUIRED,rollbackFor=Throwable.class)
    public class AccountServiceImpl implements AccountService{
    .....
    }

    第二处:
    mybatis_service2.xml配置文件主要内容:
    <!-- 配置service层对象 目标对象-->
    <bean name="service" class="com.briup.tran.service.AccountServiceImpl">
    <!-- 注入dao层对象 -->
    <property name="accountDao" ref="accountDao"></property>
    </bean>

    <!-- 配置jdbc的事务管理器 (切面类) 适用于mybatis-->
    <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <!-- 注入数据源 -->
    <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 通知spring我们在目标对象中做了事务的注解,并指明使用哪一个事务管理器 -->
    <!-- 加入上这个标签后 去目标对象去加入相应的注解就可以了 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>

  • 相关阅读:
    Finance_Time-Series-Analysis-with-app-in-R
    Linear_algebra_06_二次型
    Linear_algebra_05_相似对角形
    病理学
    S&p_14_参数的假设检验
    S&p_13_参数区间估计
    Finance_Analysis-of-Financial-Time-Series
    817. Linked List Components
    811. Subdomain Visit Count
    807. Max Increase to Keep City Skyline
  • 原文地址:https://www.cnblogs.com/nyhhd/p/12591909.html
Copyright © 2011-2022 走看看