oracle定时器在项目中的应用

业务需求:

如今业务人员提出了一个需求:html

在项目中的工做流,都要有一个流程编号,此编号有必定的规则:java

前四五位是流程的字母缩写,中间是8位的日期,后面五位是流水码,要求流水码天天从00001开始。即:QJLC2018060800001sql

 

没有想到更好的方式,暂时考虑到了使用oracle的定时器来天天定时的将流水码重置为1。数据库

Oracle数据库表和定时器的建立:

建立任务编码表:oracle

/*==============================================================*/函数

/* Table: t_flow_taskcode_conf                                  */编码

/*==============================================================*/spa

create table t_flow_taskcode_conf  (.net

   flowflag              varchar2(8),code

   flowab                varchar2(10),

   flowcode             NUMBER(5)

);

comment on table t_flow_taskcode_conf is '流程生成任务编号表';

comment on column t_flow_taskcode_conf.flowflag is '流程标识';

comment on column t_flow_taskcode_conf.flowab is '流程四位缩写';

comment on column t_flow_taskcode_conf.flowcode is '流水码';

 

insert into t_flow_taskcode_conf (FLOWFLAG, FLOWAB, FLOWCODE) values ('QJLC', 'QJLC', 1);

insert into t_flow_taskcode_conf (FLOWFLAG, FLOWAB, FLOWCODE) values ('BGYP', 'BGYP', 1);

insert into t_flow_taskcode_conf (FLOWFLAG, FLOWAB, FLOWCODE) values ('DJBX', 'DJBX', 1);

commit;

 建立oracle内部的定时器:

create or replace procedure taskcode_procedure is

begin

  update t_flow_taskcode_conf fc set fc.flowcode = 1;

  commit;

end;

 

--定义taskcode天天自动初始化的job任务

declare taskcodejob number;

begin

  dbms_job.submit(

        taskcodejob,  --定时器ID,系统自动得到

        'taskcode_procedure;', --what 执行的存储过程名

        sysdate,  --定时器开始执行的时间,这样写表示当即执行 --next_date,能够不填

        'TRUNC(sysdate)+1'--'Interval时间字符串' --关键设置,此处表示天天的0点执行

  );

commit;

end;

  

#########下面是一些oracle中的job表和内置定时器函数的介绍:

--  select * from user_jobs;  --查看调度任务

--  select * from dba_jobs_running;--查看正在执行的调度任务

--  select * from dba_jobs;--查看执行完的调度任务

 

----更新一个job的sql代码

declare

taskcodejob number;

begin

      dbms_job.run(3);        --运行jobid为3的定时器

      --dbms_job.remove(10);   --9是从user_jobs这个表中查询到而后手动赋值到这里的

      --dbms_job.broken(8);    --中止一个job

      --dbms_job.interval(84,'TRUNC(sysdate)+15/1440');--更改定时器的运行频率

commit;

end; 

 

项目中的使用

Java代码:

/**  * 传入流程的标志,返回流程的任务编码  * @param taskCodeEnum  * @return  */ public String getAndSetTaskCode(FlowTaskCodeEnum taskCodeEnum){
   String flowflag = taskCodeEnum.toString();  //flowflag是”BGYP”,”QJLC”等字符串
   //先获取流程的编码
  
String taskcode = publicCollectDao.getTaskCodeByFlow(flowflag);    Map<String,Object> map = new HashMap<>();    map.put("flowflag",flowflag);    //设置流程的编码加1    publicCollectDao.updateFlowCode(map);    return taskcode; }

Mybatis的xml文件:

<select id="getTaskCodeByFlow" parameterType="string" resultType="string">

  select fc.flowab||to_char(sysdate,'yyyyMMdd')||lpad(fc.flowcode,5,'0') taskcode

  from t_flow_taskcode_conf fc 

  where fc.flowflag = #{flowflag}

</select>

<update id="updateFlowCode" parameterType="map">

  update t_flow_taskcode_conf set flowcode = flowcode+1 where flowflag=#{flowflag}

</update>

 

上面的java代码,要保证getAndSetTaskCode()方法在使用时开启了事务。

 

参考:

https://www.cnblogs.com/mingforyou/archive/2012/06/06/2538063.html

https://blog.csdn.net/anrry258/article/details/26555693

注意区分是普通的sql窗口仍是commond窗口。

相关文章
相关标签/搜索