一、JDBC Template基本使用

1.开发步骤

1.1直接使用template

  1. 导入spring-jdbc和spring-tx坐标

    <!--    JDBC-->
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.0.3.RELEASE</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>5.0.3.RELEASE</version>
    </dependency>
    <!-- druid-->
    <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.0.9</version>
    </dependency>
    <!-- c3po-->
    <dependency>
    <groupId>c3p0</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.1.2</version>
    </dependency>
    <!-- mysql -->
    <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.23</version>
    </dependency>
    </dependencies>
  2. 创建数据库表和实体

  3. 创建JDBCTemplate对象

  4. 执行数据库操作

    /**
    * 测试JDBCTemplate开发步骤
    */
    @Test
    public void jdbcTest() throws PropertyVetoException {
    //创建数据源 c3p0
    ComboPooledDataSource dataSource = new ComboPooledDataSource();
    dataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
    dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/account");
    dataSource.setUser("root");
    dataSource.setPassword("gyb20010204"); //创建template
    JdbcTemplate template = new JdbcTemplate();
    //设置数据源
    template.setDataSource(dataSource);
    //执行操作
    int rows = template.update("insert into account values(?,?)", "tom", 200);
    System.out.println(rows);
    }

1.2使用Spring容器注入template

  1. 注入bean对象

  2. 依赖注入数据源

    <!--    注入数据源对象-->
    <bean id ="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="com.mysql.cj.jdbc.Driver"/>
    <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/account"/>
    <property name="user" value="root"/>
    <property name="password" value="gyb20010204"/>
    </bean>
    <!-- 注入jdbc模板对象-->
    <bean id="template" class="org.springframework.jdbc.core.JdbcTemplate">
    <!-- 依赖注入数据源-->
    <property name="dataSource" ref="dataSource"/>
    </bean>
  3. 测试

    @Test
    public void jdbcSpringTest(){
    ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
    JdbcTemplate template = app.getBean(JdbcTemplate.class);
    int rows = template.update("insert into account values(?,?)", "jack", 300);
    System.out.println(rows);
    }

2.CRUD操作

增,删,改:update

int rows = template.update("insert into account values(?,?)", "jack", 300);

查:query(结果为list集合)

	List<Account> accountList = template.query(sql, new BeanPropertyRowMapper<Account>(Account.class));

queryForObject(结果为 对象)

	Account account = template.queryForObject(sql, new BeanPropertyRowMapper<Account>(Account.class));

queryForObject(结果为数字)

	Integer integer = template.queryForObject("select count(*) from account", Integer.class);

二、Spring的事务控制

1.编程事务控制相关对象

管理+定义 = 状态

1.1 PlatformTransactionManager 事务平台管理器 接口(需要配置)

实现根据不同的dao层技术来实现

​ ①、TransactionStatus getTransaction(TransactionDefinition definition) ,事务管理器 通过TransactionDefinition,获得“事务状 态”,从而管理事务。

​ ②、void commit(TransactionStatus status) 根据状态提交

​ ③、void rollback(TransactionStatus status) 根据状态回滚

1.2.TransactionDefinition 事务定义 接口(需要配合)

  • ​ 设置事务隔离级别
  • ​ 设置事务传播行为

​ Spring中的7个事务传播行为:

---- 如果A调用B 假设A.., 则....
PROPAGATION_REQUIRED 支持当前事务,假设当前没有事务。就新建一个事务
PROPAGATION_SUPPORTS 支持当前事务,假设当前没有事务,就以非事务方式运行
PROPAGATION_MANDATORY 支持当前事务,假设当前没有事务,就抛出异常
PROPAGATION_REQUIRES_NEW 新建事务,假设当前存在事务。把当前事务挂起
PROPAGATION_NOT_SUPPORTED 以非事务方式运行操作。假设当前存在事务,就把当前事务挂起
PROPAGATION_NEVER 以非事务方式运行,假设当前存在事务,则抛出异常
PROPAGATION_NESTED 如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则执行与PROPAGATION_REQUIRED类似的操作。
超时时间
是否只读

1.3.TransactionStatus 事务状态(被动 不需要配置)

名称 说明
void flush() 刷新事务
boolean hasSavepoint() 获取是否存在保存点
boolean isCompleted() 获取事务是否完成
boolean isNewTransaction() 获取是否是新事务
boolean isRollbackOnly() 获取是否回滚
void setRollbackOnly() 设置事务回滚

2.基于XML的声明式事务控制

2.1作用

业务代码事务控制通过 事务配置 的方式进行松耦合

2.3方法

​ 采用AOP的思想:切点(业务)

通知(事务控制)

​ 将业务进行增强,从而达成事务配置,并且松耦合

2.4步骤

  1. 创建目标对象,编写切入点

  2. 在applicationContext中配置[平台事务管理器](#1.1 PlatformTransactionManager 事务平台管理器 接口(需要配置)),其中配置数据源(上一章中的平台事务管理器)

    <!--    平台事务管理器-->
    <bean id="transactionManger" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
    </bean>
  3. 在applicationContext中加入tx命名空间

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    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.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
  4. 编写事务的增强,将平台事务管理器配入(上一章中的[事务定义](#1.2.TransactionDefinition 事务定义 接口(需要配合)))

    <!--    通知,事务的增强-->
    <tx:advice id="txAdvice" transaction-manager="transactionManger">
    <!-- 配置事物的属性信息-->
    <tx:attributes>
    <tx:method name="trans" isolation="DEFAULT"/>
    <tx:method name="*"/>
    </tx:attributes>
    </tx:advice>
  5. 编写AOP配置(配入切点(事务)配入增强(事务增强))

    <!--    配置织入-->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* Service.Impl.*.*(..))"></aop:advisor>
</aop:config>

3.基于注解的声明式事务控制

注意:

  1. 使用@Transactional在需要进行事务控制的或者方法上修饰,注解也可配入事务定义信息
  2. 注解使用在类上,那么该类下的所有方法都是用同一套注解参数配置
  3. xml配置文件中要开启事务注解驱动注解扫描
<tx:annotation-driven/>

Spring笔记(3)的更多相关文章

  1. Spring笔记02_注解_IOC

    目录 Spring笔记02 1. Spring整合连接池 1.1 Spring整合C3P0 1.2 Spring整合DBCP 1.3 最终版 2. 基于注解的IOC配置 2.1 导包 2.2 配置文件 ...

  2. Spring笔记01_下载_概述_监听器

    目录 Spring笔记01 1.Spring介绍 1.1 Spring概述 1.2 Spring好处 1.3 Spring结构体系 1.4 在项目中的架构 1.5 程序的耦合和解耦 2. Spring ...

  3. Spring 笔记 -06- 从 MySQL 建库到 登录验证数据库信息(maven)

    Spring 笔记 -06- 从 MySQL 建库到 登录验证数据库信息(maven) 本篇和 Spring 没有什么关系,只是学习 Spring,必备一些知识,所以放在这里了. 本篇内容: (1)M ...

  4. Spring笔记:事务管理

    Spring笔记:事务管理 事务管理 Spring事务管理是通过SpringAOP去实现的.默认情况下Spring在执行方法抛出异常后,引发事务回顾,当然你可以用拦截器或者配置去改变它们. 这部门内容 ...

  5. Spring笔记:AOP基础

    Spring笔记:AOP基础 AOP 引入AOP 面向对象的开发过程中,我们对软件开发进行抽象.分割成各个模块或对象.例如,我们对API抽象成三个模块,Controller.Service.Comma ...

  6. Spring:笔记整理(1)——HelloWorld

    Spring:笔记整理(1)——HelloWorld 导入JAR包: 核心Jar包 Jar包解释 Spring-core 这个jar 文件包含Spring 框架基本的核心工具类.Spring 其它组件 ...

  7. Spring笔记:IOC基础

    Spring笔记:IOC基础 引入IOC 在Java基础中,我们往往使用常见关键字来完成服务对象的创建.举个例子我们有很多U盘,有金士顿的(KingstonUSBDisk)的.闪迪的(SanUSBDi ...

  8. Spring笔记(6) - Spring的BeanFactoryPostProcessor探究

    一.背景 在说BeanFactoryPostProcessor之前,先来说下BeanPostProcessor,在前文Spring笔记(2) - 生命周期/属性赋值/自动装配及部分源码解析中讲解了Be ...

  9. spring笔记----看书笔记

    上周末看了一章以前javaee轻量级的书spring部分,简单做了一些笔记 // ApplicationContext ac=new ClassPathXmlApplicationContext(&q ...

  10. Spring 笔记(三)Bean 装配

    前言 Spring 有两大核心,也就分成两份笔记分别记录. 其一是管理应用中对象之间的协作关系,实现方式是依赖注入(DI),注入依赖的过程也被称为装配(Wiring). 基于 JavaConfig 的 ...

随机推荐

  1. HTML之单词

    段落 paragraph <a href="https://www.runoob.com">超链接 anchor (锚点,引申为连接,link已经被html占用) Hy ...

  2. 并发队列ConcurrentLinkedQueue与LinkedBlockingQueue源码分析与对比

    目录 前言 ConcurrentLinkedQueue 使用方法 存储结构 初始化 入队 出队 获取容器元素数量 LinkedBlockingQueue 使用方法 存储结构 初始化 入队 出队 获取容 ...

  3. 【阅读笔记】Java核心技术卷一 #3.Chapter5

    5 继承 5.1 类.超类和子类 5.1.1 定义子类 超类(superclass)和子类(subclass), 基类(base class)和派生类(derived class), 父类(paren ...

  4. ASP.NET Datalist制作显示效果和img的数据库存储

    1. 具体实现效果如下图: 2.首先使用datalist控件编辑模板,在属性面板选择RepeatColumns="3" RepeatDirection="Horizont ...

  5. php 随机生成字符串

    private function createNonceStr($length = 16) { $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJK ...

  6. LeetCode通关:求次数有妙招,位运算三连

    分门别类刷算法,坚持,进步! 刷题路线参考: https://github.com/chefyuan/algorithm-base 大家好,我是刷题困难户老三,这一节我们来刷几道很有意思的求次数问题, ...

  7. 04.委托Delegation

    1. 基本了解 1.1 委托简述 官方文档 委托是一种引用类型,表示对具有特定参数列表和返回类型的方法的引用,用于将方法作为参数传递给其他方法,可将任何可访问类或结构中与委托类型匹配的任何方法分配给委 ...

  8. 原生ajax练习-post&xml

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. 【错误】element cannot be mapped to a null key

    element cannot be mapped to a null key的解决方法 报错: ERROR [o.a.c.c.C.[.[.[/sa].[dispatcherServlet]] - Se ...

  10. 解决vscode+python不提示numpy函数的问题

    前言 使用vscode编写numpy代码时,对于numpy.array()等方法总是无法提示.查找了很多博客后,大部分都是修改配置和安装多种vscode插件,经过尝试后方法对于我来说无效.最后在调试p ...