瀏覽代碼

基于注解的事务控制

zhang 2 年之前
父節點
當前提交
61478acef3

+ 23 - 0
spring_aop/src/main/java/com/TestTx.java

@@ -0,0 +1,23 @@
1
+package com;
2
+
3
+
4
+import com.transaction_anno.service.AccountService;
5
+import org.junit.Test;
6
+import org.junit.runner.RunWith;
7
+import org.springframework.beans.factory.annotation.Autowired;
8
+import org.springframework.test.context.ContextConfiguration;
9
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
10
+
11
+@RunWith(SpringJUnit4ClassRunner.class)
12
+@ContextConfiguration("classpath:context.xml")
13
+public class TestTx {
14
+
15
+    @Autowired
16
+    AccountService accountService;
17
+
18
+    @Test
19
+    public void transformAnno() {
20
+        // 交易测试
21
+        accountService.transfer(5,4,3.0);
22
+    }
23
+}

+ 2 - 2
spring_aop/src/main/java/com/config/JdbcConfig.java

@@ -9,8 +9,8 @@ import org.springframework.jdbc.core.JdbcTemplate;
9 9
 
10 10
 import javax.sql.DataSource;
11 11
 
12
-@Configuration
13
-@PropertySource("classpath:jdbc.properties")
12
+//@Configuration
13
+//@PropertySource("classpath:jdbc.properties")
14 14
 public class JdbcConfig {
15 15
     // 写一个dataSource
16 16
     @Value("${jdbc.url}")

+ 9 - 0
spring_aop/src/main/java/com/transaction_anno/dao/AccountDao.java

@@ -0,0 +1,9 @@
1
+package com.transaction_anno.dao;
2
+
3
+
4
+import com.transaction_anno.entity.AccountEntity;
5
+
6
+public interface AccountDao {
7
+     int saveAccount(AccountEntity accountEntity);
8
+     int incomeAccount(AccountEntity accountEntity, String type);
9
+}

+ 36 - 0
spring_aop/src/main/java/com/transaction_anno/dao/impl/AccountDaoImpl.java

@@ -0,0 +1,36 @@
1
+package com.transaction_anno.dao.impl;
2
+
3
+import com.transaction_anno.dao.AccountDao;
4
+import com.transaction_anno.entity.AccountEntity;
5
+import org.springframework.beans.factory.annotation.Autowired;
6
+import org.springframework.jdbc.core.JdbcTemplate;
7
+import org.springframework.stereotype.Repository;
8
+
9
+@Repository("anno_AccountDao")
10
+public class AccountDaoImpl implements AccountDao {
11
+    @Autowired
12
+    private JdbcTemplate jdbcTemplate;
13
+    @Override
14
+    public int saveAccount(AccountEntity accountEntity) {
15
+        String addsql = "insert into account(user_name,balance) values(?,?)";
16
+        Object[] args = {accountEntity.getUserName(),accountEntity.getBalance()};
17
+        return jdbcTemplate.update(addsql,args);
18
+        //返回值update代表添加了几行
19
+    }
20
+
21
+    /**
22
+     * 账户转入或转出
23
+     * @param accountEntity
24
+     * @param type   in 转入 out 转出
25
+     * @return
26
+     */
27
+    @Override
28
+    public int incomeAccount(AccountEntity accountEntity, String type) {
29
+        String updateSql = "update account set balance=balance"
30
+                + (type == "in" ? "+" :"-")
31
+                + " ? where id =?";
32
+        Object[] args = {accountEntity.getBalance(),accountEntity.getId()};
33
+        return jdbcTemplate.update(updateSql,args);
34
+    }
35
+
36
+}

+ 15 - 0
spring_aop/src/main/java/com/transaction_anno/entity/AccountEntity.java

@@ -0,0 +1,15 @@
1
+package com.transaction_anno.entity;
2
+
3
+import lombok.Data;
4
+import lombok.experimental.Accessors;
5
+
6
+import java.util.Date;
7
+
8
+@Data
9
+@Accessors(chain = true)
10
+public class AccountEntity {
11
+    private Integer id;
12
+    private String userName;
13
+    private Double balance;
14
+    private Date ctime;
15
+}

+ 7 - 0
spring_aop/src/main/java/com/transaction_anno/service/AccountService.java

@@ -0,0 +1,7 @@
1
+package com.transaction_anno.service;
2
+
3
+public interface AccountService {
4
+      void initAccount();
5
+     // 模拟转账操作
6
+      boolean transfer(Integer fromId,Integer toId,Double balance);
7
+}

+ 39 - 0
spring_aop/src/main/java/com/transaction_anno/service/impl/AccountServiceImpl.java

@@ -0,0 +1,39 @@
1
+package com.transaction_anno.service.impl;
2
+
3
+import com.transaction_anno.dao.AccountDao;
4
+import com.transaction_anno.entity.AccountEntity;
5
+import com.transaction_anno.service.AccountService;
6
+import org.springframework.beans.factory.annotation.Autowired;
7
+import org.springframework.stereotype.Service;
8
+import org.springframework.transaction.annotation.Propagation;
9
+import org.springframework.transaction.annotation.Transactional;
10
+
11
+@Service("account_anno")
12
+public class AccountServiceImpl implements AccountService {
13
+    @Autowired
14
+    AccountDao accountDao;
15
+
16
+    @Override
17
+    public void initAccount() {
18
+        AccountEntity accountEntity = new AccountEntity();
19
+        accountEntity.setUserName("李四");
20
+        accountEntity.setBalance(300.0);
21
+        int i = accountDao.saveAccount(accountEntity);
22
+    }
23
+    @Transactional(rollbackFor = Exception.class)
24
+    @Override
25
+    public boolean transfer(Integer fromId, Integer toId, Double balance) {
26
+        // 转账操作
27
+        boolean b;
28
+        AccountEntity fromEntity = new AccountEntity();
29
+        AccountEntity toEntity = new AccountEntity();
30
+        fromEntity.setId(fromId).setBalance(balance);
31
+        toEntity.setId(toId).setBalance(balance);
32
+        // 开启事务
33
+        accountDao.incomeAccount(toEntity,"to");
34
+        int i = 1/0;
35
+        accountDao.incomeAccount(fromEntity,"in");
36
+        // 提交事务
37
+        return false;
38
+    }
39
+}

+ 11 - 9
spring_aop/src/main/resources/transaction_aop.xml

@@ -34,13 +34,15 @@
34 34
     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
35 35
         <property name="dataSource" ref="dataSource"></property>
36 36
     </bean>
37
-<!--    <tx:advice id="interceptor" transaction-manager="transactionManager">-->
38
-<!--        <tx:attributes>-->
39
-<!--            <tx:method name="transfer" rollback-for="java.lang.Exception" timeout="-1"/>-->
40
-<!--        </tx:attributes>-->
41
-<!--    </tx:advice>-->
42
-<!--    <aop:config>-->
43
-<!--        <aop:advisor advice-ref="interceptor" pointcut="execution(* com.transaction_.service.impl.AccountServiceImpl.*(..))"></aop:advisor>-->
44
-<!--    </aop:config>-->
45
-
37
+    <tx:advice id="interceptor" transaction-manager="transactionManager">
38
+        <tx:attributes>
39
+            <tx:method name="transfer*" isolation="DEFAULT" rollback-for="java.lang.Exception" timeout="-1"/>
40
+        </tx:attributes>
41
+    </tx:advice>
42
+    <aop:config>
43
+        <aop:pointcut id="txPointCut" expression="execution(* com.transaction_.service.impl.AccountServiceImpl.*(..))"/>
44
+        <aop:advisor advice-ref="interceptor"
45
+                     pointcut-ref="txPointCut"/>
46
+    </aop:config>
47
+    <tx:annotation-driven />
46 48
 </beans>