声明式事务练习题

要求
声明式事务练习题_第1张图片
1.设计四张表,据此知道Dao也应该设计4个,Service相当于把这些Dao综合起来完成一个Shopping购买的行为.

CREATE TABLE `seller`(
id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
uname VARCHAR(32) NOT NULL DEFAULT '',
money DOUBLE NOT NULL DEFAULT 0.0 )CHARSET=utf8;
INSERT INTO `seller` VALUES(NULL, '张三', 1000);
INSERT INTO `seller` VALUES(NULL, '李四', 2000);

CREATE TABLE `buyer`(
id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
uname VARCHAR(32) NOT NULL DEFAULT '',
money DOUBLE NOT NULL DEFAULT 0.0
)CHARSET=utf8;
INSERT INTO `buyer` VALUES(NULL, '王五', 1000);
INSERT INTO `buyer` VALUES(NULL, '赵六', 2000);

DELETE TABLE `goods2`
CREATE TABLE `goods2`(
goods_id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
goods_name VARCHAR(32) NOT NULL DEFAULT '',
price DOUBLE NOT NULL DEFAULT 0.0,
amount INT UNSIGNED DEFAULT 0
)CHARSET=utf8 ;
INSERT INTO `goods2` VALUES(NULL,'小风扇', 10.00,10000);
INSERT INTO `goods2` VALUES(NULL,'小台灯', 12.00,10000);
INSERT INTO `goods2` VALUES(NULL,'可口可乐', 3.00,10000);

CREATE TABLE `taobao`(
id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
money DOUBLE NOT NULL DEFAULT 0.0)CHARSET=utf8;

INSERT INTO taobao VALUES(NULL,0);
SELECT * FROM taobao;
SELECT * FROM goods2;
SELECT * FROM seller;
SELECT * FROM buyer;

2.创建四个dao
声明式事务练习题_第2张图片
(1)BuyerDao.java

package com.elf.spring.tx20231211.homework.dao;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import javax.annotation.Resource;
/**
 * @author 45
 * @version 1.0
 */
@Repository
public class BuyerDao {
    @Resource
    //因为要对数据库操作,所以有jdbcTemplate
    private JdbcTemplate jdbcTemplate;

    //从买家表中,扣除金额
    public void updateMoney(int id, double money) {
        String sql = "UPDATE buyer SET money = money - ? WHERE id = ?";
        jdbcTemplate.update(sql, money, id);
    }
}

(2) Goods2Dao.java

package com.elf.spring.tx20231211.homework.dao;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import javax.annotation.Resource;
/**
 * @author 45
 * @version 1.0
 */
@Repository
public class Goods2Dao {
    @Resource
    private JdbcTemplate jdbcTemplate;

    //根据商品id,返回价格
    //返回单行单列
    public Double queryPriceById(int goodsId) {
        String sql = "SELECT price FROM goods2 WHERE goods_id=?";
        Double price = jdbcTemplate.queryForObject(sql, Double.class, goodsId);
        return  price;
    }
    //根据商品id ,减去库存
    public void updateAmount(int goodsId, int amount) {
        String sql = "UPDATE goods2 SET amount = amount - ? WHERE goods_id = ?";
        jdbcTemplate.update(sql,amount,goodsId);
    }
}

(3)SellerDao.java

package com.elf.spring.tx20231211.homework.dao;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import javax.annotation.Resource;

/**
 * @author 45
 * @version 1.0
 */
@Repository
public class SellerDao {
    @Resource
    private JdbcTemplate jdbcTemplate;

    //对卖家的账号,增加金额
    public void updateMoney(int id, double money) {
        String sql = "UPDATE seller SET money = money + ? WHERE id = ?";
        jdbcTemplate.update(sql, money, id);
    }
}

(4)TaobaoDao.java

package com.elf.spring.tx20231211.homework.dao;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import javax.annotation.Resource;
/**
 * @author 45
 * @version 1.0
 */
@Repository
public class TaobaoDao {
    @Resource
    private JdbcTemplate jdbcTemplate;
    //给id=1的taobao账号,增加金额
    public void updateMoney(int id, double money) {
        String sql = "UPDATE taobao SET money = money + ? WHERE id = ?";
        jdbcTemplate.update(sql,money,id);
    }
}

3.service包下写Goods2Service.java,简单实现声明式事务

package com.elf.spring.tx20231211.homework.service;
import com.elf.spring.tx20231211.homework.dao.BuyerDao;
import com.elf.spring.tx20231211.homework.dao.Goods2Dao;
import com.elf.spring.tx20231211.homework.dao.SellerDao;
import com.elf.spring.tx20231211.homework.dao.TaobaoDao;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
/**
 * @author 45
 * @version 1.0
 */
@Service
public class Goods2Service {
    @Resource
    private BuyerDao buyerDao;
    @Resource
    private SellerDao sellerDao;
    @Resource
    private Goods2Dao goods2Dao;
    @Resource
    private TaobaoDao taobaoDao;

    //用户的购买商品的行为会操作多张表,视为一个事务进行管理
    @Transactional
    public void shopping(int goodsId, int buyerId, int sellerId, int amount) {
        Double price = goods2Dao.queryPriceById(goodsId);
        //花费多少钱
        double money = price * amount;
        buyerDao.updateMoney(buyerId, money);
        //将成交额的90% 转入卖家
        sellerDao.updateMoney(sellerId, money * 0.9);
        //将成交额的10% 转入淘宝账号
        taobaoDao.updateMoney(1, money * 0.1);
        //减去商品的库存
        goods2Dao.updateAmount(goodsId,amount);
    }
}

4.src下创建Spring容器配置文件shopping_ioc.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:tx="http://www.springframework.org/schema/tx"
       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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!--引入外部属性文件jdbc.properties-->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!--配置数据源-DataSource-->
    <bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource">
        <!--设置属性值-->
        <property name="user" value="${jdbc.user}"/>
        <property name="password" value="elf"/>
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
    </bean>

    <!--配置JdbcTemplate对象-->
    <bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate">
        <!--配置属性datasource-->
        <!--property name="dataSource"代表jdbcYemplate对象里的一个属性名-->
        <!--ref="dataSource"指向上面创建的对象~~~> <bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource">-->
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--配置事务管理器-->
    <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="dataSourceTransactionManager">
        <!--配置数据源-->
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--开启基于注解的声明式事务管理-->
    <tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>
    <!--配置进行扫描的包-->
    <context:component-scan base-package="com.elf.spring.tx20231211.homework"/>
</beans>

5.spring包下写测试类

package com.elf.spring.tx20231211.homework;
import com.elf.spring.tx20231211.homework.service.Goods2Service;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 * @author 45
 * @version 1.0
 */
public class ShoppingTest {

    @Test
    public void shopping_() {

        //得到容器
        ApplicationContext ioc =
                new ClassPathXmlApplicationContext("shopping_ioc.xml");

        Goods2Service goods2Service = ioc.getBean(Goods2Service.class);
        goods2Service.shopping(1,1,1,1);
        System.out.println("shopping 成功...");
    }
}

运行结果

声明式事务练习题_第3张图片

你可能感兴趣的:(Spring,数据库,spring,java,后端)