您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
06-Spring的事务控制-基于xml的声明式事务控制-转账业务环境搭建
发布时间:2025-01-20 19:56:21编辑:雪饮阅读()
-
这次我选择基于之前的项目新增module来实践。
同样基于maven的webapp模板创建
org.apache.maven.archetypes:maven-archetype-webapp
创建后
pom.xml里面的依赖和之前其实是没有什么变化的。
主要的几个重要依赖如
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.32</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
然后在新module中src/main下面建立java然后建立相关package
然后准备Accout的dao层及dao层实现
package sp22.dao;
public interface AccountDao {
public void out(String outMan,double money);
public void in(String inMan,double money);
}
AccountDao的实现
package sp22.dao.impl;
import org.springframework.jdbc.core.JdbcTemplate;
import sp22.dao.AccountDao;
public class AccountDaoImpl implements AccountDao {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public void out(String outMan, double money) {
jdbcTemplate.update("update account set money=money-? where name=?",money,outMan);
}
public void in(String inMan, double money) {
jdbcTemplate.update("update account set money=money+? where name=?",money,inMan);
}
}
以及service接口
package sp22.service;
public interface AccountService {
public void transfer(String outMan,String inMan,double money);
}
和service接口的实现
package sp22.service.impl;
import sp22.dao.AccountDao;
import sp22.service.AccountService;
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
public void transfer(String outMan, String inMan, double money) {
accountDao.out(outMan,money);
accountDao.in(inMan,money);
}
}
然后建立控制器来调用(暂时先不注解控制器)
package sp22.controller;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import sp22.service.AccountService;
public class AccountController {
public static void main(String[] args) {
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
AccountService accountService = app.getBean(AccountService.class);
accountService.transfer("tom","lucy",500);
}
}
当然service里面的转账业务的转入与转出逻辑之间需要造成一个以外,若最终执行的时候数据库里面的结果正好是出现了一个逻辑操作成功,一个逻辑没有成功,则表示默认情况下两个数据库操作都是独立的事务且自动提交(jdbcTemplate情况下)
public void transfer(String outMan, String inMan, double money) {
accountDao.out(outMan,money);
int i=1/0;
accountDao.in(inMan,money);
}
然后准备数据表及数据
CREATE TABLE `account` (
`name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '',
`money` int(10) NULL DEFAULT NULL,
`id` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 7 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of account
-- ----------------------------
INSERT INTO `account` VALUES ('kasumi', NULL, 4);
INSERT INTO `account` VALUES ('tom', 1500, 5);
INSERT INTO `account` VALUES ('lucy', 1500, 6);
然后在src/main下建立resources,并在resources下面建立Spring主配置文件
这里进行配置数据源和上面的service以及dao层的自动注入,如applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
">
<!--加载properties-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--配置数据源对象-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--配置JdbcTemplate对象-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--配置AccountDao-->
<bean id="accountDao" class="sp22.dao.impl.AccountDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
<!--配置AccountService-->
<bean id="accountService" class="sp22.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"/>
</bean>
</beans>
所以同目录下还需要建立如jdbc.properties文件:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.217.132/mybatis
jdbc.username=root
jdbc.password=1448169a3cb137d7
最后运行上面的main方法进行测试就能达到tom的money减少了,而lucy的money却没有增加的转账没有使用合理使用事务处理造成的经典错误。
关键字词:事务,Spring
相关文章
- 08-xml方式实现aop-快速入门(spring)
- 04-SpringMVC异常处理-自定义异常处理器
- 03-SpringMVC异常处理-简单异常处理器(defaultErrorVi
- 01-SpringMVC异常处理-异常处理的思路(异常何时抛出?何
- 09-SpringMVC拦截器-用户登录权限控制代码实现3
- 08-SpringMVC拦截器-用户登录权限控制代码实现2(exclu
- 07-SpringMVC拦截器-用户登录权限控制代码实现1
- 04-SpringMVC拦截器-快速入门详解(多拦截器与先进后出
- 03-SpringMVC拦截器-快速入门(解决System.out.println
- 18-Spring练习-删除用户操作