• Spring AOP


    概念部分:

    代码实现部分:

    代码整体布局:

    在这里插入图片描述

    运行结果:

    在这里插入图片描述

    代码部分:

    IZuFuang :
    package com.test1;
    
    public interface IZuFuang {
        void zf();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    Master :
    package com.test1;
    
    public class Master implements IZuFuang{
    
        @Override
        public void zf() {
            System.out.println("房东租房,800/月");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    ZhongJie :
    package com.test1;
    
    public class ZhongJie implements IZuFuang{
        private Master master;
    
        public ZhongJie(Master master) {
            this.master = master;
        }
    
        @Override
        public void zf() {
            System.out.println("收取20%的回扣");
            this.master.zf();
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    Test :
    package com.test1;
    
    public class Test {
        public static void main(String[] args) {
            Master master=new Master();
            ZhongJie zhongJie=new ZhongJie(master);
            zhongJie.zf();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    ②注解方式实现AOP:

    代码整体布局:

    在这里插入图片描述

    运行结果:

    在这里插入图片描述

    代码部分:

    pom.xml:
    
    
    
      4.0.0
    
      org.example
      Aop_0914_SecondStage
      1.0-SNAPSHOT
      war
    
      Aop_0914_SecondStage Maven Webapp
      
      http://www.example.com
    
      
        8
        8
        4.0.1
        2.2
        1.2
        5.3.14
        1.4
        3.4.6
        1.3.3
        8.0.11
        1.2.78
      
      
        
        
          junit
          junit
          4.12
        
        
          javax.servlet
          javax.servlet-api
          ${servlet.version}
          
          provided
        
        
          javax.servlet.jsp
          jsp-api
          ${jsp.version}
          
          provided
        
        
          jstl
          jstl
          ${jstl.version}
        
        
        
          org.springframework
          spring-webmvc
          ${spring.version}
        
        
          org.springframework
          spring-jdbc
          ${spring.version}
        
        
        
          commons-dbcp
          commons-dbcp
          ${commons-dbcp.version}
        
        
        
          org.mybatis
          mybatis
          ${mybatis.version}
        
        
        
          org.mybatis
          mybatis-spring
          ${mybatis-spring.version}
        
        
        
          com.github.pagehelper
          pagehelper
          5.1.0
        
        
        
          com.github.abel533
          mapper
          3.0.1
        
        
        
          mysql
          mysql-connector-java
          ${mysql-connector-java.version}
        
        
        
          com.alibaba
          fastjson
          ${fastjson.version}
        
        
        
          org.projectlombok
          lombok
          1.18.24
        
        
          com.fasterxml.jackson.core
          jackson-databind
          2.11.2
        
        
          com.sun.mail
          javax.mail
          1.5.6
        
        
          org.slf4j
          slf4j-api
          1.7.30
        
        
          com.github.xuwei-k
          html2image
          0.1.0
        
        
          org.aspectj
          aspectjweaver
          1.9.8
        
        
          org.slf4j
          slf4j-simple
          1.7.25
        
    
      
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    spring.xml:
    
    
    
        
        
        
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    在这里插入图片描述

    LogAdvise :
    package com.advise;
    
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.*;
    import org.springframework.stereotype.Component;
    
    @Component
    @Aspect
    public class LogAdvise {
        @Pointcut("execution(* com.service.impl.*.*(..))")
        public void pc(){
    
        }
    
        //@Before("execution(* com.service.impl.*.*(..))")
        @Before("pc()")
        public void bf(){
            System.out.println("前置通知:开始记录日志");
        }
    
        //@AfterThrowing("execution(* com.service.impl.*.*(..))")
        @AfterThrowing("pc()")
        public void af(){
            System.out.println("后置通知:结束记录日志");
        }
    
        //@Around("execution(* com.service.impl.*.*(..))")
        @Around("pc()")
        public void hr(ProceedingJoinPoint jp) throws Throwable{
            System.out.println("环绕通知:开始记录日志");
            System.out.println("业务方法:"+jp.getSignature());
            System.out.println("业务类:"+jp.getTarget());
            jp.proceed();
            System.out.println("环绕通知:结束记录日志");
        }
    
        @AfterThrowing("pc()")
        public void yc(){
            System.out.println("异常通知:记录日志");
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    User :
    package com.entity;
    
    public class User {
        private Integer userId;
        private String userName;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    IUserService :
    package com.service;
    
    import com.entity.User;
    
    import java.util.List;
    
    public interface IUserService {
        List list();
        Integer insert(User user);
        Integer update(User user);
        Integer delete(Integer userId);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    UserServiceImpl :
    package com.service.impl;
    
    import com.entity.User;
    import com.service.IUserService;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class UserServiceImpl implements IUserService {
    
        @Override
        public List list() {
            System.out.println("业务处理:查询业务");
            return new ArrayList<>();
        }
    
        @Override
        public Integer insert(User user) {
            System.out.println("业务处理:插入业务");
            return 1;
        }
    
        @Override
        public Integer update(User user) {
            System.out.println("业务处理:更新业务");
            return 1;
        }
    
        @Override
        public Integer delete(Integer userId) {
            System.out.println("业务处理:删除业务");
            return 1;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    TestAop :
    package com.test2;
    
    import com.entity.User;
    import com.service.IUserService;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class TestAop {
        public static void main(String[] args) {
            ApplicationContext ac=new ClassPathXmlApplicationContext("spring-1.xml");
            //获取业务对象
            //IUserService userService =  ac.getBean(IUserService.class);
            IUserService userService=(IUserService) ac.getBean("userService");
            userService.list();
            userService.update(new User());
            userService.insert(new User());
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    ③xml配置方式实现AOP

    知识点:

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    代码整体布局:

    在这里插入图片描述

    运行结果:

    在这里插入图片描述

    添加代码部分(以②为基础):

    TestAop2 :

    package com.test2;
    
    import com.entity.User;
    import com.service.IUserService;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class TestAop2 {
        public static void main(String[] args) {
            ApplicationContext ac=new ClassPathXmlApplicationContext("spring-2.xml");
            //获取业务对象
            IUserService userService =  ac.getBean(IUserService.class);
            userService.list();
            userService.update(new User());
            userService.insert(new User());
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    spring-2.xml:

    
    
    
        
        
        
    
        
        
            
            
            
            
                
                
                
                
                
                
                
            
        
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    事务回滚

    代码/数据库整体布局:

    在这里插入图片描述
    在这里插入图片描述

    在这里插入图片描述

    运行结果:

    在这里插入图片描述

    test1:

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    test2:

    在这里插入图片描述
    在这里插入图片描述

    test3:

    在这里插入图片描述
    在这里插入图片描述

    test4:

    在这里插入图片描述
    在这里插入图片描述

    代码部分 :

    数据库:

    #创建数据库
    create database 70915_db
    default character set utf8mb4 #设置字符集
    default collate utf8mb4_general_ci #设置排序规则
    
    • 1
    • 2
    • 3
    • 4

    数据表:

    #创建部门表
    create table tbl_dept
    ( 
    	dept_id int primary key auto_increment comment '部门编号', 
    	dept_name varchar(20) comment '部门名字', 
    	dept_desc varchar(200) comment '描述' 
    );
    
    select * from tbl_dept;
    
    insert into tbl_dept
    (dept_name,dept_desc)
    select '财务部','管钱的' union
    select '后勤部','搞物业' union
    select 'IT部门','编程序';
    
    
    #创建角色表
    create table tbl_role
    ( 
    	role_id int primary key auto_increment comment '员工编号', 
    	role_name varchar(20) comment '员工姓名', 
    	role_desc varchar(200) comment '描述' 
    );
    
    drop table tbl_role;
    select * from tbl_role;
    select * from tbl_dept;
    
    insert into tbl_role
    (role_name,role_desc)
    select '张三','描述1' union
    select '李四','描述2' union
    select '王五','描述3' union
    select '赵六','描述4';
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    pom.xml :

    
    
    
      4.0.0
    
      org.example
      Aop_0914_SecondStage
      1.0-SNAPSHOT
      war
    
      Aop_0914_SecondStage Maven Webapp
      
      http://www.example.com
    
      
        8
        8
        4.0.1
        2.2
        1.2
        5.3.14
        1.4
        3.4.6
        1.3.3
        8.0.11
        1.2.78
      
      
        
        
          junit
          junit
          4.12
        
        
          javax.servlet
          javax.servlet-api
          ${servlet.version}
          
          provided
        
        
          javax.servlet.jsp
          jsp-api
          ${jsp.version}
          
          provided
        
        
          jstl
          jstl
          ${jstl.version}
        
        
        
          org.springframework
          spring-webmvc
          ${spring.version}
        
        
          org.springframework
          spring-jdbc
          ${spring.version}
        
        
        
          commons-dbcp
          commons-dbcp
          ${commons-dbcp.version}
        
        
        
          org.mybatis
          mybatis
          ${mybatis.version}
        
        
        
          org.mybatis
          mybatis-spring
          ${mybatis-spring.version}
        
        
        
          com.github.pagehelper
          pagehelper
          5.1.0
        
        
        
          com.github.abel533
          mapper
          3.0.1
        
        
        
          mysql
          mysql-connector-java
          ${mysql-connector-java.version}
        
        
        
          com.alibaba
          fastjson
          ${fastjson.version}
        
        
        
          org.projectlombok
          lombok
          1.18.24
        
        
          com.fasterxml.jackson.core
          jackson-databind
          2.11.2
        
        
          com.sun.mail
          javax.mail
          1.5.6
        
        
          org.slf4j
          slf4j-api
          1.7.30
        
        
          com.github.xuwei-k
          html2image
          0.1.0
        
        
          org.aspectj
          aspectjweaver
          1.9.8
        
    
    
    
    
    
        
          com.fasterxml.jackson.core
          jackson-databind
          2.13.2.2
        
        
        
          org.logback-extensions
          logback-ext-spring
          0.1.4
        
        
        
          javax.validation
          validation-api
          2.0.1.Final
        
        
          org.hibernate
          hibernate-validator
          6.1.0.Final
        
    
        
        
        
          org.apache.poi
          poi
          3.17
        
        
        
          org.apache.poi
          poi-ooxml
          3.17
        
        
        
          org.apache.poi
          poi-ooxml-schemas
          3.17
        
        
        
          com.sun.mail
          jakarta.mail
          1.6.7
        
    
      
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195

    db.properties:

    jdbc.driver=com.mysql.cj.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/70915_db?useSSL=false&serverTimezone=UTC&characterEncoding=utf8&allowPublicKeyRetrieval=true
    jdbc.username=root
    jdbc.password=123456
    
    • 1
    • 2
    • 3
    • 4

    logback.xml:

    
    
         
        
        
        
            
                
                %d{HH:mm:ss.SSS} [%thread] > %green(%-5level) %cyan(%logger{35}) - %m%n
            
        
    
        
        
            
    			
    			/aaalogs/%d{yyyy-MM-dd}.aaa.log.zip
                
                365
            
            
                %d{yyyy-MM-dd HH:mm:ss.SSS} > %-5level %X{X-B3-TraceId:-} ${springAppName:-} %logger{35} %m%n
            
        
        
        
            
            
        
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    mybatis-config.xml:

    
    
    
        
            
            
            
            
        
        
        
            
            
    
                
                
                
                
                
                
                
            
            
                
                
                
                
            
        
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    spring-mvc.xml:

    
    
        
        
        
        
        
        
            
            
            
            
        
    
        
        
            
            
                
                
                
                
                
                
                
                
                
                
                
            
    
        
    
        
        
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    spring-mybatis.xml:

    
    
    
        
        
    
        
        
        
        
            
            
            
            
            
        
    
        
        
            
            
            
            
            
            
            
            
    
        
        
        
            
            
            
            
        
    
        
        
            
        
        
        
            
                
                
            
        
    
        
        
            
            
            
            
        
        
        
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72

    spring-mybatis2.xml:

    
    
    
        
        
    
        
        
        
        
            
            
            
            
            
        
    
        
        
            
            
            
            
            
            
            
            
    
        
        
        
            
            
            
            
        
    
        
        
            
        
    
        
        
    
        
        
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61

    Dept :

    package com.entity;
    
    import lombok.Data;
    
    @Data
    public class Dept {
        private Integer deptId;
        private String deptName;
        private String deptDesc;
    
        public Dept(){}
    
        public Dept(String deptName,String deptDesc){
            this.deptName=deptName;
            this.deptDesc=deptDesc;
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    Role:

    package com.entity;
    
    import lombok.Data;
    
    @Data
    public class Role {
        private Integer roleId;
        private String roleName;
        private String roleDesc;
    
        public Role(){}
    
        public Role(String roleName,String roleDesc){
            this.roleName=roleName;
            this.roleDesc=roleDesc;
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    DeptMapper :

    package com.mapper;
    
    import com.entity.Dept;
    import org.apache.ibatis.annotations.Insert;
    
    public interface DeptMapper {
        @Insert("insert into tbl_dept(dept_name,dept_desc)values(#{deptName},#{deptDesc})")
        Integer insert(Dept dept);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    RoleMapper :

    package com.mapper;
    
    import com.entity.Role;
    import org.apache.ibatis.annotations.Insert;
    
    public interface RoleMapper {
        @Insert("insert into tbl_role(role_name,role_desc) values(#{roleName},#{roleDesc})")
        Integer insert(Role role);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    IDeptService :

    package com.service;
    
    
    import com.entity.Dept;
    
    public interface IDeptService {
        Integer insert(Dept dept);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    IRoleService :

    package com.service;
    
    import com.entity.Role;
    
    public interface IRoleService {
        Integer insert(Role role);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    DeptServiceImpl :
    package com.service.impl;
    
    import com.entity.Dept;
    import com.mapper.DeptMapper;
    import com.service.IDeptService;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Propagation;
    import org.springframework.transaction.annotation.Transactional;
    
    import javax.annotation.Resource;
    
    @Service
    //REQUIRES_NEW:对于当前业务总是创建新的事务,不受外部事务的影响。
    //             外部事务回滚不会影响当前事务。当前事务回滚同时会回滚外部事务
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRES_NEW) //针对当前业务类进行事务管理
    public class DeptServiceImpl implements IDeptService {
        @Resource
        private DeptMapper deptMapper;
        @Override
        public Integer insert(Dept dept) {
            Integer count = deptMapper.insert(dept);
            System.out.println(1/0);
            return count;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    RoleServiceImpl :

    package com.service.impl;
    
    import com.entity.Dept;
    import com.entity.Role;
    import com.mapper.RoleMapper;
    import com.service.IDeptService;
    import com.service.IRoleService;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Propagation;
    import org.springframework.transaction.annotation.Transactional;
    
    import javax.annotation.Resource;
    
    
    @Service
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public class RoleServiceImpl implements IRoleService {
        @Resource
        private RoleMapper roleMapper;
        @Resource
        private IDeptService deptService;
        @Override
        public Integer insert(Role role) {
            System.out.println("插入角色");
            Integer count = roleMapper.insert(role);
            System.out.println("插入部门");
            deptService.insert(new Dept("d12","d12"));
            System.out.println("插入完成");
    
            return count;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    Test1 :

    package com.test3;
    
    import com.entity.Dept;
    import com.entity.Role;
    import com.service.IDeptService;
    import com.service.IRoleService;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Test1 {
        public static void main(String[] args) {
            ApplicationContext ac = new ClassPathXmlApplicationContext("spring-mybatis.xml");
            IRoleService roleService = ac.getBean(IRoleService.class);
            roleService.insert(new Role("r1","r1"));
    
            IDeptService deptService = ac.getBean(IDeptService.class);
            deptService.insert(new Dept("d1","d1"));
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    Test2 :

    package com.test3;
    
    import com.entity.Dept;
    import com.service.IDeptService;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Test2 {
        public static void main(String[] args) {
            ApplicationContext ac = new ClassPathXmlApplicationContext("spring-mybatis.xml");
            IDeptService deptService = ac.getBean(IDeptService.class);
            //此时已经针对业务方法配置spring事务,我们要观察当业务方法出错时,业务处理是否回滚
            deptService.insert(new Dept("d3","d3"));
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    Test3 :

    package com.test3;
    
    import com.entity.Dept;
    import com.service.IDeptService;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Test3 {
        public static void main(String[] args) {
            ApplicationContext ac = new ClassPathXmlApplicationContext("spring-mybatis2.xml");
            IDeptService deptService = ac.getBean(IDeptService.class);
            //此时已经针对业务方法配置spring事务,我们要观察当业务方法出错时,业务处理是否回滚
            deptService.insert(new Dept("d5","d5"));
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    Test4 :

    package com.test3;
    
    import com.entity.Role;
    import com.service.IRoleService;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    
    public class Test4 {
        public static void main(String[] args) {
            ApplicationContext ac = new ClassPathXmlApplicationContext("spring-mybatis2.xml");
            //事务传播特性
            IRoleService ro = ac.getBean(IRoleService.class);
            ro.insert(new Role("r12","r12"));
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    异常:

    ①SLF4J: Class path contains multiple SLF4J bindings.

    参考:SLF4J: Class path contains multiple SLF4J bindings.错误解决https://blog.csdn.net/weixin_42181264/article/details/112168270
    以上说明这是maven问题,两者冲突,找到目标所在文件,删除其中一个就好。
    在这里插入图片描述

    解决:

    在这里插入图片描述

  • 相关阅读:
    Alibaba Nacos注册中心实战
    CSA研讨会|聚焦云原生安全,探讨技术与应用策略
    Spring源码之Mvc的启动挂载流程
    [AIGC] Maven的生命周期和Spring Boot的结合使用
    Unity布料系统_Cloth组件(包含动态调用相关)
    快速编写CSDN博客 小技巧
    Redis数据持久化
    【JavaSE】类和对象(下篇)
    vue的由来、vue教程和M-V-VM架构思想、vue的使用、nodejs
    BIOS < UEFI
  • 原文地址:https://blog.csdn.net/Liu_wen_wen/article/details/126876541