初始Seata(三)

目录

1.背景

2.库存服务

1)pom.xml

2)配置文件


1.背景

书接上回初识Seata(二),继续库存服务模块的代码。

2.库存服务

目录结构

  • java
    • com.seata.storage
      • config
      • controller
      • dao
      • entity
      • service
  • resources
    • mapper(package)
    • application.yml
    • file.conf
    • registry.conf

1)pom.xml

这部分与上回中的

@RequestController
@RequestMapping("/storage/")
public class StorageController {
    @Resource
    private StorageService storageService;
 
    @GetMapping("decrease")
    public String decrease(@RequestParam("productId") Long productId, @RequestParam("count") Integer count){
        storageService.decrease(productId, count);
        return "库存扣减成功"
    }
}

服务是一样的,不再赘述。

2)配置文件

*** file.confregistry.conf与上文一致,在此不再赘述

*** application.yml

server:
  port: 8803
spring:
  application:
    name: seata-storage
  cloud:
    alibaba:
     seata:
       # 这里要与file.conf中的值保持一致
       tx-service-group: my_test_tx_group
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: 123456
    url: jdbc:mysql://locahost:3306/seata_storage?serverTimezone=GMT%2B8&characterEncoding=utf8
 
eureka:
  client:
    service-url:
      dafaultZone: http://localhost:8801/eureka/
  instance:
    hostname: localhost
    prefer-ip-address: true
logging:
  level:
    io:
      seata: info
mybatis:
  mapperLocations: classpath:mapper/*.xml

*** 启动类

@EnableDiscoveryClient
@EnableEurekaClients
public class StorageAppMain8803
{
    public static void main(String[] args){
        SpringApplication.run(StorageAppMain8803.class, args);
    }
}

*** config

**** DataSourceProxyConfig.java

这个文件与前文相同,不再赘述

**** MyBatisConfig.java

@Configuration
@MapperScan({"com.seata.storage.dao"})
public class MyBatisConfig{
}

*** entity

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Storage {
    private Long id;
    private Long productId;
    private Integer total;
    private Integer used;
    private Integer residue;
}

*** controller

@RequestMapping("/storage/")
public class StorageController {
    @Resource
    private StorageService storageService;
 
    @GetMapping("decrease")
    public String decrease(@RequestParam("productId") Long productId, @RequestParam("count") Integer count){
        storageService.decrease(productId, count);
        return "库存扣减成功"
    }
}

*** service & serviceImpl

**** service

public interface StorageService {
    void decrease(Long productId, Integer count);
}
@Service
public class StorageServiceImpl implements StorageService {
    @Resource
    private StorageDao storageDao;
 
    public void decrease(Long productId, Integer count) {
        storageDao.decrease(productId, count);
    }
}

*** dao

@Mapper
public interface StorageDao {
    void decrease(@Param("productId") Long productId, @Param("count") Integer count);
}

*** resources

**** mapper.StorageMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 
<mapper namespace="com.seata.storage.dao.StorageDao">
    <resultMap id="BaseResultMap" type="com.seata.storage.entity.Storage">
        <id column="id" property="id" jdbcType="BIGINT">
        <id column="product_id" property="productId" jdbcType="BIGINT">
        <id column="total" property="total" jdbcType="BIGINT">
        <id column="used" property="used" jdbcType="BIGINT">
        <id column="residue" property="residue" jdbcType="BIGINT">
    </resultMap>
    <update id="decrease">
        update storage
        set used = used + #{count}, residue = residue - #{count}
        where product_id=#{productId}
    </update>
</mapper>

未完待续~~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值