0

我有一个需要通过条件子句从数据库中获取一些项目的 Cronjob。在这种情况下,我需要比较一些日期的时间,它们就像数据库中的 (Fri Dec 18 12:36:07 EET 2020)(java.util.Date)。执行查询时,我需要从数据库中检索早于 1 小时的项目,例如当前时间。
我认为我的查询需要像
select * from table_name where date_column > int_variable.
事情是变量是从其他地方设置的整数。我该怎么做?我需要将变量从 int 更改为其他值吗?下面我以我的班级为例。在我的课堂上,我还没有上面写的示例查询。非常感谢!

public class HttpSessionCleanJob extends AbstractJobPerformable<HttpSessionCleanJobModel> {

private static final Logger LOG = Logger.getLogger(HttpSessionCleanJob.class.getName());

@Resource
private ModelService modelService;
@Resource
private FlexibleSearchService flexibleSearchService;

@Override
public PerformResult perform(final HttpSessionCleanJobModel httpSessionCleanJobModel) {

    LOG.info("Start cleaning StoredHttpSession items...");
    final int maxJobTime = httpSessionCleanJobModel.getTime();

    final String queryString = "SELECT {" + StoredHttpSessionModel.PK + "} FROM {" + StoredHttpSessionModel._TYPECODE + "}";

    final Date specificTime = new Date();
    final FlexibleSearchQuery query = new FlexibleSearchQuery(queryString, Collections.singletonMap("specificTime", specificTime));

    final SearchResult<StoredHttpSessionModel> searchResult = flexibleSearchService.search(query);

    List<StoredHttpSessionModel> assetModelList = searchResult.getResult();

    for(StoredHttpSessionModel storedHttpSessionModel : assetModelList) {
        LOG.info(storedHttpSessionModel.getModifiedtime());
    }

    LOG.info("Finished cleaning StoredHttpSession items...");
    return new PerformResult(CronJobResult.SUCCESS, CronJobStatus.FINISHED);
}
}
4

2 回答 2

1

你可以尝试这样的事情:

specificTime.setTime(specificTime.getTime() - 5000)

结果是当前日期减去 5000 毫秒(5 秒)。

然后 :

 final String queryString = "SELECT {" + StoredHttpSessionModel.PK + "} FROM {" + StoredHttpSessionModel._TYPECODE + "} WHERE {modifiedTime} < ?specificTime";

 final Date specificTime = new Date();
 final FlexibleSearchQuery query = new FlexibleSearchQuery(queryString, Collections.singletonMap("specificTime", specificTime));

这将检索所有StoredHttpSessionModel修改日期超过 5 秒的,

调整代码以更符合您的需求。

于 2020-12-18T12:47:52.570 回答
0

我解决了这个问题:

final String queryString = "SELECT {" + StoredHttpSessionModel.PK + "} FROM {" + StoredHttpSessionModel._TYPECODE + "} WHERE {" + StoredHttpSessionModel.MODIFIEDTIME +"} < now() - interval ?maxJobTime SECOND";
于 2020-12-18T12:49:33.637 回答