目录
1.背景
书接上回初识Seata(二),继续库存服务模块的代码。
2.库存服务
目录结构
- java
- com.seata.storage
- config
- controller
- dao
- entity
- service
- com.seata.storage
- 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.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
<?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>
未完待续~~