oracle定时器在项目中的应用
業務需求:
現在業務人員提出了一個需求:
在項目中的工作流,都要有一個流程編號,此編號有一定的規則:
前四五位是流程的字母縮寫,中間是8位的日期,后面五位是流水碼,要求流水碼每天從00001開始。即:QJLC2018060800001
?
沒有想到更好的方式,暫時考慮到了使用oracle的定時器來每天定時的將流水碼重置為1。
Oracle數據庫表和定時器的創建:
創建任務編碼表:
| /*==============================================================*/ /* Table: t_flow_taskcode_conf????????????????????????????????? */ /*==============================================================*/ create table t_flow_taskcode_conf? ( ?? flowflag?????? ???????varchar2(8), ?? 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);//設置流程的編碼加1publicCollectDao.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窗口。
?
轉載于:https://www.cnblogs.com/yjk295722366/p/9155112.html
總結
以上是生活随笔為你收集整理的oracle定时器在项目中的应用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: spring手动回滚
- 下一篇: router-link-active 和