1.JdbcTemplate简单使用

1.1.引入相关依赖包

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.32</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>

1.2.创建spring配置

@Configuration
@ComponentScan({"ox"})
@PropertySource("classpath:jdbc.properties")
public class SpringConfiguration { // 1.数据连接信息
// jdbc.properties不能使用username这个key.spring占用了,所以要加前缀
@Value("${jdbc.driver}")
private String driver;
@Value("${jdbc.url}")
private String jdbcUrl;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password; /**
* 2.创建数据源
*/
@Bean
public ComboPooledDataSource comboPooledDataSource() throws PropertyVetoException {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass(driver);
dataSource.setJdbcUrl(jdbcUrl);
dataSource.setUser(username);
dataSource.setPassword(password);
return dataSource;
} /**
* 3.创建jdbcTemplate
* 方法形参 自动注入
*/
@Bean
public JdbcTemplate jdbcTemplate(ComboPooledDataSource comboPooledDataSource){
return new JdbcTemplate(comboPooledDataSource);
}
}

1.3.jdbcTemplate使用

a.聚合查询

@Test
public void t01(){
Long count = jdbcTemplate.queryForObject(
"select count(*) from account",
Long.class);
System.out.println("数据库有"+count+"条数据!");
}

b.查询单个数据

@Test
public void t02(){
Account account = jdbcTemplate.queryForObject(
"select * from account where name=?",
new BeanPropertyRowMapper<Account>(Account.class),
"张三");
System.out.println(account);
}

c.查询多个

@Test
public void t03(){
List<Account> accountList = jdbcTemplate.query(
"select * from account",
new BeanPropertyRowMapper<Account>(Account.class)); for (Account account : accountList) {
System.out.println(account);
}
}

d.插入

@Test
public void t04(){
Account account = new Account();
account.setName("王五");
account.setMoney(19.1d);
int update = jdbcTemplate.update(
"insert into account values(?,?)",
account.getName(),
account.getMoney()); System.out.println(update > 0 ? "success" : "fail");
}

e.修改

@Test
public void t05(){
int update = jdbcTemplate.update(
"update account set money = 100 where name = ?",
"王五"); System.out.println(update > 0 ? "success" : "fail");
}

f.删除

@Test
public void t06(){
int flag = jdbcTemplate.update(
"delete from account where name =?",
"王五"); System.out.println(flag > 0 ? "success" : "fail");
}

2.声明式事务

2.1.简述

2.2.相关对象

2.2.1.PlatformTransactionManager

PlatformTransactionManager 接口是 spring 的事务管理器,它里面提供了我们常用的操作事务的方法。

一般使用实现类DataSourceTransactionManager或HibernateTransactionManager

2.2.2.TransactionDefinition

TransactionDefinition 是事务的定义信息对象.定义了事务的隔离级别和传播行为

2.2.3.TransactionStatus

TransactionStatus 接口提供的是事务具体的运行状态

2.3 事务隔离级别

设置隔离级别,可以解决事务并发产生的问题,如脏读、不可重复读和虚读。

  • ISOLATION_DEFAULT
  • ISOLATION_READ_UNCOMMITTED
  • ISOLATION_READ_COMMITTED
  • ISOLATION_REPEATABLE_READ
  • ISOLATION_SERIALIZABLE

2.4 事务传播行为

  • REQUIRED:如果当前没有事务,就新建一个事务,如果已经存在一个事务中,加入到这个事务中。一般的选择(默认值)
  • SUPPORTS:支持当前事务,如果当前没有事务,就以非事务方式执行(没有事务)
  • MANDATORY:使用当前的事务,如果当前没有事务,就抛出异常
  • REQUERS_NEW:新建事务,如果当前在事务中,把当前事务挂起。
  • NOT_SUPPORTED:以非事务方式执行操作,如果当前存在事务,就把当前事务挂起
  • NEVER:以非事务方式运行,如果当前存在事务,抛出异常
  • NESTED:如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则执行 REQUIRED 类似的操作
  • 超时时间:默认值是-1,没有超时限制。如果有,以秒为单位进行设置
  • 是否只读:建议查询时设置为只读

2.5.基于xml的声明式事务

2.5.1.什么时声明式事务

是什么:在配置文件中声明,用在 Spring 配置文件中声明式的处理事务来代替代码式的处理事务。

作用:事务管理不侵入开发的组件

底层:Spring 声明式事务控制底层就是AOP

2.5.2.配置实现

<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/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
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--平台事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean> <!--事务增强配置-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*"/>
</tx:attributes>
</tx:advice> <!--事务的aop增强-->
<aop:config>
<aop:pointcut id="myPointcut" expression="execution(* xxx.yyy.service.impl.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut"></aop:advisor>
</aop:config>

2.5.3.切点方法的事务参数的配置

<!--事务增强配置-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>

其中,tx:method 代表切点方法的事务参数的配置,例如:

<tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" timeout="-1" read-only="false"/>
  • name:切点方法名称
  • isolation:事务的隔离级别
  • propogation:事务的传播行为
  • timeout:超时时间
  • read-only:是否只读

2.6.基于注解的声明式事务

2.6.1.使用步骤

a.在需要事务控制的类或方法上添加@Transactional

b.在xml中开启事务的注解驱动

<tx:annotation-driven/>

2.6.2.@Transactional

  • 使用 @Transactional 在需要进行事务控制的类或是方法上修饰,注解可用的属性同 xml 配置方式,例如隔离级别、传播行为等。
  • 注解使用在类上,那么该类下的所有方法都使用同一套注解参数配置。
  • 使用在方法上,不同的方法可以采用不同的事务参数配置。
  • Xml配置文件中要开启事务的注解驱动<tx:annotation-driven/>
  • 注解配置文件中开启事务注解驱动@EnableTransactionManagement

spring入门3-jdbcTemplate简单使用和声明式事务的更多相关文章

  1. Spring的编程式事务和声明式事务

    事务管理对于企业应用来说是至关重要的,当出现异常情况时,它也可以保证数据的一致性. Spring事务管理的两种方式 spring支持编程式事务管理和声明式事务管理两种方式. 编程式事务使用Transa ...

  2. Spring的 JDBCTemplate和声明式事务控制

    Spring 中的 JdbcTemplate[会用] JdbcTemplate 概述 它是 spring 框架中提供的一个对象,是对原始 Jdbc API 对象的简单封装.spring 框架为我们提供 ...

  3. Spring笔记(4) - Spring的编程式事务和声明式事务详解

    一.背景 事务管理对于企业应用而言至关重要.它保证了用户的每一次操作都是可靠的,即便出现了异常的访问情况,也不至于破坏后台数据的完整性.就像银行的自助取款机,通常都能正常为客户服务,但是也难免遇到操作 ...

  4. Spring框架——事务处理(编程式和声明式)

     一. 事务概述 ●在JavaEE企业级开发的应用领域,为了保证数据的完整性和一致性,必须引入数据库事务的概念,所以事务管理是企业级应用程序开发中必不可少的技术. ●事务就是一组由于逻辑上紧密关联而合 ...

  5. Spring编程式和声明式事务实例讲解

    Java面试通关手册(Java学习指南):https://github.com/Snailclimb/Java_Guide 历史回顾: 可能是最漂亮的Spring事务管理详解 Spring事务管理 S ...

  6. 【Spring】编程式事务和声明式事务

    一.概述 二.准备工作 1. 创建表 2. 创建项目并引入Maven依赖 3. 编写实体类 4. 编写Dao层 5. 业务层 6. XML中的配置 7. 测试 三.编程式事务 1. 在业务层代码上使用 ...

  7. Spring编程式事务管理和声明式事务管理

    本来想写一篇随笔记一下呢,结果发现一篇文章写的很好了,已经没有再重复写的必要了. https://www.ibm.com/developerworks/cn/education/opensource/ ...

  8. spring入门之JdbcTemplate 操作crud

    Spring 通过调用 JdbcTemplate来实现对数据库的增删改查,主要用到JdbcTemplate类的4个方法,首先,配置数据库信息,创建对象,代码通用: //设置数据库信息 DriverMa ...

  9. 【spring 7】spring和Hibernate的整合:声明式事务

    一.声明式事务简介 Spring 的声明式事务管理在底层是建立在 AOP 的基础之上的.其本质是对方法前后进行拦截,然后在目标方法开始之前创建或者加入一个事务,在执行完目标方法之后根据执行情况提交或者 ...

随机推荐

  1. Java之JSP

    JSP JSP简介 JSP指的是 JavaServerPages ,Java服务器端页面,也和Servlet一样,用来开发动态web JSP页面中可以嵌入java代码为用户提供动态数据 JSP原理 J ...

  2. IDE快捷键的使用

    ctrl+ait+l,整理代码 ctrl+atl+v,生成等号左边的类型和变量 shift+方向键,选择内容 ctrl+方向键,自己领悟.常常与shift同时使用 ctrl+alt+方向键,光标前进或 ...

  3. 多线程同步AutoResetEvent 和ManualResetEvent

  4. dataTemplate 的使用之listView

    <Window x:Class="WpfApplication1.Window39" xmlns="http://schemas.microsoft.com/win ...

  5. 设置一个元素的HTML内容

    问题 你需要一个元素中的HTML内容 方法 可以使用Element中的HTML设置方法具体如下: Element div = doc.select("div").first(); ...

  6. JFrame显示刷新

    1 import java.awt.BorderLayout; 2 import java.awt.Font; 3 import java.awt.event.ActionEvent; 4 impor ...

  7. C程序设计学习笔记(完结)

    时间:2015-4-16 09:17 不求甚解,每有会意,欣然忘食.学习的过程是痛苦的 第1章    程序设计和C语言     第2章    算法--程序的灵魂   -算法的五个特点          ...

  8. Go与接口:接口即约定

    接口 接口类型是对其他类型行为的概括与抽象.我们可以通过接口来约定某一类通用行为.Go语言的接口是隐式的:只要实现接口A的所有方法就代表实现了接口A. 接口即约定 接口是什么样的? package i ...

  9. SpEL表达式注入漏洞学习和回显poc研究

    目录 前言 环境 基础学习和回显实验 语法基础 回显实验 BufferedReader Scanner SpEL漏洞复现 低版本SpringBoot中IllegalStateException CVE ...

  10. Nginx从安装到虚拟主机、https加密、重定向的设置

    编译前的设置: 在源代码文件中把版本号注释掉,这是为了防止针对特定版本的恶意攻击 关闭编译时的调试模式 解决编译前的依赖性 进行配置参数: 对参数进行解读: 编译和安装: 做软链接方便调用: 创建ng ...