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

初始Seata(三)

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

初始Seata(三)

目录

1.背景

2.库存服务

1)pom.xml

2)配置文件


1.背景

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

2.库存服务

目录结构

java

com.seata.storage

configcontrollerdaoentityserviceresources

mapper(package)application.ymlfile.confregistry.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.conf 和 registry.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



 

    
        
        
        
        
        
    
    
        update storage
        set used = used + #{count}, residue = residue - #{count}
        where product_id=#{productId}
    

未完待续~~

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

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

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