记录登录系统的时间,最多记录10次,后来添加的时间会覆盖之前的时间,永远只看到最后的10次登录时间。
设计如下:
关键字段:zhxgsj VARCHAR2(500);
数据库表的建设sql:
-- Create table
create table tableA
(
yhdm VARCHAR2(50) not null,
tokenval VARCHAR2(250),
zhxgsj VARCHAR2(500)
)
tablespace JXYTH
pctfree 10
initrans 1
maxtrans 255
storage
(
initial 7M
next 1M
minextents 1
maxextents unlimited
);
-- Add comments to the table
comment on table tableA
is 'APP_token管理表';
-- Add comments to the columns
comment on column tableA.yhdm
is '用户代码';
comment on column tableA.tokenval
is '登录成功后的token值';
comment on column tableA.zhxgsj
is '最后登录时间';
-- Create/Recreate primary, unique and foreign key constraints
alter table tableA
add constraint PK_tableA primary key (YHDM)
using index
tablespace JXYTH
pctfree 10
initrans 2
maxtrans 255
storage
(
initial 5M
next 1M
minextents 1
maxextents unlimited
);
java后台存储 zhxgsj 字段值的时候,这么存储:
xml中的sql:
<update id="userlogin.updateTokenByYhdm" parameterClass="java.util.HashMap">
merge into tableA p using (select count(*) count from tableA where yhdm = #yhdm#) numm on (numm.count > 0)
when not matched then
insert into tableA (yhdm, tokenval, zhxgsj)
values (#yhdm#, #token#, (select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual) )
when matched then
update tableA set zhxgsj = (
case when length(zhxgsj) >= 219 then
(select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual)
||'&'
||(select substr(zhxgsj,0,199) from tableA where yhdm=#yhdm#)
else (
(select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual)
||'&'
||(select zhxgsj from tableA where yhdm=#yhdm#)
)
end
)
where yhdm = #yhdm#
</update>
注意:里面之所以有像219、199这些数字,是因为,一直都是对日期值的格式化严格把控,加上 & 符号,全部在内,计算得来。
219是10个【yyyy-mm-dd hh24:mi:ss】格式的数据和10个【&】符号组成的字符串的长度。
查询最后的登录时间:
select substr(zhxgsj,0,19) as zhxgsj from tableA where yhdm = #yhdm#