直接看例子:
1、声明接口:
public interface CustomRankingRecordMapper { void saveRankCustomInfoProcedure(); }
2、在CustomRankingRecord.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.migu.reading.dao.CustomRankingRecordMapper"> <!-- 调用存储过程更新自定义排行表 --> <update id="saveRankCustomInfoProcedure" statementType="CALLABLE"> {call PROC_CUSTOM_RECORD_UPDATE} </update> </mapper>
3、在Oracle中创建存储过程:
CREATE OR REPLACE procedure VCODE.PROC_CUSTOM_RECORD_UPDATE is cursor ACTIVITY_CUR is --声明显式游标 select T.ACTIVITYID, T.COUNTSTARTTIME, T.COUNTENDTIME from vcode.T_INVITE_ACTIVITYINFO T where T.HASCOUNTTIME = 1; type ACTIVITY_CUR_ROW is table of ACTIVITY_CUR%ROWTYPE; --定义游标变量,该变量的类型为基于游标ACTIVITY_CUR的记录 cs_invitestat SYS_REFCURSOR; type tp_CUSTOM_RECORD is table of T_INVITER_CUSTOM_RECORD%ROWTYPE; va_CUSTOM_RECORD tp_CUSTOM_RECORD; ACTIVITY_ID number(11,0); START_TIME date; END_TIME date; begin --For 循环 for ACTIVITY_CUR_ROW in ACTIVITY_CUR LOOP ACTIVITY_ID := ACTIVITY_CUR_ROW.ACTIVITYID; START_TIME := ACTIVITY_CUR_ROW.COUNTSTARTTIME; select TRUNC(ACTIVITY_CUR_ROW.COUNTENDTIME+1)-1/(24*3600) into END_TIME from DUAL; open cs_invitestat for select T1.INVITERMSISDN,COUNT(*) as TOTALACTIVENUMBER,max(ACTIVETIME) LASTACTIVETIME,ACTIVITYID from T_INVITEE_RECORD T1 where (T1.ACTIVETIME <= END_TIME and T1.ACTIVETIME >= START_TIME and T1.ACTIVESTATUS = 1 and T1.INVITEETYPE = 0 and T1.ACTIVITYID = ACTIVITY_ID) group by ACTIVITYID,T1.INVITERMSISDN; fetch cs_invitestat bulk collect into va_CUSTOM_RECORD limit 500; forall i in 1..va_CUSTOM_RECORD.count merge into vcode.T_INVITER_CUSTOM_RECORD T5 using (select * from dual) on (INVITERMSISDN = va_CUSTOM_RECORD(i).INVITERMSISDN AND ACTIVITYID=va_CUSTOM_RECORD(i).ACTIVITYID) when matched then update set TOTALACTIVENUMBER =va_CUSTOM_RECORD(i).TOTALACTIVENUMBER, LASTACTIVETIME =va_CUSTOM_RECORD(i).LASTACTIVETIME where T5.TOTALACTIVENUMBER!=va_CUSTOM_RECORD(i).TOTALACTIVENUMBER when not matched then insert ( INVITERMSISDN, TOTALACTIVENUMBER, LASTACTIVETIME, ACTIVITYID ) values ( va_CUSTOM_RECORD(i).INVITERMSISDN, va_CUSTOM_RECORD(i).TOTALACTIVENUMBER, va_CUSTOM_RECORD(i).LASTACTIVETIME, va_CUSTOM_RECORD(i).ACTIVITYID ); commit; end LOOP; end;