Spring事务案例准备

该博客介绍了Spring事务管理的一个案例准备工作,包括导入一致版本的jar包,创建数据库和表,设计DAO接口和实现,Service接口和实现,以及配置IOC文件,但未涉及事务处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

案例准备:
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>
  1. 创建数据库
    在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');
  1. 创建dao接口
public interface ICardInfoDao {
    /**
     * 加钱方法
     * @param id
     * @param money
     */
     void increaseMoney(int id , float money);

    /**
     * 减钱方法
     * @param id
     * @param money
     */
     void decreaseMoney(int id , float money);
}
  1. 创建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);
    }
}
  1. 创建service接口
public interface ICardInfoService {

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

}
  1. 创建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);
    }
}
  1. 添加ioc配置文件
    先不处理事务问题

注意xmlns的导入

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
	http://www.springframework.org/schema/tx
	http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/context
	http://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);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值