第5章Spring 事务(测试)--在这个笔记中记录的是没有添加事务,数据库返回的效果。
1. 首先搞两张表,商品表和订单表
举例:购买商品 trans_sale 项目 本例要实现购买商品,模拟用户下订单,向订单表添加销售记录,从商品表减少库存。
实现步骤:
-
Step0:创建数据库表
sale 销售表
goods 商品表
-
Step1: maven 依赖 pom.xml
- <dependency>
- <groupId>junitgroupId>
- <artifactId>junitartifactId>
- <version>4.11version>
- <scope>testscope>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-contextartifactId>
- <version>5.2.5.RELEASEversion>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-txartifactId>
- <version>5.2.5.RELEASEversion>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-jdbcartifactId>
- <version>5.2.5.RELEASEversion>
- dependency>
- <dependency>
- <groupId>org.mybatisgroupId>
- <artifactId>mybatisartifactId>
- <version>3.5.1version>
- dependency>
- <dependency>
- <groupId>org.mybatisgroupId>
- <artifactId>mybatis-springartifactId>
- <version>1.3.1version>
- dependency>
- <dependency>
- <groupId>mysqlgroupId>
- <artifactId>mysql-connector-javaartifactId>
- <version>5.1.9version>
- dependency>
- <dependency>
- <groupId>com.alibabagroupId>
- <artifactId>druidartifactId>
- <version>1.1.12version>
- dependency>
- 插件
- <build>
- <resources>
- <resource>
- <directory>src/main/javadirectory>
- <includes>
- <include>**/*.propertiesinclude>
- <include>**/*.xmlinclude>
- includes>
- <filtering>falsefiltering>
- resource>
- resources>
- <plugins>
- <plugin>
- <artifactId>maven-compiler-pluginartifactId>
- <version>3.1version>
- <configuration>
- <source>1.8source>
- <target>1.8target>
- configuration>
- plugin>
- plugins>
- build>
项目的结构
-
Step2:创建实体类
创建实体类 Sale 与 Goods
-
Step3:定义 dao 接口
-
Step4:定义 dao 接口对应的 sql 映射文件
-
Step5:定义异常类
定义 service 层可能会抛出的异常类 NotEnoughException
- package com.bjpowernode.excep;
- //自定义的运行时异常
- public class NotEnoughException extends RuntimeException {
- public NotEnoughException() {
- super();
- }
- public NotEnoughException(String message) {
- super(message);
- }
- }
-
Step6:定义 Service 接口
- package com.bjpowernode.service;
- public interface BuyGoodsService {
- //购买商品的方法, goodsId:购买商品的编号, nums:购买的数量
- void buy(Integer goodsId,Integer nums);
- }
-
Step7:定义 service 的实现类
- package com.bjpowernode.service.impl;
- import com.bjpowernode.dao.GoodsDao;
- import com.bjpowernode.dao.SaleDao;
- import com.bjpowernode.domain.Goods;
- import com.bjpowernode.domain.Sale;
- import com.bjpowernode.excep.NotEnoughException;
- import com.bjpowernode.service.BuyGoodsService;
- public class BuyGoodsServiceImpl implements BuyGoodsService {
- private SaleDao saleDao;
- private GoodsDao goodsDao;
- @Override
- public void buy(Integer goodsId, Integer nums) {
- System.out.println("=====buy方法的开始====");
- //记录销售信息,向sale表添加记录
- Sale sale = new Sale();
- sale.setGid(goodsId);
- sale.setNums(nums);
- saleDao.insertSale(sale);
- //更新库存
- Goods goods = goodsDao.selectGoods(goodsId);
- if( goods == null){
- //商品不存在
- throw new NullPointerException("编号是:"+goodsId+",商品不存在");
- } else if( goods.getAmount() < nums){
- //商品库存不足
- throw new NotEnoughException("编号是:"+goodsId+",商品库存不足");
- }
- //修改库存了
- Goods buyGoods = new Goods();
- buyGoods.setId( goodsId);
- buyGoods.setAmount(nums);
- goodsDao.updateGoods(buyGoods);
- System.out.println("=====buy方法的完成====");
- }
- public void setSaleDao(SaleDao saleDao) {
- this.saleDao = saleDao;
- }
- public void setGoodsDao(GoodsDao goodsDao) {
- this.goodsDao = goodsDao;
- }
- }
-
Step8:修改 Spring 配置文件内容
- "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"
- 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">
- <context:property-placeholder location="classpath:jdbc.properties" />
- <bean id="myDataSource" class="com.alibaba.druid.pool.DruidDataSource"
- init-method="init" destroy-method="close">
- <property name="url" value="${jdbc.url}" />
- <property name="username" value="${jdbc.username}"/>
- <property name="password" value="${jdbc.passwd}" />
- <property name="maxActive" value="${jdbc.max}" />
- bean>
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="dataSource" ref="myDataSource" />
- <property name="configLocation" value="classpath:mybatis.xml" />
- bean>
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
- <property name="basePackage" value="com.bjpowernode.dao"/>
- bean>
- <bean id="buyService" class="com.bjpowernode.service.impl.BuyGoodsServiceImpl">
- <property name="goodsDao" ref="goodsDao" />
- <property name="saleDao" ref="saleDao" />
- bean>
- beans>
-
其中mybatis的主配置文件和jdbc连接的配置文件如下:
-
Step9:定义测试类
- package com.bjpowernode;
- import com.bjpowernode.service.BuyGoodsService;
- import org.junit.Test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class MyTest {
- @Test
- public void test01(){
- String config="applicationContext.xml";
- ApplicationContext ctx = new ClassPathXmlApplicationContext(config);
- //从容器获取service
- BuyGoodsService service = (BuyGoodsService) ctx.getBean("buyService");
- //调用方法
- service.buy(1001,200);
- }
- }
-
当正常执行时候。
-
商品不存在时
-
商品数量不足时候