Spring jdbc 以及事务管理

一,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事务 前言 这篇其实也要归纳到《常识》系列中,但这重点又...
    码农戏码阅读 4,775评论 2 59
  • 5.Spring的事务 通常情况下,J2EE有2种事务管理方式:全局事务和本地事务,2种事务都比较明显的缺陷。 全...
    FTOLsXD阅读 1,532评论 0 8
  • 一.Spring对编程式事务的支持 Spring中的事务分为物理事务和逻辑事务;物理事务:就是底层数据库提供的事务...
    zlb阅读 7,664评论 0 5
  • 对大多数Java开发者来说,Spring事务管理是Spring应用中最常用的功能,使用也比较简单。本文主要从三个方...
    sherlockyb阅读 3,237评论 0 18
  • 1. new window will be blocked if it's opened by js in aja...
    MengchunCao阅读 316评论 0 0