一、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. Windows10 64位解决无法使用Microsoft.Jet.OLEDB.4.0的方法

    本机软件环境:Windows10 64位+Office2003 (32位) ============================================= 1.下载 ACE2010的驱动, ...

  2. 第二十八篇 -- 写一个简陋的WIFI服务器界面

    效果图: Dlg.cpp // WIFIWMITestDlg.cpp : implementation file // #include "stdafx.h" #include & ...

  3. 利用C++11可变模板,封装调用dll导出函数

    起因 开发中经常需要动态调用一些导出函数,试着利用C++11特性封装一下 尝试 常规使用 typedef int WINAPI (*TMessageBoxA)(HWND hWnd,LPCSTR lpT ...

  4. Gogs+Drone搭建CI/CD平台

    Gogs 是由 Go 语言编写的 Git 服务器,由中国人主导开发的一款开源项目,搭建方便并且拥有完善的中文文档,配合 Drone 可以实现持续集成/持续部署.本文介绍如何通过 Docker 搭建 G ...

  5. vue目首屏添加skeleton骨架屏

    1. 安装插件:npm install vue-skeleton-webpack-plugin 2. 在src目录下创建 Skeleton.vue <template> <div c ...

  6. 在Java开发工具的project中使用相对路径

    1.在project中,相对路径的根目录是project的根文件夹,在此就是repathtest文件夹了.创建文件的写法是: File f = new File("src/com/lavas ...

  7. postman怎么调中文

    事先准备 1 安装好postman. 下载网址:https://www.postman.com/downloads/ 2 下载好app.zip中文压缩包,下载地址:https://wws.lanzou ...

  8. [TensorFlow2.0]-正则化

    本人人工智能初学者,现在在学习TensorFlow2.0,对一些学习内容做一下笔记.笔记中,有些内容理解可能较为肤浅.有偏差等,各位在阅读时如有发现问题,请评论或者邮箱(右侧边栏有邮箱地址)提醒. 若 ...

  9. JAVA基础语法:java编程规范和常用数据类型(转载)

    JAVA基础语法:java编程规范和常用数据类型 摘要 本文主要介绍了最基本的java程序规则,和常用数据类型,其中侧重说了数组的一些操作. 面向java编程 java是纯面向对象语言,所有的程序都要 ...

  10. 记面试的一道JS题

    给一个数组arr=[1,2,3,4,5],索引第二位插入'z',设计一个函数change,调用change(arr, 2, 'z')返回一个新数组[1,2,'z',3,4,5] 我想了两种办法: 第一 ...