• Spring 事务(测试)--在这个笔记中记录的是没有添加事务,数据库返回的效果。...


    第5章Spring 事务(测试)--在这个笔记中记录的是没有添加事务,数据库返回的效果。

    1. 首先搞两张表,商品表和订单表

    举例:购买商品 trans_sale 项目 本例要实现购买商品,模拟用户下订单,向订单表添加销售记录,从商品表减少库存。

    实现步骤:

    • Step0:创建数据库表

      sale 销售表

      goods 商品表

    • Step1: maven 依赖 pom.xml

      1. <dependency>
      2. <groupId>junitgroupId>
      3. <artifactId>junitartifactId>
      4. <version>4.11version>
      5. <scope>testscope>
      6. dependency>
      7. <dependency>
      8. <groupId>org.springframeworkgroupId>
      9. <artifactId>spring-contextartifactId>
      10. <version>5.2.5.RELEASEversion>
      11. dependency>
      12. <dependency>
      13. <groupId>org.springframeworkgroupId>
      14. <artifactId>spring-txartifactId>
      15. <version>5.2.5.RELEASEversion>
      16. dependency>
      17. <dependency>
      18. <groupId>org.springframeworkgroupId>
      19. <artifactId>spring-jdbcartifactId>
      20. <version>5.2.5.RELEASEversion>
      21. dependency>
      22. <dependency>
      23. <groupId>org.mybatisgroupId>
      24. <artifactId>mybatisartifactId>
      25. <version>3.5.1version>
      26. dependency>
      27. <dependency>
      28. <groupId>org.mybatisgroupId>
      29. <artifactId>mybatis-springartifactId>
      30. <version>1.3.1version>
      31. dependency>
      32. <dependency>
      33. <groupId>mysqlgroupId>
      34. <artifactId>mysql-connector-javaartifactId>
      35. <version>5.1.9version>
      36. dependency>
      37. <dependency>
      38. <groupId>com.alibabagroupId>
      39. <artifactId>druidartifactId>
      40. <version>1.1.12version>
      41. dependency>
      42. 插件
      43. <build>
      44. <resources>
      45. <resource>
      46. <directory>src/main/javadirectory>
      47. <includes>
      48. <include>**/*.propertiesinclude>
      49. <include>**/*.xmlinclude>
      50. includes>
      51. <filtering>falsefiltering>
      52. resource>
      53. resources>
      54. <plugins>
      55. <plugin>
      56. <artifactId>maven-compiler-pluginartifactId>
      57. <version>3.1version>
      58. <configuration>
      59. <source>1.8source>
      60. <target>1.8target>
      61. configuration>
      62. plugin>
      63. plugins>
      64. build>

      项目的结构

    • Step2:创建实体类

      创建实体类 Sale 与 Goods

    • Step3:定义 dao 接口

    • Step4:定义 dao 接口对应的 sql 映射文件

    • Step5:定义异常类

      定义 service 层可能会抛出的异常类 NotEnoughException

      1. package com.bjpowernode.excep;
      2. //自定义的运行时异常
      3. public class NotEnoughException extends RuntimeException {
      4. public NotEnoughException() {
      5. super();
      6. }
      7. public NotEnoughException(String message) {
      8. super(message);
      9. }
      10. }
    • Step6:定义 Service 接口

      1. package com.bjpowernode.service;
      2. public interface BuyGoodsService {
      3. //购买商品的方法, goodsId:购买商品的编号, nums:购买的数量
      4. void buy(Integer goodsId,Integer nums);
      5. }
    • Step7:定义 service 的实现类

      1. package com.bjpowernode.service.impl;
      2. import com.bjpowernode.dao.GoodsDao;
      3. import com.bjpowernode.dao.SaleDao;
      4. import com.bjpowernode.domain.Goods;
      5. import com.bjpowernode.domain.Sale;
      6. import com.bjpowernode.excep.NotEnoughException;
      7. import com.bjpowernode.service.BuyGoodsService;
      8. public class BuyGoodsServiceImpl implements BuyGoodsService {
      9. private SaleDao saleDao;
      10. private GoodsDao goodsDao;
      11. @Override
      12. public void buy(Integer goodsId, Integer nums) {
      13. System.out.println("=====buy方法的开始====");
      14. //记录销售信息,向sale表添加记录
      15. Sale sale = new Sale();
      16. sale.setGid(goodsId);
      17. sale.setNums(nums);
      18. saleDao.insertSale(sale);
      19. //更新库存
      20. Goods goods = goodsDao.selectGoods(goodsId);
      21. if( goods == null){
      22. //商品不存在
      23. throw new NullPointerException("编号是:"+goodsId+",商品不存在");
      24. } else if( goods.getAmount() < nums){
      25. //商品库存不足
      26. throw new NotEnoughException("编号是:"+goodsId+",商品库存不足");
      27. }
      28. //修改库存了
      29. Goods buyGoods = new Goods();
      30. buyGoods.setId( goodsId);
      31. buyGoods.setAmount(nums);
      32. goodsDao.updateGoods(buyGoods);
      33. System.out.println("=====buy方法的完成====");
      34. }
      35. public void setSaleDao(SaleDao saleDao) {
      36. this.saleDao = saleDao;
      37. }
      38. public void setGoodsDao(GoodsDao goodsDao) {
      39. this.goodsDao = goodsDao;
      40. }
      41. }
    • Step8:修改 Spring 配置文件内容

      1. "1.0" encoding="UTF-8"?>
      2. <beans xmlns="http://www.springframework.org/schema/beans"
      3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      4. xmlns:context="http://www.springframework.org/schema/context"
      5. 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">
      6. <context:property-placeholder location="classpath:jdbc.properties" />
      7. <bean id="myDataSource" class="com.alibaba.druid.pool.DruidDataSource"
      8. init-method="init" destroy-method="close">
      9. <property name="url" value="${jdbc.url}" />
      10. <property name="username" value="${jdbc.username}"/>
      11. <property name="password" value="${jdbc.passwd}" />
      12. <property name="maxActive" value="${jdbc.max}" />
      13. bean>
      14. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
      15. <property name="dataSource" ref="myDataSource" />
      16. <property name="configLocation" value="classpath:mybatis.xml" />
      17. bean>
      18. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
      19. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
      20. <property name="basePackage" value="com.bjpowernode.dao"/>
      21. bean>
      22. <bean id="buyService" class="com.bjpowernode.service.impl.BuyGoodsServiceImpl">
      23. <property name="goodsDao" ref="goodsDao" />
      24. <property name="saleDao" ref="saleDao" />
      25. bean>
      26. beans>
    • 其中mybatis的主配置文件和jdbc连接的配置文件如下:

    • Step9:定义测试类

      1. package com.bjpowernode;
      2. import com.bjpowernode.service.BuyGoodsService;
      3. import org.junit.Test;
      4. import org.springframework.context.ApplicationContext;
      5. import org.springframework.context.support.ClassPathXmlApplicationContext;
      6. public class MyTest {
      7. @Test
      8. public void test01(){
      9. String config="applicationContext.xml";
      10. ApplicationContext ctx = new ClassPathXmlApplicationContext(config);
      11. //从容器获取service
      12. BuyGoodsService service = (BuyGoodsService) ctx.getBean("buyService");
      13. //调用方法
      14. service.buy(1001,200);
      15. }
      16. }
    • 当正常执行时候。

    • 商品不存在时

    • 商品数量不足时候

  • 相关阅读:
    openGauss学习笔记-126 openGauss 数据库管理-设置账本数据库-归档账本数据库
    1092 To Buy or Not to Buy (PAT甲级)
    C语言初学1:详解#include <stdio.h>
    bootstrap的相关知识点
    chrome浏览器一键切换搜索引擎,一键切换谷歌和百度搜索
    文生视频开源产品的一些调研(一)
    “最熟悉的陌生人”之Elsevier出版社全解说
    抗疫逆行者HTML网页作业 感动人物网页代码成品 最美逆行者网页模板 致敬疫情感动人物网页设计制作
    基于leetcode的算法训练:Day2
    Spring Boot集成第三方登录之微信登录
  • 原文地址:https://blog.csdn.net/weixin_48370579/article/details/127827746