gpt4 book ai didi

java - Liferay 的带日期输入的自定义查询

转载 作者:行者123 更新时间:2023-12-01 13:16:05 24 4
gpt4 key购买 nike

我在使用日期输入执行自定义查询时遇到问题。这是我的 sql:

<sql
id="MyEntityFinder.findByIns">
<![CDATA[
SELECT {EX_MyEntity.*}
FROM EX_MyEntity
WHERE
(EX_MyEntity.myDate >= ? AND EX_MyEntity.myDate <= ?)
]]>
</sql>

这是我的查找器 MyEntityFinderImpl:

public class MyEntityFinderImpl extends BasePersistenceImpl<MyEntity> implements MyEntityFinder{

public static final String FIND_BY_INS = "MyEntityFinder.findByIns";
public List<MyEntity> findByIns(Date from, Date to, int begin, int end) throws SystemException{
Session session = null;
try {
session = openSession();
String sql = CustomSQLUtil.get(FIND_BY_INS);
SQLQuery q = session.createSQLQuery(sql);
q.setCacheable(false);
q.addEntity("EX_MyEntity", MyEntityImpl.class);
QueryPos qPos = QueryPos.getInstance(q);
qPos.add("%"+from+"%");
qPos.add("%"+to+"%");
return (List<MyEntity>) QueryUtil.list(q, getDialect(), begin, end);
} catch (Exception e) {
throw new SystemException(e);
} finally {
closeSession(session);
}
}

}

这是我从 jsp 页面调用它的方法:

Calendar aMonthAgo = Calendar.getInstance();
aMonthAgo.set(Calendar.MONTH, aMonthAgo.get(Calendar.MONTH) - 1);
Calendar now = Calendar.getInstance();
List<MyEntity> monthly = MyEntityLocalServiceUtil.findByIns(
new java.sql.Date(aMonthAgo.getTimeInMillis()),
new java.sql.Date(now.getTimeInMillis()), 0,
MyEntityLocalServiceUtil.getMyEntitysCount());

尝试执行查询时出现“java.sql.SQLException:数据异常:无效的日期时间格式”,但我不明白为什么。

最佳答案

尝试这样的事情:

在jsp中:时间戳时间戳 = new Timestamp(aMonthAgo.getTimeInMillis());

在取景器中:qPos.add(timestamp);

另外在 sql 查询中我会使用 BETWEEN:

日期(EX_MyEntity.myDate)在哪里?和?

关于java - Liferay 的带日期输入的自定义查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22457292/

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