gpt4 book ai didi

java - QueryDSL 日期算法

转载 作者:行者123 更新时间:2023-11-30 06:10:56 27 4
gpt4 key购买 nike

我有一个这样的表:

CREATE  TABLE IF NOT EXISTS `sma`.`MIGRation` (
`MIGR_ID` BIGINT NOT NULL AUTO_INCREMENT ,
`MIGR_From` VARCHAR(1024) NOT NULL ,
`MIGR_To` VARCHAR(1024) NOT NULL ,
`MIGR_Validity_Days` INT NOT NULL ,
`MIGR_CreationDate` DATETIME NOT NULL ,
PRIMARY KEY (`MIGR_ID`) );

您将如何在 QueryDSL 中编写表达式来查找仍然有效的条目 (creationDate + validity_Days)?

SELECT * FROM MIGRation WHERE DATE_ADD(MIGR_CreationDate, INTERVAL MIGR_Validity_Days DAY) > now()

最佳答案

对于算术查询,请查看 com.querydsl.core.types.dsl.Expressions 类。在这种情况下,可以使用 Expressions::dateTimeOperation:

import com.querydsl.core.types.Ops;
import com.querydsl.core.types.dsl.*;
import com.querydsl.sql.SQLQuery;
...
@Transactional
public List<MIGRationEntity> getMigration() {
SQLQuery<MIGRationEntity> query = selectFrom(QMIGRation.MIGRation)
.where(
Expressions.dateTimeOperation(
Date.class, Ops.DateTimeOps.ADD_DAYS,
QMIGRation.MIGRation.mIGRCreationDate,
QMIGRation.MIGRation.mIGRValidityDays)
.gt(DateTimeExpression.currentTimestamp()));
String sql = query.getSQL().getSQL();
System.out.print(sql);
return query.fetch();
}
...

创建的SQL:

select MIGRation.MIGR_CreationDate, MIGRation.MIGR_From, MIGRation.MIGR_ID, MIGRation.MIGR_To, MIGRation.MIGR_Validity_Days
from MIGRation MIGRation
where date_add(MIGRation.MIGR_CreationDate, interval MIGRation.MIGR_Validity_Days day) > current_timestamp

关于java - QueryDSL 日期算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35021343/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com