wuheng 2 yıl önce
ebeveyn
işleme
a61677331d

+ 0 - 4
day03/src/main/java/com/lovecoding/jdbc/Application.java

@@ -1,12 +1,8 @@
 package com.lovecoding.jdbc;
 
-import com.alibaba.druid.pool.DruidDataSource;
-import com.lovecoding.jdbc.dao.Test;
 import org.springframework.context.support.ClassPathXmlApplicationContext;
-import org.springframework.jdbc.core.BeanPropertyRowMapper;
 import org.springframework.jdbc.core.JdbcTemplate;
 
-import java.util.List;
 
 public class Application {
     public static void main(String[] args) {

+ 2 - 15
day03/src/main/java/com/lovecoding/jdbc/dao/Test.java

@@ -1,20 +1,7 @@
 package com.lovecoding.jdbc.dao;
 
-public class Test {
-    private int id;
+public interface Test {
 
-    public int getId() {
-        return id;
-    }
+    void update();
 
-    public void setId(int id) {
-        this.id = id;
-    }
-
-    @Override
-    public String toString() {
-        return "Test{" +
-                "id=" + id +
-                '}';
-    }
 }

+ 21 - 0
day03/src/main/java/com/lovecoding/jdbc/dao/TestImpl.java

@@ -0,0 +1,21 @@
+package com.lovecoding.jdbc.dao;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.stereotype.Repository;
+
+@Repository
+public class TestImpl implements Test {
+
+    @Autowired
+    JdbcTemplate jdbcTemplate;
+
+    @Override
+    public void update() {
+
+        int update = jdbcTemplate.update(
+                "INSERT test values (null)"
+        );
+
+    }
+}

+ 5 - 0
day03/src/main/java/com/lovecoding/jdbc/service/TestService.java

@@ -0,0 +1,5 @@
+package com.lovecoding.jdbc.service;
+
+public interface TestService {
+    void updateTest();
+}

+ 24 - 0
day03/src/main/java/com/lovecoding/jdbc/service/TestServiceImpl.java

@@ -0,0 +1,24 @@
+package com.lovecoding.jdbc.service;
+
+import com.lovecoding.jdbc.dao.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Propagation;
+import org.springframework.transaction.annotation.Transactional;
+
+@Service
+public class TestServiceImpl implements TestService {
+
+    @Autowired
+    Test test;
+
+    @Override
+    @Transactional(
+            propagation = Propagation.REQUIRES_NEW
+    )
+    public void updateTest() {
+        test.update();
+        //int n = 1 / 0;
+        System.out.println( " TestServiceImpl 执行完毕! " );
+    }
+}

+ 29 - 10
day03/src/main/java/com/lovecoding/jdbc/service/UserBuyBookServiceImpl.java

@@ -6,6 +6,7 @@ import com.lovecoding.jdbc.dao.User;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Isolation;
+import org.springframework.transaction.annotation.Propagation;
 import org.springframework.transaction.annotation.Transactional;
 
 @Service
@@ -24,7 +25,8 @@ public class UserBuyBookServiceImpl implements UserBuyBookService{
     private Book book;
     @Autowired
     private User user;
-
+    @Autowired
+    private TestService testService;
     /**
      * isolation
      * 事务隔离级别 是处理多事务, 多个事务交叉的时候隔离事务用的参数
@@ -34,11 +36,7 @@ public class UserBuyBookServiceImpl implements UserBuyBookService{
      *  串行话, 所有事务排队执行
      */
     @Override
-    @Transactional(
-            timeout = 3,
-            noRollbackFor = java.lang.ArithmeticException.class,
-            isolation = Isolation.DEFAULT
-    )
+    @Transactional
     public void buyBook(int uid, int bookid) {
         //查询书的价格
         int price = book.getBook(bookid);
@@ -52,14 +50,35 @@ public class UserBuyBookServiceImpl implements UserBuyBookService{
         /**
          * 我们在公司 开发项目, 会有很多人协作
          * 每个人负责不同的代码, 最终代码混织在一起
-         * 这个时候, 别人的代码就有可能会抛异常, 但是这个异常可能不是正纳闷
-         * 自己的业务逻辑,所以我们可能会自己处理自己的异常
+         * 这个时候, 别人的代码就有可能会抛异常, 但是这个异常可能不是我们关注的业务
+         * 为了不影响到自己的业务逻辑,所以我们可能会自己处理自己的异常
+         * 指定抛出异常的 异常业务类
          */
         //扣减用户钱包
-        user.balance( price, uid );
+        user.balance(price, uid);
+
+        //int n = 1 / 0;
 
-        int n = 1 / 0;
+        try {
+            testService.updateTest();
+        } catch (ArithmeticException e) {
+            System.out.println( e.getMessage() );
+        }
 
         System.out.println( "购买书本成功!" );
+
+        /**
+         * 事务传播, 也叫事务交叉
+         * 这块涉及到 Spring事务管理, 是如何管理事务的、
+         * 事务交叉, 我们开发项目, 经常和多个人协同处理
+         * 如果别人也开了一个事务, 那么我们怎么处理事务呢
+         * @T
+         * buyBook(){
+         *     @T
+         *     bugBook()
+         * }
+         * 默认嵌套事务的时候,事务会互相影响
+         * 一个事务里面嵌套 另一个事务, 实际上是一个事务
+         */
     }
 }

+ 1 - 0
pom.xml

@@ -13,6 +13,7 @@
         <module>demo01</module>
         <module>day02</module>
         <module>day03</module>
+        <module>day04</module>
     </modules>
 
     <properties>