亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在vaadin 10中創建表并從mysql數據庫中獲取數據

如何在vaadin 10中創建表并從mysql數據庫中獲取數據

暮色呼如 2023-04-26 17:00:29
我正在使用 vaadin 做一個項目,我是新手。我想在垂直布局中創建一個包含 2 或 3 列的表,并通過我的 Myqsl 數據庫獲取值。誰能幫忙解決這個問題。謝謝你的時間。
查看完整描述

1 回答

?
回首憶惘然

TA貢獻1847條經驗 獲得超11個贊

示例示例:

import com.vaadin.flow.component.button.Button;

import com.vaadin.flow.component.grid.Grid;

import com.vaadin.flow.component.orderedlayout.VerticalLayout;

import com.vaadin.flow.component.textfield.TextField;

import com.vaadin.flow.data.binder.Binder;

import com.vaadin.flow.router.Route;


@Route("")

public class VaadinUI extends VerticalLayout {


? ? private final CustomerService service;

? ? private Customer customer;


? ? private Grid<Customer> grid = new Grid<>(Customer.class);

? ? private TextField firstName = new TextField("First name");

? ? private TextField lastName = new TextField("Last name");

? ? private Button save = new Button("Save", e -> saveCustomer());


? ? public VaadinUI(CustomerService service) {

? ? ? ? this.service = service;


? ? ? ? grid.setColumns("firstName", "lastName");

? ? ? ? grid.addSelectionListener(e -> updateForm());


? ? ? ? add(grid, firstName, lastName, save);

? ? ? ? setMargin(true);

? ? ? ? setSpacing(true);

? ? ? ? updateGrid();

? ? }


? ? private void updateGrid() {

? ? ? ? grid.setItems(service.findAll());

? ? ? ? setFormVisible(false);

? ? }


? ? private void updateForm() {

? ? ? ? if (grid.asSingleSelect().isEmpty()) {

? ? ? ? ? ? setFormVisible(false);

? ? ? ? } else {

? ? ? ? ? ? customer = grid.asSingleSelect().getValue();

? ? ? ? ? ? Binder<Customer> binder = new Binder<>(Customer.class);

? ? ? ? ? ? binder.bindInstanceFields(this);

? ? ? ? ? ? binder.setBean(customer);

? ? ? ? ? ? setFormVisible(true);

? ? ? ? }

? ? }


? ? private void setFormVisible(boolean visible) {

? ? ? ? firstName.setVisible(visible);

? ? ? ? lastName.setVisible(visible);

? ? ? ? save.setVisible(visible);

? ? }


? ? private void saveCustomer() {

? ? ? ? service.update(customer);

? ? ? ? updateGrid();

? ? }


}

使用 WebUI 和 MySQL 實現 Vaadin 10 的完整示例:


https://github.com/alejandro-du/mysql-jdbc-vaadin/tree/vaadin10


查看完整回答
反對 回復 2023-04-26
  • 1 回答
  • 0 關注
  • 118 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號