当前位置: 首页 > news >正文

spring笔记2

一、基于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.lryc.cn/news/341613.html

相关文章:

  • 【挑战30天首通《谷粒商城》】-【第一天】02、简介-项目整体效果展示
  • Kafka 生产者应用解析
  • GEE错误——image.reduceRegion is not a function
  • rk356x 关于yocto编译linux及bitbake实用方法
  • Chrome您的连接不是私密连接 |输入“thisisunsafe”命令绕过警告or添加启动参数
  • 牛客面试前端1
  • Linux的软件包管理器-yum
  • 选择排序(Selection Sort)
  • 网络面试题目
  • Web,Sip,Rtsp,Rtmp,WebRtc,专业MCU融屏视频混流会议直播方案分析
  • Unreal 编辑器工具 批量重命名资源
  • Voice Conversion、DreamScene、X-SLAM、Panoptic-SLAM、DiffMap、TinySeg
  • 短信群发平台分析短信群发的未来发展趋势
  • supervisord 使用指南
  • AngularJS 的生命周期和基础语法
  • docker-compose 网络
  • 农药生产厂污废水如何处理达标
  • 根据相同的key 取出数组中最后一个值
  • Github Action Bot 开发教程
  • 使用docker创建rocketMQ主从结构,使用
  • 一次完整的 http 请求是怎样的?
  • 并行执行的概念—— 《OceanBase 并行执行》系列 一
  • 使用 ipdb 调试回调函数
  • 介绍一下mybatis的基本配置(mybatis-config.xml)
  • 【MySQL】第一次作业
  • 10个免费视频素材网站,剪辑师们赶紧收藏!
  • 【毕业设计】基于SSM的运动用品商城的设计与实现
  • 【Web】CTFSHOW 中期测评刷题记录(1)
  • vs配置cplex12.10
  • Kubernetes 弃用Docker后 Kubelet切换到Containerd