如何在 querydsl 中使用 mysql now() 函数?

How to use mysql now() function in querydsl?

假设我有一个 mySQL table 神器如下:

artifact_id varchar(50)
locked_status char(1)
valid_till datetime

 Select artifact_id from artifact where locked_status='Y'

对于上述查询,我​​将 querydsl 表示为:

SQLTemplates templates = new MySQLTemplates();
Configuration configuration = new Configuration(templates);
QArtifact artifact = new QArtifact("a");
SQLQuery<?> query = new SQLQuery<Void>(con, templates);
List<String> artifactIds = query.select(artifact.artifact_id)
            .from(artifact)
            .where(artifact.locked_status.eq("Y"))
            .fetch();

如何在 querydsl 中表示以下查询?

Select artifact_id from artifact where locked_status='Y' and valid_till > now()

最简单的方法是添加

.and(artifact.valid_till.after(new Date()))

但是,如果您对要在服务器上调用的 NOW 函数很挑剔,您可以选择以下方法:

.and(artifact.valid_till.after(Expressions.currentTimestamp()))

您可以浏览代码以查看此助手返回的 DateTimeExpression 是如何生成的。