首页 > 编程语言 >Spring 事务源码(一):源码分析示例

Spring 事务源码(一):源码分析示例

时间:2022-12-30 21:01:04浏览次数:47  
标签:xml jdbc 示例 Spring goodsId 源码 public stock

1、新建数据库及表

  商品库存表
1 CREATE TABLE `product_stock` ( 
2     `goods_id` VARCHAR ( 30 ) NOT NULL, 
3     `stock` INT ( 8 ) DEFAULT NULL, 
4     PRIMARY KEY ( `goods_id` ) 
5 ) ENGINE = INNODB DEFAULT CHARSET = utf8;
  初始化数据
1 insert into `product_stock` values ('202212032152', 10000);
2 insert into `product_stock` values ('202212032153', 10000);
3 commit;

2、xml方式

1、ProductDao

 1 package com.snails.transaction.xml.dao;
 2 
 3 import org.springframework.jdbc.core.JdbcTemplate;
 4 
 5 public class ProductDao {
 6 
 7    JdbcTemplate jdbcTemplate;
 8 
 9    public JdbcTemplate getJdbcTemplate() {
10       return jdbcTemplate;
11    }
12 
13    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
14       this.jdbcTemplate = jdbcTemplate;
15    }
16 
17    /**
18     * 更新库存
19     * @param goodsId
20     */
21    public void updateStock(String goodsId) {
22       String sql = "update product_stock set stock=stock-1 where goods_id=?";
23       jdbcTemplate.update(sql,goodsId);
24    }
25 
26 }

2、ProductService

 1 package com.snails.transaction.xml.service;
 2 
 3 import com.snails.transaction.xml.dao.ProductDao;
 4 
 5 public class ProductService {
 6 
 7    ProductDao productDao;
 8 
 9    public ProductDao getProductDao() {
10       return productDao;
11    }
12 
13    public void setProductDao(ProductDao productDao) {
14       this.productDao = productDao;
15    }
16 
17    /**
18     * 某个用户购买指定商品
19     * @param buyerName
20     * @param goodsId
21     */
22    public void sellProduct(String buyerName, String goodsId) {
23       // 更新库存
24       productDao.updateStock(goodsId);
25    }
26 
27 }

3、配置文件

3.1、dbconfig.properties

1 jdbc.username=root
2 jdbc.password=root
3 jdbc.url=jdbc:mysql://localhost:3306/tx
4 jdbc.driverClassName=com.mysql.jdbc.Driver

3.2、tx.xml

 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:context="http://www.springframework.org/schema/context"
 5       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
 6       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
 7    <!--引入外部资源文件-->
 8    <context:property-placeholder location="classpath:dbconfig.properties"></context:property-placeholder>
 9    <!--Druid数据源-->
10    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
11       <property name="username" value="${jdbc.username}"></property>
12       <property name="password" value="${jdbc.password}"></property>
13       <property name="url" value="${jdbc.url}"></property>
14       <property name="driverClassName" value="${jdbc.driverClassName}"></property>
15    </bean>
16    <!--jdbc模板-->
17    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" >
18       <constructor-arg name="dataSource" ref="dataSource"></constructor-arg>
19    </bean>
20    <!--事务管理器-->
21    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
22       <property name="dataSource" ref="dataSource"></property>
23    </bean>
24    <!--商品服务-->
25    <bean id="productDao" class="com.snails.transaction.xml.dao.ProductDao">
26       <property name="jdbcTemplate" ref="jdbcTemplate"></property>
27    </bean>
28    <bean id="productService" class="com.snails.transaction.xml.service.ProductService">
29       <property name="productDao" ref="productDao"></property>
30    </bean>
31    <!--aop切点切面定义-->
32    <aop:config>
33       <aop:pointcut id="txPoint" expression="execution(* com.snails.transaction.xml.*.*.*(..))"/>
34       <aop:advisor advice-ref="myAdvice" pointcut-ref="txPoint"></aop:advisor>
35    </aop:config>
36    <!--Advice-->
37    <tx:advice id="myAdvice" transaction-manager="transactionManager">
38       <tx:attributes>
39          <tx:method name="sellProduct" propagation="REQUIRED" />
40          <tx:method name="updateStock" propagation="REQUIRED" />
41       </tx:attributes>
42    </tx:advice>
43 </beans>

4、TxXmlTest - 测试代码

 1 package com.snails.transaction;
 2 
 3 import com.snails.transaction.xml.service.ProductService;
 4 import org.springframework.context.ApplicationContext;
 5 import org.springframework.context.support.ClassPathXmlApplicationContext;
 6 
 7 import java.sql.SQLException;
 8 
 9 public class TxXmlTest {
10    public static void main(String[] args) throws SQLException {
11       ApplicationContext context = new ClassPathXmlApplicationContext("tx.xml");
12       ProductService productService = context.getBean("productService", ProductService.class);
13       productService.sellProduct("黑风衣","202212032152");
14    }
15 }

5、执行测试代码成功后,查看数据库库存

  至此,xml方式的事务源码入口已完成,可以开始Spring事务源码学习之旅了。

 

 

 

 

 

 

 

 

 

 

 

 

 

标签:xml,jdbc,示例,Spring,goodsId,源码,public,stock
From: https://www.cnblogs.com/RunningSnails/p/17015808.html

相关文章