D43-Spring04(事务管理)

一 、jdbcTemplate

  • jdbcTemplate:Spring提供的
  • spring提供一个用来操作数据的子框架(或工具类)对jdbc进行封装。
  • jdbcTemplate交给Spring管理的时候需要依赖:DataSource
  • 使用增删改查操作
    • 测试类–Dao层–JdbcTemplate----连接池(c3p0,druid,dbcp,spring内置连接池)—mysql数据库

1.1 Jdbctemplate在dao层操作数据库的细节整理

  1. jdbcTemplate 注入连接池的方式:·
    1.1 有参构造
    1.2 set属性
  2. Spring配置文件中引入外部的properties的两种方式
    2.1 第一种方式
<context:property-placeholder location="jdbc.properties">
</context:property-placeholder>
  • 2.2 第二种方式
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:jdbc.properties"></property>
</bean>
  1. spring集成各种连接池
    3.1 c3p0
<!--c3p0连接池  ComboPooledDataSource-->
<bean id="c3p0" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="${jdbc.driver}"></property>
    <property name="jdbcUrl" value="${jdbc.url}"></property>
    <property name="user" value="${jdbc.username}"></property>
    <property name="password" value="${jdbc.password}"></property>
</bean>
  • 3.2 Druid DataSource
<!--druid DruidDataSource -->
<bean id="druid" class="com.alibaba.druid.pool.DruidDataSource">
    <property name="driverClassName" value="${jdbc.driver}"></property>
    <property name="url" value="${jdbc.url}"></property>
    <property name="username" value="${jdbc.username}"></property>
    <property name="password" value="${jdbc.password}"></property>
</bean>
  • 3.3 < !–dbcp BasicDataSource–> <!–spring内置连接池 DriverMangerDataSource-- >

    • 除了class标签不一样外, 其他与 Druid 一模一样。
  1. 自定义jdbcTemplate的映射器类–自己根据返回的结果集自己封装对象
    • 默认的BeanRowMapper:字段名==对象属性名----封装5. jdbcTemplate在dao层注入方式:
    • 构造器方式—set方式
    • 继承方式

二、事务管理

Spring的声明式事务:用spring提供的切面类,通过AOP指定切入点的事务配置。

2.1 事务管理器(切面类)的介绍

  • PlatformTransactionManager(事务管理器): 接口
    • DatasourceTransactionManager:jdbcTemplate和mybatis用来操作事务
    • HibernateTransactionManager:hibernate框架用来操作事务
    • JPATransactionManage:所有ORM框架来使用
      • ORM:一系列的框架,指的不是一个框架
      • 特点:必须要做对象和表关系的映射。

2.2 TransactionDefinition(事务定义对象)

  • 作用:
  1. 设置事务的隔离级别
  2. 设置数据库的超时时间
  3. 设置是否只读
  4. 设置事务的传播行为

2.2.1 事务的隔离级别

  • 事务: 逻辑认为操作的一组事情,要么同时成功,要么同时失败。
  • 特性:ACID
    • 原子性
    • 一致性
    • 持久性
    • 隔离性
  • 在不考虑隔离性的前提下, 会发生读取数据的问题:
  1. 脏读
  2. 不可重复读
  3. 虚读/幻读
  • 解决:
  1. Read unCommitted :什么都解决不了
  2. Read committed(oracle) :解决脏读
  3. Repeatable Read(mysql) :解决脏读和不可重复读
  4. Serializable :解决所有但是不用

2.2.2 XML配置项:ioslation

  1. 一般全用默认值(default)
  2. **默认值:**当前数据库的隔离级别是什么就用什么。

2.2.3 数据库的超时时间

  • 默认值:-1
  • xml配置项:timeout
  • 一般全用默认值

2.2.4 是否只读事务

  • 只读事务:对查询有效
  • 非只读事务:对增删改有效
  • xml配置项: read-only true(只读事务) false(非只读事务)

2.2.5 事务的传播行为

  • 多方法嵌套调用的时候,被调用方法对事务的支持情况
  • xml配置型:propagation

三、声明式事务的配置方式

3.1 基于xml的配置

  1. 导入aop和事务的坐标
    • 在pom.xml文件中配置
  1. 把spring通过了事务通知的切面类交给ioc容器管理—依赖连接池(DateSource
<!--accountDao-->
<bean id="accountDao" class="com.itheima.daoimpl.AccountDaoImpl">
    <property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<!--accountService-->
<bean id="accountService" class="com.itheima.servcieimpl.AccountServiceImpl">
    <property name="accountDao" ref="accountDao"></property>
</bean>
<!--jdbcTemplate-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <!--set属性赋值-->
    <property name="dataSource" ref="druid"></property>
</bean>
<!--连接外部的jdbc.properties文件   PropertyPlaceholderConfigurer-->
<!--第一种方式-->
<context:property-placeholder location="jdbc.properties"></context:property-placeholder>
<!--第二种方式-->
<!-- <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
     <property name="location" value="classpath:jdbc.properties"></property>
 </bean>-->
    • 配置连接池
<!--druid DruidDataSource -->
<bean id="druid" class="com.alibaba.druid.pool.DruidDataSource">
    <property name="driverClassName" value="${jdbc.driver}"></property>
    <property name="url" value="${jdbc.url}"></property>
    <property name="username" value="${jdbc.username}"></property>
    <property name="password" value="${jdbc.password}"></property>
</bean>

<!--spring提供的事务通知切面类
    建议id名称:transactionManager
    需要一个连接池
-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="druid"></property>
</bean>
  1. 配置事务的特性
    a. 事务的隔离级别
    b. 超时
    c. 是否只读
    d. 事务的传播行为
<!--为通知设置事务特性
    transaction-manager="transactionManager"默认值为 transactionManager
    -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="tranFer" isolation="DEFAULT" read-only="false" timeout="-1" propagation="REQUIRED"/>
        <tx:method name="find*" isolation="DEFAULT" read-only="true" timeout="-1"
                   propagation="REQUIRED"></tx:method>
    </tx:attributes>
</tx:advice>
  1. 配置aop,将提供好的切面类事务通知在指定的切入点上使用
<!--配置aop-->
<aop:config>
    <!--指定切入点-->
    <aop:pointcut id="pt" expression="execution(* com.itheima..AccountDaoImpl.tranFer(..))"></aop:pointcut>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"></aop:advisor>
</aop:config>
    • 切入点
//切入点
public void tranFer() {
    //减钱
    accountDao.toMoeny();
    //加钱
    accountDao.inMoney();
}

3.2 半xml半注解

  • 半xml半注解:
    • 半xml: jdbcTemplate druid transactionManager
    • 半注解:代替accountDao 代替accoutService 代替事务的aop配置
    1. 开启ioc注解
      扫描创建自己资源的对象交给ioc容器
    1. 如果用的是spring的切面类开启的不是aop的注解支持 而是事务的注解支持
  • 事务注解找是@Transactional(事务特性)
    • 可以定义在类上 也可以定义在方法上
    • 类上: 针对类下所有方法
    • 方法: 只针对当前方法
    • 都定义了: 就近原则 以方法为主
  1. 开启对注解事务的支持
<!--开启ioc注解-->
<context:component-scan base-package="com.itheima"></context:component-scan>
<!--开启事务注解-->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
  • AccountServiceImpl
@Service(value = "accountService")
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountDao accountDao;
    //切入点
    public void tranFer() {
        //减钱
        accountDao.toMoeny();
        //加钱
        accountDao.inMoney();
    }
  • AccountDaoImpl
@Repository(value = "accountDao")
public class AccountDaoImpl implements AccountDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    public void toMoeny() {
      //sql语句
    }
    public void inMoney() {
        //sql语句
    }
}
  1. 将事务管理器(切面类)交给spring容器
<!--spring提供的事务通知切面类
    建议id名称:transactionManager
    需要一个连接池
-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="druid"></property>
</bean>
<!--为通知设置事务特性
    transaction-manager="transactionManager"默认值为 transactionManager
    -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="tranFer" isolation="DEFAULT" read-only="false" timeout="-1" propagation="REQUIRED"/>
        <tx:method name="find*" isolation="DEFAULT" read-only="true" timeout="-1"
                   propagation="REQUIRED"></tx:method>
    </tx:attributes>
</tx:advice>

<!--配置aop-->
<aop:config>
    <!--指定切入点-->
    <aop:pointcut id="pt" expression="execution(* com.itheima..AccountDaoImpl.tranFer(..))"></aop:pointcut>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"></aop:advisor>
</aop:config>
  1. 在需要事务的类或者方法上使用 @Transacational 注解配置事务
  • 事务注解找是 @Transactional(事务特性)
    • 可以定义在类上 也可以定义在方法上
    • 类上:针对类下所有方法
    • 方法:只针对当前方法
    • 都定义了:就近原则 以方法为主

3.3 纯注解

  1. 开启事务注解的支持
  2. 将事务管理交给spring容器管理
  3. 在需要事务的类或者方法上使用 @Transacational 注解配置事务

步骤:

  1. 测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes =SpringConfig.class)
public class TestJunit {
    @Autowired
   private AccountService accountService;
    @Test
    public void tranFer(){
        accountService.tranFer();
    }

}
  1. AccountServiceImpl
@Service(value = "accountService")
public class AccountServiceImpl implements AccountService {
    @Autowired
    private AccountDao accountDao;
    //切入点
    public void tranFer() {
        //减钱
        accountDao.toMoeny();
        //加钱
        accountDao.inMoney();
    }
}
  1. AccountDaoImpl
@Repository(value = "accountDao")
public class AccountDaoImpl implements AccountDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    public void toMoeny() {
        //sql语句
    }
    public void inMoney() {
        //sql语句
    }
}
  1. 配置类
@Configuration//配置类
//  <context:component-scan base-package="com.itheima"></context:component-scan>
@ComponentScan(basePackages = "com.itheima")
//<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
@EnableTransactionManagement
@PropertySource(value = "classpath:jdbc.properties")
public class SpringConfig {
    @Value(value = "${jdbc.driver}")
    private String driver;
    @Value(value = "${jdbc.url}")
    private String url;
    @Value(value = "${jdbc.username}")
    private String username;
    @Value(value = "${jdbc.password}")
    private String password;
    // 创建第三方的资源
    // 连接池
    @Bean(value = "druid")
    public DataSource createDateSource(){
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName(driver);
        druidDataSource.setUrl(url);
        druidDataSource.setUsername(username);
        druidDataSource.setPassword(password);
        return druidDataSource;
    }
    // jdbcTemplate
    @Bean(value = "jdbcTemplate")
    public JdbcTemplate createJdbcTemplate(@Qualifier(value ="druid") DataSource ds){
        return new JdbcTemplate(ds);
    }
    // spring提供的事务管理器--切面类
    @Bean(value = "transactionManager")
    public DataSourceTransactionManager createDataSourceTransactionManager(@Qualifier(value ="druid") DataSource ds){
        DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
        dataSourceTransactionManager.setDataSource(ds);
        return dataSourceTransactionManager;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值