Spring5笔记2 - JDBC Template操作数据库、事务操作

JdbcTemplate(概念和准备) 26

1、什么是JdbcTemplate

(1)Spring框架对JDBC进行封装,使用JdbcTemplate方便实现对数据库操作

2、准备工作

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

JdbcTemplate 操作数据库(添加) 28

数据库中的表
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

JdbcTemplate 操作数据库(修改和删除) 29

在这里插入图片描述

JdbcTemplate 操作数据库(查询返回某个值)29

在这里插入图片描述
在这里插入图片描述

JdbcTemplate 操作数据库(查询返回对象) 30

在这里插入图片描述

JdbcTemplate 操作数据库(查询返回集合) 30

在这里插入图片描述

JdbcTemplate 操作数据库(批量操作) 30

1、批量操作:操作表里面多条记录
2、JdbcTemplate实现批量添加操作
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

事务操作(事务概念) 32

在这里插入图片描述
注意:
原子性(Atomicity):
事务是一个原子操作,由一系列动作组成。事务的原子性确保动作要么全部完成,要么完全不起作用。(不可分割,要么都成功,要么都失败)

一致性(Consistency):一旦事务完成(不管成功还是失败),系统必须确保它所建模的业务处于一致的状态,而不会是部分完成部分失败。在现实中的数据不应该被破坏。(操作前操作后总钱数不变,加起来还是200,也就是转账前转账后总量都是200)

隔离性(Isolation):可能有许多事务会同时处理相同的数据,因此每个事务都应该与其他事务隔离开来,防止数据损坏。(多事务操作时它们之间不会产生影响,两个人都操作同一条记录不会产生影响)

持久性(Durability):一旦事务完成,无论发生什么系统错误,它的结果都不应该受到影响,这样就能从任何系统崩溃中恢复过来。通常情况下,事务的结果被写到持久化存储器中。(事务要提交,提交了表中数据发生了变化)

事务操作(搭建事务操作环境) 32

javaEE三层:web、Service、Dao
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

补充代码:

xml配置文件:bean1.xm

<?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:context="http://www.springframework.org/schema/context"
       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.xsd
                        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
                        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
                        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!-- 组件扫描 -->
    <context:component-scan base-package="com.atguigu"></context:component-scan>

    <!-- 数据库连接池 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
          destroy-method="close">
        <property name="url" value="jdbc:mysql:///user_db" />
        <property name="username" value="root" />
        <property name="password" value="root" />
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    </bean>

    <!-- JdbcTemplate对象 -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <!--注入dataSource-->
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--创建事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--注入数据源-->
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--开启事务注解-->
    <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
</beans>

Service代码:

package com.atguigu.spring5.service;

import com.atguigu.spring5.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
@Transactional(readOnly = false,timeout = -1,propagation = Propagation.REQUIRED,isolation = Isolation.REPEATABLE_READ)
public class UserService {
    
    

    //注入dao
    @Autowired
    private UserDao userDao;

    //转账的方法
    public void accountMoney() {
    
    
//        try {
    
    
            //第一步 开启事务

            //第二步 进行业务操作
            //lucy少100
            userDao.reduceMoney();

            //模拟异常
            int i = 10/0;

            //mary多100
            userDao.addMoney();

            //第三步 没有发生异常,提交事务
//        }catch(Exception e) {
    
    
            //第四步 出现异常,事务回滚
//        }
    }
}

接口:

package com.atguigu.spring5.dao;

public interface UserDao {
    
    
    //多钱
    public void addMoney();
    //少钱
    public void reduceMoney();
}

接口实现类

package com.atguigu.spring5.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class UserDaoImpl implements UserDao {
    
    

    @Autowired
    private JdbcTemplate jdbcTemplate;

    //lucy转账100给mary
    //少钱
    @Override
    public void reduceMoney() {
    
    
        String sql = "update t_account set money=money-? where username=?";
        jdbcTemplate.update(sql,100,"lucy");
    }

    //多钱
    @Override
    public void addMoney() {
    
    
        String sql = "update t_account set money=money+? where username=?";
        jdbcTemplate.update(sql,100,"mary");
    }
}

测试:

 @Test
    public void testAccount() {
    
    

        ApplicationContext context =
                new ClassPathXmlApplicationContext("bean1.xml");
        UserService userService = context.getBean("userService", UserService.class);
        userService.accountMoney();
    }

事务操作(Spring 事务管理介绍) 35

开发中一般写到Service层,用声明式事务管理,基于注解方式,用到了aop。
比如问到项目中哪里使用到了aop,就答事务
在这里插入图片描述

事务操作(注解声明式事务管理) 35

在这里插入图片描述
在这里插入图片描述

事务操作(声明式事务管理参数配置) 36

正式内容

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

补充内容

1、在 service 类上面添加注解@Transactional,在这个注解里面可以配置事务相关参数

2、propagation:事务传播行为,即当一个事务方法被另外一个事务方法调用时,这个事务方法如何进行

  • requied
    在这里插入图片描述
  • required-new
    在这里插入图片描述
  • supports

在这里插入图片描述

3、ioslation:事务隔离级别

有三个读问题:脏读、不可重复读、虚(幻)读

Mysql默认是REPEATABLE_READ可重复读

4、timeout:超时时间

(1)事务需要在一定时间内进行提交,如果不提交进行回滚
(2)默认值是 -1 ,设置时间以秒单位进行计算

@Service
@Transactional(readOnly = false,timeout = -1,propagation = Propagation.REQUIRED,isolation = Isolation.REPEATABLE_READ)
public class UserService {
    
    

5、readOnly:是否只读

(1)读:查询操作,写:添加修改删除操作
(2)readOnly 默认值 false,表示可以查询,可以添加修改删除操作
(3)设置 readOnly 值是 true,设置成 true 之后,只能查询

6、rollbackFor:回滚

(1)设置出现哪些异常进行事务回滚

7、noRollbackFor:不回滚

(1)设置出现哪些异常不进行事务回滚

事务操作(XML 声明式事务管理)不重要,实际中用注解方式 38页

在这里插入图片描述
在这里插入图片描述

事务操作(完全注解声明式事务管理)

在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/m0_50736744/article/details/126197755
今日推荐