栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

第5章Spring 事务(第一个实例,购买商品)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

第5章Spring 事务(第一个实例,购买商品)

程序举例环境搭建 举例:购买商品 trans_sale 项目 本例要实现购买商品,模拟用户下订单,向订单表添加销售记录,从商品表减少库存。 实现步骤: Step0 :创建数据库表      创建两个数据库表 sale , goods      sale 销售表

       goods 商品表

       sale 表数据

      goods 表数据

项目结构

Step1: maven 依赖 pom.xml




    
        spring3
        com.it
        1.0-SNAPSHOT
    
    4.0.0

    springTransaction

    springTransaction
    
    http://www.example.com

    
        UTF-8
        1.8
        1.8
    

    
        
            junit
            junit
            4.11
            test
        

        
            org.springframework
            spring-context
            5.2.5.RELEASE
        

        
            org.springframework
            spring-tx
            5.2.5.RELEASE
        
        
            org.springframework
            spring-jdbc
            5.2.5.RELEASE
        
        
        
            org.mybatis
            mybatis
            3.5.1
        
        
        
            org.mybatis
            mybatis-spring
            1.3.1
        
        
        
            mysql
            mysql-connector-java
            5.1.9
        
        
        
            com.alibaba
            druid
            1.1.12
        

    


    

        
        
        
            
                src/main/java
                
                    ***.xml
                
                false
            
            
                src/main/resources
                
                    ***.xml
                
                false
            
        


        
        
            
                maven-compiler-plugin
                3.1
                
                    1.8
                    1.8
                
            
        



        
            
                
                
                    maven-clean-plugin
                    3.1.0
                
                
                
                    maven-resources-plugin
                    3.0.2
                
                
                    maven-compiler-plugin
                    3.8.0
                
                
                    maven-surefire-plugin
                    2.22.1
                
                
                    maven-jar-plugin
                    3.0.2
                
                
                    maven-install-plugin
                    2.5.2
                
                
                    maven-deploy-plugin
                    2.8.2
                
                
                
                    maven-site-plugin
                    3.7.1
                
                
                    maven-project-info-reports-plugin
                    3.0.0
                
            
        
    

 Step2:创建实体类

  实体类Sale

package com.it.entity;

public class Sale {
    private Integer id;
    private Integer gid;
    private Integer nums;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getGid() {
        return gid;
    }

    public void setGid(Integer gid) {
        this.gid = gid;
    }

    public Integer getNums() {
        return nums;
    }

    public void setNums(Integer nums) {
        this.nums = nums;
    }

    @Override
    public String toString() {
        return "Sale{" +
                "id=" + id +
                ", gid=" + gid +
                ", nums=" + nums +
                '}';
    }
}

  实体类Goods

package com.it.entity;

public class Goods {
    private Integer id;
    private String name;
    private Integer amount;
    private Float price;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAmount() {
        return amount;
    }

    public void setAmount(Integer amount) {
        this.amount = amount;
    }

    public Float getPrice() {
        return price;
    }

    public void setPrice(Float price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Goods{" +
                "id=" + id +
                ", name='" + name + ''' +
                ", amount=" + amount +
                ", price=" + price +
                '}';
    }
}
Step3 :定义 dao 接口    (1)GoodsDao
package com.it.dao;

import com.it.entity.Goods;

public interface GoodsDao {

//    更新库存
//    goods表示本次用户购买的商品信息,id,购买的数量
    int updateGoods(Goods goods);

//    查询商品信息
    Goods selectGoods(Integer id);
}
(2)SaleDao
package com.it.dao;

import com.it.entity.Sale;

public interface SaleDao {

//    增加销售记录
    int insertSale(Sale sale);


}
Step4 :定义 dao 接口对应的 sql 映射文件       SaleDao.xml




    
insert into sale(gid,nums) values(#{gid},#{nums})
    



      GoodsDao.xml




    
        update goods set amount=amount - #{amount} where id=#{id}
    

    



Step5 :定义异常类        定义 service 层可能会抛出的异常类 NotEnoughException
package com.it.exception;

//演示出现运行时异常,发生回滚事件
public class NotEnoughException extends RuntimeException {
    public NotEnoughException() {
        super();
    }

    public NotEnoughException(String s) {
        super(s);
    }
}
Step6 :定义 Service 接口         定义 Service 接口 BuyGoodsService
package com.it.service;

public interface BuyGoodsService {
//购买商品的方法,goodId购买商品的编号,nums:购买商品的数量
    void buy(Integer goodsId,Integer nums);
}
Step7 :定义 service 的实现类       定义 service 层接口的实现类 BuyGoodsServiceImpl
package com.it.service.impl;

import com.it.dao.GoodsDao;
import com.it.dao.SaleDao;
import com.it.entity.Goods;
import com.it.entity.Sale;
import com.it.exception.NotEnoughException;
import com.it.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() 
            
             Step8
             :修改 
             Spring 
             配置文件内容 
             
             
             
              声明 
              Mybatis 
              对象
              ,声明业务层对象 
              
              
              




    



    
    


        
        
        
        
    


    

        

        
    


    

        

        
    


    
        
        
    


Step9 :定义测试类 定义测试类 MyTest 。现在就可以在无事务代理的情况下运行了。
package com.it;

import static org.junit.Assert.assertTrue;

import com.it.service.BuyGoodsService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class AppTest 
{
//  测试正常购买
  @Test
    public void test1(){
      String config="applicationContext.xml";
      ApplicationContext ac=new ClassPathXmlApplicationContext(config);
//      从容器获取service
      BuyGoodsService buyService = (BuyGoodsService) ac.getBean("buyService");

//      调用方法
      buyService.buy(1001,10);
  }

//  测试购买不存在的商品
  @Test
  public void test2(){
    String config="applicationContext.xml";
    ApplicationContext ac=new ClassPathXmlApplicationContext(config);
    BuyGoodsService buyService = (BuyGoodsService) ac.getBean("buyService");
    buyService.buy(1003,10);
  }

  //  测试购买超出库存的商品
  @Test
  public void test3(){
    String config="applicationContext.xml";
    ApplicationContext ac=new ClassPathXmlApplicationContext(config);
    BuyGoodsService buyService = (BuyGoodsService) ac.getBean("buyService");
    buyService.buy(1002,100);
  }







}

测试结果

测试正常购买后

测试购买不存在的商品

测试购买存在的商品,但超出商品库存

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/952708.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号