MyBatis调用存储过程

Posted 不想下火车的人

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MyBatis调用存储过程相关的知识,希望对你有一定的参考价值。

  直接看例子:

  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;

 

以上是关于MyBatis调用存储过程的主要内容,如果未能解决你的问题,请参考以下文章

mybatis 调用存储过程

使用MyBatis轻松实现递归查询与存储过程调用

mybatis 调用mysql存储过程 带输出输入参数

mybatis调用Oracle存储过程

mybatis 存储过程

mybatis 调用存储过程进行批量修改操作(只需要一次调用存储过程),不知道如何配置配置文件