目录

SpringSpring整合以及事务控制

【Spring】Spring整合以及事务控制

Spring整合以及事务

Spring整合

Spring整合Junit

导入依赖

https://i-blog.csdnimg.cn/img_convert/062b604a8a824db43c7e1614e8f62b34.png

创建测试类

https://i-blog.csdnimg.cn/img_convert/bfd0d2fd11cdbda11ebc34cd3fba8fbd.png

也可以使用这种方法找到xml文件位置

https://i-blog.csdnimg.cn/img_convert/3ed931320cd74f29b41ca6b7fe047f81.png


Spring整合Mybatis

导入依赖

https://i-blog.csdnimg.cn/img_convert/d2556f367cb20acc3bcde6d39beab7df.png

JDBC配置文件

https://i-blog.csdnimg.cn/img_convert/1aaa5325af6c86beb9edb4796938fc5c.png

spring核心xml文件配置

https://i-blog.csdnimg.cn/img_convert/76473167dd673ed4407224e29b1a5bb1.png

mybatis核心配置文件

https://i-blog.csdnimg.cn/img_convert/a91818fda1d7e2f585041eca068410ee.png

dao接口和实现类

https://i-blog.csdnimg.cn/img_convert/cee80dde89c4448e3c1e3cb7e3710ebe.png

https://i-blog.csdnimg.cn/img_convert/b5606cb726cfcab0285aa9fd73586868.png

实现类对应mapper文件

https://i-blog.csdnimg.cn/direct/573c13949fdf4458aaa77fe4bc42c72b.png


sqlSessionFactoryBean

https://i-blog.csdnimg.cn/img_convert/252b636f36904d59fd6370d998f96b7f.png

基础的MyBatis中获取sqlSessionFactory对象

https://i-blog.csdnimg.cn/img_convert/a553a0102505d0b7883db7955be878ec.png

Spring的xml文件中配置sqlSessionFactoryBean

https://i-blog.csdnimg.cn/img_convert/4b6c1b430a09c1afb5747e890f4b08e0.png

实际上就是new了一个sqlSessionFactoryBean对象

这里是通过getObject()来获取sqlSessionFactory对象

源码:

https://i-blog.csdnimg.cn/img_convert/f755b9bc52ce0d1d0a469d812a51ba67.png


Srping解耦合

三层架构

https://i-blog.csdnimg.cn/img_convert/2e58aaae2f1c373c4e8f6a83384e75d7.png

service实现类需要依赖dao实现类

没有spring的时候需要new dao接口的实现类

https://i-blog.csdnimg.cn/img_convert/a0ccf646789ddf40b11c72a05bd49afd.png

servlet没有实现类,要依赖service实现类

new了service的实现类

https://i-blog.csdnimg.cn/img_convert/ad00ea6d0d6bcf4335b7c6aa4fa1e718.png

spring改进测试类

https://i-blog.csdnimg.cn/img_convert/33712355ccae37578d1dfbebf6068e31.png


Spring事务

注解实现事务

spring配置文件

https://i-blog.csdnimg.cn/img_convert/7cecc4e3ccef87917c281dd73b8331da.png

@Transactional添加事务

可以添加在方法和类上,添加到类就说明这个类所有的方法都支持事务

一般都在service层添加事务

https://i-blog.csdnimg.cn/img_convert/807d04eee8f61e3605b68491bf7e180d.png

事务的底层逻辑就是通过AOP增强解耦合

所以要加上AOP依赖

https://i-blog.csdnimg.cn/img_convert/acac1671a95a5156d46d1587adb205ff.png


xml实现事务(可跳过)

https://i-blog.csdnimg.cn/img_convert/ab3ed6ac251fa5436c8a1e1e0a296f8d.png


传播行为 propagation (重点)

事务方法嵌套调用 时,需要控制是否开启新事务,可以使用事务传播行为来控制

https://i-blog.csdnimg.cn/direct/e48f9864d79e44c69c557d7761ded98a.png

https://i-blog.csdnimg.cn/img_convert/b2b0670b539b141e8b9769df200c806e.png

@Transactional本质

https://i-blog.csdnimg.cn/img_convert/b08e7ca5d4307c2c1e81df11f4f9249a.png

我们需要 日志出现异常的时候不需要回滚转账事务

就需要控制事务的传播行为

https://i-blog.csdnimg.cn/img_convert/8a524c60ec2246489ed501fbb14a7c53.png

默认情况下,外层test方法执行的时候有一个外层事务包裹,内层的转账和打印日志两个事务就加入这个外层的事务了, 本身不会有单独的事务包裹

我们可以给转账事务设置REQUIRES_NEW

这时候这个转账事务就会在外层test事务的包裹下还会新建一个自身单独的事务

转账完成后会自动提交

https://i-blog.csdnimg.cn/img_convert/caeddd22841a5bba4be9eae2a0127b6f.png

https://i-blog.csdnimg.cn/direct/444cb2d646fc4d709e8c2fe5ff958550.png

实际上就是new了一个新的连接对象,log还是和外层用一个连接对象


隔离级别isolation

https://i-blog.csdnimg.cn/img_convert/22e7aabc54b7f11fb634c343ff15a85d.png

https://i-blog.csdnimg.cn/img_convert/69d7c5197dac71ca48a906d9b6367b38.png


只读readOnly

https://i-blog.csdnimg.cn/img_convert/426c97b6c2adbac25dd8fabfba71180b.png