Browse Source

基于xml配置事务

zhang 2 years ago
parent
commit
2c3c7c6e32

+ 5 - 1
spring_aop/src/main/java/com/AopTest.java

@@ -27,5 +27,9 @@ public class AopTest {
27
     public void testInitAccount() {
27
     public void testInitAccount() {
28
         accountService.initAccount();
28
         accountService.initAccount();
29
     }
29
     }
30
-
30
+    @Test
31
+    public void transform() {
32
+        // 交易测试
33
+        accountService.transfer(5,4,1.0);
34
+    }
31
 }
35
 }

+ 2 - 2
spring_aop/src/main/java/com/transaction_/dao/AccountDao.java

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

+ 21 - 1
spring_aop/src/main/java/com/transaction_/dao/impl/AccountDaoImpl.java

@@ -8,8 +8,12 @@ import org.springframework.stereotype.Repository;
8
 
8
 
9
 @Repository
9
 @Repository
10
 public class AccountDaoImpl implements AccountDao {
10
 public class AccountDaoImpl implements AccountDao {
11
-    @Autowired
12
     private JdbcTemplate jdbcTemplate;
11
     private JdbcTemplate jdbcTemplate;
12
+
13
+    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
14
+        this.jdbcTemplate = jdbcTemplate;
15
+    }
16
+
13
     @Override
17
     @Override
14
     public int saveAccount(AccountEntity accountEntity) {
18
     public int saveAccount(AccountEntity accountEntity) {
15
         String addsql = "insert into account(user_name,balance) values(?,?)";
19
         String addsql = "insert into account(user_name,balance) values(?,?)";
@@ -17,4 +21,20 @@ public class AccountDaoImpl implements AccountDao {
17
         return jdbcTemplate.update(addsql,args);
21
         return jdbcTemplate.update(addsql,args);
18
         //返回值update代表添加了几行
22
         //返回值update代表添加了几行
19
     }
23
     }
24
+
25
+    /**
26
+     * 账户转入或转出
27
+     * @param accountEntity
28
+     * @param type   in 转入 out 转出
29
+     * @return
30
+     */
31
+    @Override
32
+    public int incomeAccount(AccountEntity accountEntity,String type) {
33
+        String updateSql = "update account set balance=balance"
34
+                + (type == "in" ? "+" :"-")
35
+                + " ? where id =?";
36
+        Object[] args = {accountEntity.getBalance(),accountEntity.getId()};
37
+        return jdbcTemplate.update(updateSql,args);
38
+    }
39
+
20
 }
40
 }

+ 3 - 1
spring_aop/src/main/java/com/transaction_/entity/AccountEntity.java

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

+ 2 - 0
spring_aop/src/main/java/com/transaction_/service/AccountService.java

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

+ 22 - 8
spring_aop/src/main/java/com/transaction_/service/impl/AccountServiceImpl.java

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

+ 2 - 3
spring_aop/src/main/resources/context.xml

@@ -12,6 +12,7 @@
12
        https://www.springframework.org/schema/context/spring-context.xsd
12
        https://www.springframework.org/schema/context/spring-context.xsd
13
 
13
 
14
     ">
14
     ">
15
+    <import resource="classpath:transaction_aop.xml"></import>
15
 <!--       1、配置目标对象 2、配置切面对象 3、配置织入告诉Spring 哪些方法(切点)需要增强 -->
16
 <!--       1、配置目标对象 2、配置切面对象 3、配置织入告诉Spring 哪些方法(切点)需要增强 -->
16
     <bean id="target" class="com.aop.TargetImpl"></bean>
17
     <bean id="target" class="com.aop.TargetImpl"></bean>
17
     <bean id="myaspect" class="com.aop.MyAspect">
18
     <bean id="myaspect" class="com.aop.MyAspect">
@@ -32,9 +33,7 @@
32
             <aop:after method="after" pointcut-ref="save2" />
33
             <aop:after method="after" pointcut-ref="save2" />
33
         </aop:aspect>
34
         </aop:aspect>
34
     </aop:config>
35
     </aop:config>
35
-    <context:component-scan base-package="com.anno"></context:component-scan>
36
-    <context:component-scan base-package="com.config"></context:component-scan>
37
-    <context:component-scan base-package="com.transaction_"></context:component-scan>
36
+    <context:component-scan base-package="com"></context:component-scan>
38
 <!--    aop自动代理-->
37
 <!--    aop自动代理-->
39
     <aop:aspectj-autoproxy />
38
     <aop:aspectj-autoproxy />
40
 </beans>
39
 </beans>

+ 32 - 0
spring_aop/src/main/resources/transaction_aop.xml

@@ -0,0 +1,32 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<beans xmlns="http://www.springframework.org/schema/beans"
3
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4
+       xmlns:tx="http://www.springframework.org/schema/tx"
5
+       xmlns:context="http://www.springframework.org/schema/context"
6
+       xsi:schemaLocation="http://www.springframework.org/schema/beans
7
+       http://www.springframework.org/schema/beans/spring-beans.xsd
8
+       http://www.springframework.org/schema/tx
9
+       https://www.springframework.org/schema/tx/spring-tx.xsd
10
+       http://www.springframework.org/schema/context
11
+       https://www.springframework.org/schema/context/spring-context.xsd
12
+">
13
+    <context:property-placeholder location="jdbc.properties"></context:property-placeholder>
14
+    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
15
+        <property name="driverClassName" value="${jdbc.driver}"></property>
16
+        <property name="url" value="${jdbc.url}"></property>
17
+        <property name="username" value="${jdbc.username}"></property>
18
+        <property name="password" value="${jdbc.password}"></property>
19
+    </bean>
20
+    <bean id="jdbcTemp" class="org.springframework.jdbc.core.JdbcTemplate">
21
+        <property name="dataSource" ref="dataSource"></property>
22
+    </bean>
23
+    <bean id="accountDao" class="com.transaction_.dao.impl.AccountDaoImpl">
24
+        <property name="jdbcTemplate" ref="jdbcTemp"></property>
25
+    </bean>
26
+    <bean id="accountService" class="com.transaction_.service.impl.AccountServiceImpl">
27
+        <property name="accountDao" ref="accountDao">
28
+        </property>
29
+    </bean>
30
+
31
+
32
+</beans>