spring笔记2

ops/2024/10/11 13:21:59/

一、基于xml的AOP实现

基于注解管理Bean,注解+扫描

<context:component-scan base-package="com.zhou.spring.aop.xml"></context:component-scan><aop:config>
<!--        设置一个公共的切入点表达式--><aop:pointcut id="pointCut" expression="execution(* com.zhou.spring.aop.xml.CalculatorImpl.*(..))"/>
<!--        将IOC容器中的某个bean设置为切面--><aop:aspect ref="loggerAspect"><aop:before method="beforeAdviceMethod" pointcut-ref="pointCut"></aop:before><aop:after method="afterAdviceMethod" pointcut-ref="pointCut"></aop:after><aop:after-returning method="afterReturningAdviceMethod" pointcut-ref="pointCut" returning="result"></aop:after-returning><aop:after-throwing method="afterThrowingAdvice" pointcut-ref="pointCut" throwing="ex"></aop:after-throwing><aop:around method="aroundAdviceMethod" pointcut-ref="pointCut"></aop:around></aop:aspect></aop:config>

二、JDBCTemplate

1.配置JDBCTemplate

首先引入jdbc的property文件,接着设置数据源,JDBCTemplate是一个对象,需要ioc的管理


<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder><bean  id="dataSource" 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><bean class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource"></property></bean>

2.spring整合junit

指定测试类在spring的测试环境下执行,此时就可以通过注入的方式直接获取IOC容器中的bean
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-jdbc.xml")

3.jdbcTemplate实现查询功能

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-jdbc.xml")
public class JDBCTemplateTest {@Autowiredprivate JdbcTemplate jdbcTemplate;@Testpublic void testInsert(){String sql="insert into t_user values(null,?,?,?,?,?)";jdbcTemplate.update(sql,"root","123",23,"女","123@qq.com");}@Testpublic void testGetUserById(){String sql="select * from t_user where id=?;";User user = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(User.class), 41);System.out.println(user);}@Testpublic void testGetAllUser(){String sql="select * from t_user";List<User> users = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(User.class));users.forEach(System.out::println);}
@Testpublic void testGetCount(){String sql="select count(*) from t_user";Integer count = jdbcTemplate.queryForObject(sql, Integer.class);System.out.println(count);
}

三、声明式事务

1.声明式事务的概念

编程式 自己写代码 实现功能
声明式 :通过 配置 框架 实现功能

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

a>无事务的实现
Mysql中一个sql语句独占一个事务且自动提交。
b>实现事务功能
* 声明式事务的配置步骤:
* 1.配置事务管理器
* 2.开启事务的注解驱动
* 在需要被事务管理的方法上,添加@Transactional注解,该方法就会被事务管理
* Transactional注解标识的位置:
* 1.标识在方法上
* 2.标识在类上,则类中所有的方法都会被事务管理
<!--配置事务管理器--><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property></bean>
<!--    开启事务的注解驱动
将使用@Transactional注解所标识的方法或类中所有的方法使用事务管理进行管理
transaction-manager属性设置事务管理器的id--><tx:annotation-driven transaction-manager="transactionManager"/>

3.声明式事务的属性

a>只读readOnly

只有当当前事务中全部都是查询操作时才可用只读,才可从数据库层面去优化当前的操作。

b>超时timeout=-1(默认一直等)
eg.timeout=3,超时时间是3秒钟,如果超过3秒事务没有执行完,当前的事务回滚并抛出异常
c>回滚策略
声明式事务默认只针对运行时异常回滚,编译时异常不回滚。
可以通过 @Transactional 中相关属性设置回滚策略
  @Transactional(
//            noRollbackFor = ArithmeticException.classnoRollbackForClassName = "java.lang.ArithmeticException")
rollbackFor 属性:需要设置一个 Class 类型的对象
rollbackForClassName 属性:需要设置一个字符串类型的全类名
noRollbackFor 属性:需要设置一个 Class 类型的对象
rollbackFor 属性:需要设置一个字符串类型的全类名
d>事务的隔离级别
 
读未提交: READ UNCOMMITTED
允许 Transaction01 读取 Transaction02 未提交的修改。
读已提交: READ COMMITTED
要求 Transaction01 只能读取 Transaction02 已提交的修改。
可重复读: REPEATABLE READ
确保 Transaction01 可以多次从一个字段中读取到相同的值,即 Transaction01 执行期间禁止其它
事务对这个字段进行更新。
串行化: SERIALIZABLE
确保 Transaction01 可以多次从一个表中读取到相同的行,在 Transaction01 执行期间,禁止其它
事务对这个表进行添加、更新、删除操作。可以避免任何并发问题,但性能十分低下。
e>传播行为
//            使用的是被调用事务本身的事务
//            propagation = Propagation.REQUIRES_NEW
//            使用的是调用的事务,默认propagation = Propagation.REQUIRED

4.基于xml的声明式事务

<!--    配置事务通知,即事物的属性,注意:切入点是连接点的实现方式-->
<tx:advice id="tx" transaction-manager="transactionManager"><tx:attributes>
<!--        表示切入点表达式所对应的连接点的所有方法都会被事务管理--><tx:method name="*"/></tx:attributes>
</tx:advice>
<aop:config><aop:advisor advice-ref="tx" pointcut="execution(* com.zhou.spring.impl.*.*(..))"></aop:advisor>
</aop:config>
注意:基于xml实现的声明式事务,必须引入aspect的依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.3.1</version>
</dependency>


http://www.ppmy.cn/ops/35965.html

相关文章

一切历史都是当代史,每个人都可以预测这个世界的未来

目录 每个人都可以预测这个世界的未来,只要他足够了解这个世界的过往;

测径仪视窗镜片的维护和保养步骤

关键字:测径仪镜片,测径仪保养,测径仪维护,视窗镜片维护,视窗镜片擦拭保养,视窗镜片的检查, 视窗镜片定期保养 视窗镜片是保护光学镜头免受污染和损伤的光学平镜片&#xff0c;它的污染和破损会直接影响光学系统的测量结果。 视窗镜片一般在受到轻微污染&#xff08;指镜片上…

Linux下制作Nginx绿色免安装包

前言 linux下安装nginx比较繁琐&#xff0c;遇到内网部署环境更是麻烦&#xff0c;所以研究了下nginx绿色免安装版的部署包制作&#xff0c;开箱即用&#xff0c;特此记录分享&#xff0c;一下操作在centos8环境下安装&#xff0c;如果需要其他内核系统的安装&#xff08;Debi…

oracle试用期过期,解决办法

过期重置方法&#xff0c;删除注册表&#xff0c;相当于无限试用&#xff0c;缺点每30天都要重置一次 1. window r 输入 regedit 确定&#xff0c;打开注册表 2.删除下图里的两个文件夹 3.重启 plsql,登录成功

八股设计模式(一)

目录 1 工厂方法模式 1.1 概述 1.2 简单工厂模式 1.2.1 结构 1.2.2 实现 1.2.3 优缺点 1.2.4 扩展 1.3 工厂方法模式 1.3.1 概念 1.3.2 结构 1.3.3 实现 1.3.4 优缺点 1.4 抽象工厂模式 1.4.1 概念 1.4.2 结构 1.4.3 实现 1.4.4 优缺点 1.4.5 使用场景 2 策略…

【一起深度学习——NIN】

NIN神经网络 原理图&#xff1a;代码实现&#xff1a;输出结果&#xff1a; 原理图&#xff1a; 代码实现&#xff1a; import torch from torch import nn from d2l import torch as d2ldef nin_block(in_channels, out_channels, kernel_size, strides, padding):return nn.…

模拟实现小米商城(有源码)

项目介绍 这是一个只有前端没有后端的项目, 适合于基础前端课设.该前端项目没有花里胡哨的特效, 纯手写 HTML CSS JS, 特别适合基础小白入门, 或者做为基础的课设(含有组员介绍页面), 此项目中, 充分使用 flex 布局, 绝对 相对定位, css 动画, 封装公共样式区域, 代码风格及命…

Java 笔记 08:Scanner 类的使用方法

一、前言 记录时间 [2024-04-28] 系列文章简摘&#xff1a; Java 笔记 01&#xff1a;Java 概述&#xff0c;MarkDown 常用语法整理 Java 笔记 04&#xff1a;Java 数据类型基础&#xff0c;数据类型转换&#xff0c;及其相关场景拓展 Java 笔记 05&#xff1a;变量和常量相关知…