0
点赞
收藏
分享

微信扫一扫

在库存服务中实现缓存与数据库双写一致性保障方案(二)

小亦同学321 2022-03-18 阅读 6


封装请求接口,第一个实现类,data数据更新的操作,比如一个商品发生了交易,就要修改商品对应的库存了,此时就

data update request,数据更新请求

1.删除缓存

2.更新数据库

实体类:

package com.roncoo.eshop.entity;

/**
* 库存数量model
* @author Administrator
*
*/
public class ProductInventory {

/**
* 商品id
*/
private Integer productId;
/**
* 库存数量
*/
private Long inventoryCnt;

public ProductInventory() {

}

public ProductInventory(Integer productId, Long inventoryCnt) {
this.productId = productId;
this.inventoryCnt = inventoryCnt;
}

public Integer getProductId() {
return productId;
}
public void setProductId(Integer productId) {
this.productId = productId;
}
public Long getInventoryCnt() {
return inventoryCnt;
}
public void setInventoryCnt(Long inventoryCnt) {
this.inventoryCnt = inventoryCnt;
}

}


  mapper:

package com.roncoo.eshop.dao;

import com.roncoo.eshop.entity.ProductInventory;
import org.apache.ibatis.annotations.Param;

public interface ProductInventoryMapper {

/**
* 更新库存数量
* @param productInventory 商品库存
*/
void updateProductInventory(ProductInventory productInventory);

/**
* 根据商品id查询商品库存信息
* @param productId 商品id
* @return 商品库存信息
*/
ProductInventory findProductInventory(@Param("productId") Integer productId);
}


  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.roncoo.eshop.dao.ProductInventoryMapper">

<update id="updateProductInventory" parameterType="com.roncoo.eshop.entity.ProductInventory">
update product_inventory set inventory_cnt=#{inventoryCnt} where product_id=#{productId}
</update>

<select id="findProductInventory" resultType="com.roncoo.eshop.entity.ProductInventory">
select product_id as "productId",inventory_cnt as "inventoryCnt" from
product_inventory where product_id=#{productId}
</select>

</mapper>


  sql:

在库存服务中实现缓存与数据库双写一致性保障方案(二)_xml



create table product_inventory
(
product_id int auto_increment
primary key,
inventory_cnt bigint(255) null
);




作者:​​三号小玩家​​​,转载请注明原文链接



举报

相关推荐

0 条评论