美文网首页程序员
Spring jdbc 以及事务管理

Spring jdbc 以及事务管理

作者: 遗忘的大志若愚 | 来源:发表于2017-11-20 17:04 被阅读0次

    一,Springjdbc的一些常用类

    spring与jdbc.png

    这里有两个常用的类jdbcDaoSupport和jdbcTemplate

    1,jdbcDaoSupport提供了jdbcTemplate和DataSource的setter方法,可以通过它们获取到jdbcTemplate和DataSource
    2,在jdbcTemplate里面封装了基本的对数据库的一些操作
    3,如果想要操作数据库,直接继承jdbcDaoSupport和jdbcTemplate这两个类或者直接 使用di方式直接注入这两个类

    二,Spring的事务控制

    1,事务架构

    spring的事务的结构.png
    PlatformTransactionManager是事务处理的核心接口,规定了事务开启,提交和回滚,AbstractPlatformTransactionManager是实现PlatformTransactionManager的抽象类,已经实现了事务的提交(commit)和回滚(rollback),对于采取不同的数据技术,事务开启的方式是不一样的。

    2,事务的状态以及定义

    事务定义


    image.png

    事务状态


    image.png
    在事务控制中又涉及到TransactionStatus和TransactionDefinition两个类。一个描述事务的状态信息(是否为新的事务,事务是否完成),另外一个描述事务的传播属性(解决事务的嵌套问题)和事务的隔离机制以及是否只读

    3,实例操作

    dao层以及实现

    //dao
    package com.fiberhome.spring.jdbc.dao;
    
    public interface PersonDao {
      public void savePerson(String sql);
    
    }
    //daoImpl
    package com.fiberhome.spring.jdbc.dao;
    
    import org.springframework.jdbc.core.support.JdbcDaoSupport;
    
    public class PersonDaoImpl extends JdbcDaoSupport implements PersonDao  {
       
      @Override
      public void savePerson(String sql) {
       this.getJdbcTemplate().execute(sql);
      }
    
    }
    

    service层以及实现

    //service
    package com.fiberhome.spring.jdbc.service;
    
    public interface PersonService {
    public void savePerson();
    }
    //serviceImpl
    package com.fiberhome.spring.jdbc.service;
    
    import com.fiberhome.spring.jdbc.dao.PersonDao;
    
    public class PersonServiceImpl implements PersonService{
    
      private PersonDao dao;
      public void setDao(PersonDao dao) {
        this.dao = dao;
      }
      public void savePerson() {
        
        dao.savePerson("INSERT INTO ist_library_user (NAME ) VALUES('xixi')");
        int a=1/0;
         dao.savePerson("INSERT INTO ist_library_user (NAME ) VALUES('xixi')");
      }
    
    }
    

    xml配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
               http://www.springframework.org/schema/aop 
               http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
               http://www.springframework.org/schema/tx 
               http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    <!-- 获取dataSource-->
        <bean
            class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="locations">
                <value> classpath:jdbc.properties</value>
            </property>
        </bean>
    
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
            <property name="driverClassName" value="${jdbc.driverClassName}" />
            <property name="url" value="${jdbc.url}" />
            <property name="username" value="${jdbc.username}" />
            <property name="password" value="${jdbc.password}" />
    
        </bean>
    
    
        <bean id="personDao" class="com.fiberhome.spring.jdbc.dao.PersonDaoImpl">
            <property name="dataSource">
                <ref bean="dataSource" />
            </property>
    
        </bean>
    
        <bean id="personService" class="com.fiberhome.spring.jdbc.service.PersonServiceImpl">
            <property name="dao">
                <ref bean="personDao" />
            </property>
        </bean>
        
        
        <!-- 事务管理器 告诉spring容器要采用什么样的技术处理事务 -->
        <bean id="transactionManager"
            class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource">
                <ref bean="dataSource" />
            </property>
        </bean>
        
        
        <tx:advice id="tx" transaction-manager="transactionManager">
    
            <tx:attributes>
    <!--告知事务处理的策略-->
                <tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT"
                    read-only="false" />
    
            </tx:attributes>
        </tx:advice>
        <aop:config>
            <aop:pointcut
                expression="execution(* com.fiberhome.spring.jdbc.service.PersonServiceImpl.*(..))"
                id="perform" />
            <aop:advisor advice-ref="tx" pointcut-ref="perform" />
    
        </aop:config>
    
    </beans>
    

    相关文章

      网友评论

        本文标题:Spring jdbc 以及事务管理

        本文链接:https://www.haomeiwen.com/subject/bfwvvxtx.html