81,092
社区成员
发帖
与我相关
我的任务
分享
String hql="select count(*) from CstCustomer";
Integer index=(Integer)baseDAO.uniqueResult(hql, null);
String noStr="KH";//前缀
Date date=new Date();
String time=date.toLocaleString();
int index1=time.indexOf("-");
String year=time.substring(0, index1);
int index2=time.lastIndexOf("-");
String month=time.substring(index1+1,index2);
if(month.length()<2){
month="0"+month;
}
int index3=time.indexOf(" ");
String day=time.substring(index2+1,index3);
if(day.length()<2){
day="0"+day;
}
String count="";
if(index<10){
count="0000"+(index+1);
}else if(index<100){
count="000"+(index+1);
}else if(index<1000){
count="00"+(index+1);
}else if(index<10000){
count="0"+(index+1);
}else{
count=""+(index+1);
}
time+=year+month+day;
noStr+="_"+time+"_"+count;
表结构:
CREATE TABLE "table_name"
( "ID" NUMBER(11,0),
"ENTITY_ID" NUMBER(10,0),
"ENTITY_CLASS" VARCHAR2(200 BYTE),
"MESSAGE" VARCHAR2(300 BYTE) NOT NULL ENABLE,
"CREATED" DATE,
PRIMARY KEY ("MESSAGE"))
触发器:
CREATE OR REPLACE
TRIGGER AUTO_AUDIT_LOGS_PK -- 触发器名称
BEFORE INSERT ON YT_AUDIT_LOGS -- 设置在更新操作前
FOR EACH ROW -- 使用DML触发
DECLARE
old_id VARCHAR2(15); -- 数据库中当前主键
cr_date VARCHAR2(10); -- 当前日期yyyymmdd
new_id VARCHAR2(15); -- 根据规则生成的主键id
sNo VARCHAR2(4); -- 流水号
BEGIN
select max(message) into old_id from table_name; -- 获取当前数据库中最大的id
select to_char(sysdate, 'yyyymmdd') INTO cr_date from dual; -- 获取当前时间
IF length(old_id) > 14 THEN
sNo:= lpad(to_char(to_number(substr(old_id,12,4))+1),4,'0'); -- 获取新的流水号
new_id:='abc' || cr_date || sNo; -- 生成新的ID
ELSE
new_id:='abc' || cr_date || '0001';
END IF;
:NEW.message:= new_id; -- 给插入时的主键赋值
END;
插入SQL:
insert into yt_audit_logs(id, entity_class, entity_id, created)
values(1,'ac',2,sysdate); -- 插入时不用声明message主键,而是由触发器自动生成
Date date = new Date();
SimpleDateFormat sp1 = new SimpleDateFormat("yyyyMMdd");
SimpleDateFormat sp2 = new SimpleDateFormat("yyyyMM");
String time = sp2.format(date);
//修改下在17楼的这部分,睡觉去了
String t1=oldId.substring(3,9);
if (time.equals(t1)) {