Saya membuat obrolan vaadin, dan saya membutuhkannya ketika ada pengguna yang memasuki obrolan, dia hanya akan melihat 10 pesan terakhir dari database MySQL. Saya pikir kode saya diimplementasikan dengan tidak benar. Karena itu menunjukkan kepada saya 10 pesan pertama, meskipun program harus menunjukkan 10 pesan terakhir.

Kelas MainView kode saya -

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));

    }

}

Saya memiliki api/last - Yang seharusnya menampilkan 10 pesan terakhir

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

MessageRepository saya punya kueri SQL untuk 10 pesan terakhir

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

Github saya https://github.com/adk96/vaadin-chat

0
Grace23 29 Januari 2020, 07:14

2 jawaban

Jawaban Terbaik

Kueri SQL diurutkan dalam urutan menaik secara default. Anda perlu mengatur kueri Anda untuk mengurutkan dalam urutan menurun.

@Query(value = "SELECT * FROM chatMessages ORDER BY <yourFieldName> DESC LIMIT 10", nativeQuery = true)
    List<Message> getLastMessages();
6
Eneias Silva 29 Januari 2020, 04:24

Untuk mengembalikan 10 catatan terakhir, Anda dapat menggunakan sesuatu seperti ini:

Dalam perubahan MessageRepository:

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

Untuk

Page<Message> findAll(Pageable pageable);

Dalam perubahan MessageServiceImpl:

@Override
public List<Message> getLast() {
    return repository.getLastMessages();
}

Untuk

@Override
public List<Message> getLast() {
    Pageable pageable = new PageRequest(0, 10, Sort.Direction.DESC, "time");
    Page<Message> bottomPage = repository.findAll(pageable);
    return bottomPage.getContent();
}
2
FarhanNayyer 29 Januari 2020, 06:09