gpt4 book ai didi

java - 以错误的顺序显示消息

转载 作者:行者123 更新时间:2023-12-01 07:00:18 27 4
gpt4 key购买 nike

我创建了 vaadin 聊天,我需要当任何用户进入聊天时,他应该只看到 MySQL 数据库中的最后 10 条消息。我认为我的代码执行不正确。因为它显示了前 10 条消息,尽管程序应该显示最后 10 条消息。

我的代码MainView 类 -

public class MainView extends VerticalLayout {
private final MessagesInfoManager messagesInfoManager;
private final RestService restService;
private String username;
private TextField textField;
private Message messageLast;


@Autowired
public MainView(RestService restService) {
this.messagesInfoManager = MessageConfigurator.getInstance().getChatMessagesInfoManager();
addClassName("main-view");
setSizeFull();
setDefaultHorizontalComponentAlignment(Alignment.CENTER);

H1 header = new H1("Vaadin Chat");
header.getElement().getThemeList().add("dark");

add(header);

askUsername();
this.restService = restService;
}

private void askUsername() {
HorizontalLayout layout = new HorizontalLayout();
TextField usernameField = new TextField();
Button startButton = new Button("Start chat");

layout.add(usernameField, startButton);

startButton.addClickListener(click -> {
username = usernameField.getValue();
remove(layout);
showChat(username);
});

add(layout);
}

private void showChat(String username) {
MessageList messageList = new MessageList();

List<Message> lasts = restService.getLast();
for (Message message : lasts) {
messageList.add(new Paragraph(message.getFromV() + ": " + message.getMessageV()));
}

messageLast = lasts.get(lasts.size() - 1);

add(messageList, createInputLayout(username, messageList));
expand(messageList);
}

private Component createInputLayout(String username, MessageList messageList) {
HorizontalLayout layout = new HorizontalLayout();
layout.setWidth("100%");

TextField messageField = new TextField();
messageField.addKeyDownListener(Key.ENTER, keyDownEvent -> sender(messageField, messageList));
Button sendButton = new Button("Send");
sendButton.addThemeVariants(ButtonVariant.LUMO_PRIMARY);

layout.add(messageField, sendButton);
layout.expand(messageField);

messageField.addFocusListener(event -> {
for (Message message : messagesInfoManager.getMessagesByUI(getUI())) {
if (!message.getFromV().equals(username)) {
message.setUnread(false);
this.restService.updateMessage(message.getId(), message);
}
}
});

sendButton.addClickListener(click -> sender(messageField, messageList));
messageField.focus();

return layout;
}

private void sender(TextField textField, MessageList messageList) {
Message message = new Message(username, textField.getValue());
message = restService.saveMessage(message);
messageLast = message;
this.textField = textField;
messagesInfoManager.updateMessageUIInfo(new MessageInfo(messageList, message, this));
textField.clear();
textField.focus();

}

@Scheduled(fixedDelay = 5000)
public void scheduleFixedDelayTask() {

MessageList messageList = new MessageList();

List<LinkedHashMap> lasts = restService.getUnreadMessages(messageLast.getId());

for (LinkedHashMap message : lasts) {
messageList.add(new Paragraph(message.get("fromV") + ": " + message.get("messageV")));
}

messagesInfoManager.updateMessageUIInfo(new MessageInfo(messageList, messageLast, this));

}

}

我有 api/last - 应该显示最后 10 条消息

 @GetMapping("/api/last")
public List<Message> getLasts() {
return (messageService.getLast());
}

MessageRepository 我有最近 10 条消息的 SQL 查询

@Query(value = "SELECT * FROM chatMessages LIMIT 10", nativeQuery = true)
List<Message> getLastMessages();

我的Github https://github.com/adk96/vaadin-chat

最佳答案

SQL 查询默认按升序排序。您需要将查询设置为按降序排序。

@Query(value = "SELECT * FROM chatMessages ORDER BY <yourFieldName> DESC LIMIT 10", nativeQuery = true)
List<Message> getLastMessages();

关于java - 以错误的顺序显示消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59960383/

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