首先从一个例子入手:
需求:根据门店id和商品id查询门店下面的商品的库存量
开始使用的方式为:
@Test
public void selectGoodsInventoryByShopIdAndGoodsId(){
Map<String,Object> param=new HashMap<>();
param.put("goodsId", 2L);
param.put("shopId", 2L);
int goodsInventory=goodsMapper.selectGoodsInventoryByShopIdAndGoodsId(param);
System.out.println("goodsInventory="+goodsInventory);
}
其中看到了 dao层传入的是map,这样就导致看到接口之后不知道传入什么参数,必须去看dao层的源码才能知道具体传递的参数,总是觉得很不爽!
所以改造接口,改造之后的代码:
public int selectGoodsInventoryByShopIdAndGoodsId(@Param("shopId") long shopId,@Param("goodsId") long goodsId);
<select id="selectGoodsInventoryByShopIdAndGoodsId" resultType="java.lang.Integer">
SELECT
goods_inventory
FROM
z_shop_goods t
WHERE goods_id = #{goodsId} AND shop_id = #{shopId}
</select>