1.创建Oracle存储过程完成库存加减操作,如果按equipType1和equipType2查询记录存在,则在原数量基础上进行增减;如果查询记录不存在,则新增一条记录。
存储过程创建如下:
CREATE OR REPLACE PROCEDURE "UPDATE_EQUIPMENT_STOCK" (P_EQUIP_TYPE1 IN NUMBER, P_EQUIP_TYPE2 IN NUMBER, P_NUM IN NUMBER, P_STATION_ID IN NUMBER, P_RESULT OUT NUMBER) AS STOCK_NUM NUMBER DEFAULT 0; E_COUNT NUMBER DEFAULT 0; BEGIN SELECT COUNT(ID) INTO E_COUNT from EQUIPMENT_STOCK WHERE EQUIP_TYPE1=P_EQUIP_TYPE1 AND EQUIP_TYPE2=P_EQUIP_TYPE2; IF (E_COUNT > 0) THEN SELECT E_STOCK INTO STOCK_NUM from EQUIPMENT_STOCK WHERE EQUIP_TYPE1=P_EQUIP_TYPE1 AND EQUIP_TYPE2=P_EQUIP_TYPE2; STOCK_NUM := STOCK_NUM + P_NUM; UPDATE EQUIPMENT_STOCK SET E_STOCK=STOCK_NUM WHERE EQUIP_TYPE1=P_EQUIP_TYPE1 AND EQUIP_TYPE2=P_EQUIP_TYPE2; --UPDATE ELSE STOCK_NUM:=P_NUM; INSERT INTO EQUIPMENT_STOCK(ID,EQUIP_TYPE1,EQUIP_TYPE2,E_STOCK,STATION_ID) VALUES(SEQ_EQUIPMENT_STOCK.NEXTVAL,P_EQUIP_TYPE1,P_EQUIP_TYPE2,STOCK_NUM,P_STATION_ID); --INSERT END IF; P_RESULT:=1; END;
mybatis调用
Mapper.xml
<select id="updateEquipmentStock" parameterType="java.util.Map" statementType="CALLABLE"> {call UPDATE_EQUIPMENT_STOCK (#{equipType1,mode=IN,jdbcType=INTEGER}, #{equipType2,mode=IN,jdbcType=INTEGER}, #{pNum,mode=IN,jdbcType=INTEGER}, #{stationId,mode=IN,jdbcType=INTEGER},#{p_result,mode=OUT,jdbcType=INTEGER})} </select>
Mapper.java
public Integer updateEquipmentStock(Map<String, Object> paramMap);
Dao
public Integer updateEquipmentStock(Map<String, Object> paramMap) { // TODO Auto-generated method stub return equipmentStockMapper.updateEquipmentStock(paramMap); }
Service
public Integer updateEquipmentStock(Long equipType1, Long equipType2, Integer pNum, Long stationId) { Map<String, Object> paramMap = new HashMap<String, Object>(); paramMap.put("equipType1", equipType1); paramMap.put("equipType2", equipType2); paramMap.put("pNum", pNum); paramMap.put("stationId", stationId); paramMap.put("p_result", -1); equipmentStockDAO.updateEquipmentStock(paramMap); return (Integer)paramMap.get("p_result"); }
自己搜索资料编写,可用。
PS:pNum参数使用正负数完成库存的增减