Spring事务案例有哪些具体应用场景?

2026-05-28 01:341阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计1003个文字,预计阅读时间需要5分钟。

Spring事务案例有哪些具体应用场景?

案例准备:

1.导入jar包

注意版本一致性 !-- Spring --> org.springframework spring-context 4.1.3.RELEASE

Spring事务案例有哪些具体应用场景?

案例准备:
1.导入jar包
注意版本一致

<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.3.Release</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.3.Release</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.1.3.Release</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.1.3.Release</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.1.3.RELEASE</version>
</dependency>

<!-- spring jdbc 相关的包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.37</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!-- 配置的 spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.1.3.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.1.3.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.1.3.RELEASE</version>
</dependency>
  • 创建数据库
    在springjdbc库中直接创建表
  • /*Table structure for table `cardinfo` (
    ` */

    CREATE TABLE `cardinfo` (
    `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
    `username` VARCHAR(20) NOT NULL,
    `money` DECIMAL(10,2) DEFAULT NULL,
    PRIMARY KEY (`id`)
    ) ENGINE=INNODB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

    /*Data for the table `cardinfo` */

    INSERT INTO `cardinfo`(`id`,`username`,`money`) VALUES (1,'Helen','1000.00');
    INSERT INTO `cardinfo`(`id`,`username`,`money`) VALUES (2,'Tom','1000.00');
  • 创建dao接口
  • public interface ICardInfoDao {
    /**
    * 加钱方法
    * @param id
    * @param money
    */
    void increaseMoney(int id , float money);

    /**
    * 减钱方法
    * @param id
    * @param money
    */
    void decreaseMoney(int id , float money);
    }
  • 创建dao实现类
  • @Component
    public class CardInfoDao implements ICardInfoDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void increaseMoney(int id , float money) {

    jdbcTemplate.update("update cardinfo set money = money + ? where id = ? ;",money,id);

    }

    public void decreaseMoney(int id , float money) {

    jdbcTemplate.update("update cardinfo set money = money - ? where id = ? ;",money,id);
    }
    }
  • 创建service接口
  • public interface ICardInfoService {

    //转账业务
    void transfer(int from , int to , float money);

    }
  • 创建service实现类
  • @Service
    public class CardInfoService implements ICardInfoService {
    @Autowired
    private ICardInfoDao cardInfoDao;

    public void transfer(Integer from, Integer to, Double money) {

    cardInfoDao.decreaseMoney(from,money);

    cardInfoDao.increaseMoney(to,money);
    }
    }
  • 添加ioc配置文件
    先不处理事务问题
  • 注意xmlns的导入

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="www.springframework.org/schema/beans"
    xmlns:aop="www.springframework.org/schema/aop"
    xmlns:tx="www.springframework.org/schema/tx"
    xmlns:xsi="www.w3.org/2001/XMLSchema-instance"
    xmlns:context="www.springframework.org/schema/context"
    xsi:schemaLocation="www.springframework.org/schema/beans
    www.springframework.org/schema/beans/spring-beans-4.0.xsd
    www.springframework.org/schema/aop
    www.springframework.org/schema/aop/spring-aop-4.0.xsd
    www.springframework.org/schema/tx
    www.springframework.org/schema/tx/spring-tx-4.0.xsd
    www.springframework.org/schema/context
    www.springframework.org/schema/context/spring-context-4.0.xsd">



    <context:component-scan base-package="com.test"/>
    <context:property-placeholder location="db.properties" />

    <bean id="comboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
    p:driverClass="${jdbc.driverClass}"
    p:jdbcUrl="${jdbc.jdbcUrl}"
    p:user="${jdbc.user}"
    p:password="${jdbc.password}"
    />

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="comboPooledDataSource"/>
    </bean>

    </beans>

    测试代码:

    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

    ICardInfoService service = (ICardInfoService) context.getBean("cardInfoService");

    service.transfer(1,2,100.0);

    本文共计1003个文字,预计阅读时间需要5分钟。

    Spring事务案例有哪些具体应用场景?

    案例准备:

    1.导入jar包

    注意版本一致性 !-- Spring --> org.springframework spring-context 4.1.3.RELEASE

    Spring事务案例有哪些具体应用场景?

    案例准备:
    1.导入jar包
    注意版本一致

    <!-- Spring -->
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.1.3.Release</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>4.1.3.Release</version>
    </dependency>

    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>4.1.3.Release</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>4.1.3.Release</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-expression</artifactId>
    <version>4.1.3.RELEASE</version>
    </dependency>

    <!-- spring jdbc 相关的包 -->
    <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.37</version>
    </dependency>
    <dependency>
    <groupId>com.mchange</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.5.2</version>
    </dependency>
    <!-- 配置的 spring-jdbc -->
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>4.1.3.RELEASE</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>4.1.3.RELEASE</version>
    </dependency>

    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aspects</artifactId>
    <version>4.1.3.RELEASE</version>
    </dependency>

    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>4.1.3.RELEASE</version>
    </dependency>
  • 创建数据库
    在springjdbc库中直接创建表
  • /*Table structure for table `cardinfo` (
    ` */

    CREATE TABLE `cardinfo` (
    `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
    `username` VARCHAR(20) NOT NULL,
    `money` DECIMAL(10,2) DEFAULT NULL,
    PRIMARY KEY (`id`)
    ) ENGINE=INNODB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

    /*Data for the table `cardinfo` */

    INSERT INTO `cardinfo`(`id`,`username`,`money`) VALUES (1,'Helen','1000.00');
    INSERT INTO `cardinfo`(`id`,`username`,`money`) VALUES (2,'Tom','1000.00');
  • 创建dao接口
  • public interface ICardInfoDao {
    /**
    * 加钱方法
    * @param id
    * @param money
    */
    void increaseMoney(int id , float money);

    /**
    * 减钱方法
    * @param id
    * @param money
    */
    void decreaseMoney(int id , float money);
    }
  • 创建dao实现类
  • @Component
    public class CardInfoDao implements ICardInfoDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void increaseMoney(int id , float money) {

    jdbcTemplate.update("update cardinfo set money = money + ? where id = ? ;",money,id);

    }

    public void decreaseMoney(int id , float money) {

    jdbcTemplate.update("update cardinfo set money = money - ? where id = ? ;",money,id);
    }
    }
  • 创建service接口
  • public interface ICardInfoService {

    //转账业务
    void transfer(int from , int to , float money);

    }
  • 创建service实现类
  • @Service
    public class CardInfoService implements ICardInfoService {
    @Autowired
    private ICardInfoDao cardInfoDao;

    public void transfer(Integer from, Integer to, Double money) {

    cardInfoDao.decreaseMoney(from,money);

    cardInfoDao.increaseMoney(to,money);
    }
    }
  • 添加ioc配置文件
    先不处理事务问题
  • 注意xmlns的导入

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="www.springframework.org/schema/beans"
    xmlns:aop="www.springframework.org/schema/aop"
    xmlns:tx="www.springframework.org/schema/tx"
    xmlns:xsi="www.w3.org/2001/XMLSchema-instance"
    xmlns:context="www.springframework.org/schema/context"
    xsi:schemaLocation="www.springframework.org/schema/beans
    www.springframework.org/schema/beans/spring-beans-4.0.xsd
    www.springframework.org/schema/aop
    www.springframework.org/schema/aop/spring-aop-4.0.xsd
    www.springframework.org/schema/tx
    www.springframework.org/schema/tx/spring-tx-4.0.xsd
    www.springframework.org/schema/context
    www.springframework.org/schema/context/spring-context-4.0.xsd">



    <context:component-scan base-package="com.test"/>
    <context:property-placeholder location="db.properties" />

    <bean id="comboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
    p:driverClass="${jdbc.driverClass}"
    p:jdbcUrl="${jdbc.jdbcUrl}"
    p:user="${jdbc.user}"
    p:password="${jdbc.password}"
    />

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="comboPooledDataSource"/>
    </bean>

    </beans>

    测试代码:

    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

    ICardInfoService service = (ICardInfoService) context.getBean("cardInfoService");

    service.transfer(1,2,100.0);