Sql 服务器查询在 SQLServer 中运行良好,但相同的查询不适用于 JDBI

Sql Server query runs fine within SQLServer,but the same query does not work with JDBI

为什么我可以在 Sql 服务器

中正常执行以下查询
SELECT notificationMessage FROM NotificationMessages WHERE timesUsed < 2

但是当在

中调用查询时
@SqlQuery("SELECT notificationMessage FROM NotificationMessages WHERE timesUsed < 2")
List<String> getNotificationMessages();

我收到以下错误

There is only one expression of non-boolean type in a context where there was expected a condition near 'timeUsed'

只是 google 将其翻译为快速通道。另一件有趣的事情是,我的异常消息是用丹麦语打印的 -> 只有 sql 异常。我曾尝试 运行 SET LANGUAGE English 在 Sql 服务器中提供帮助,但在 Intellij 中来自 jdbi 的异常消息中没有帮助。

编辑 1 如果有人遇到同样的问题,那么我会使用 SELECT 所有消息的解决方案,然后在代码中执行所有逻辑。我做了类似以下的事情:

public interface NotificationDAO {
@SqlQuery("SELECT id, notificationMessage, timesUsed, messageType, messageDays FROM NotificationMessages WHERE messageType = 'REMINDER'")
    List<NotificationMessage> getReminderNotificationMessages();
}

public class NotificationResource {
    ...
    private NotificationMessage pickNotificationMessage(){
        List<NotificationMessage> notificationMessages = notificationDAO.getReminderNotificationMessages();
        // Extract the number of times a message has been sent out to users, store in Integer list.
        List<Integer> timesUsedList = notificationMessages.stream().map(nm -> nm.getTimesUsed()).collect(Collectors.toList());
        // Remove all those messages which have been used once more than others. //TODO maybe another type of removal of messages
        List<NotificationMessage> notificationMessagesTruncated = notificationMessages.stream().filter(nm -> nm.getTimesUsed() <= Collections.max(timesUsedList)-1).collect(Collectors.toList());
        ...
    }
    ...
}

编辑 2 @zloster 我不会说它与您 linking 的 post 完全相同。因为他们的问题是将列表绑定到查询,所以我的解决方案运行良好。我的问题是我无法弄清楚为什么我不能对我的查询进行简单的低于操作,例如 WHERE 1 < 2。我没有意识到 < 是 StringTemplate 的保留字符。但是,是的,你给出的 link 解决了我的问题“...括号 < 像这样 \< ...”,但我不同意说它自己的问题是重复的。

所以他们的问题是"How do you bind a list to a sql query IN statement?"

我的问题是 "Why does comparison operators not work in JDBI sql"

当然是因为我使用注释 @UseStringTemplate3StatementLocator 使我的列表映射到 IN 语句。我有工作,他们没有。但是我无法使任何类型的低于或高于操作起作用。

我找到了我的查询问题的解决方案,在 JDBI 上搜索另​​一个主题时我偶然发现了这个 post 我特别注意到这句话

Also note that with this annotation, you cannot use '<' character in your SQL queries without escaping (beacause it is a special symbol used by StringTemplate).

并且我在我的界面中使用了注解 @UseStringTemplate3StatementLocator,我已经对其进行了测试,当它们使用 \ 进行转义时,它可以与 gt, lt, gte, lte 运算符一起使用。因此,如果有人使用相同的注释并遇到与我相同的问题,那么如果您使用这些查询运算符 <, >, <=, >=.

就可以解决问题