Golang中buffalo数据库查询问题求助

Golang中buffalo数据库查询问题求助 我的PostgreSQL服务器中有几个表。我想在这些数据库上运行一些自定义查询,并将数据以表格形式显示在HTML页面中。

应该如何实现?我在网站上没有找到相关的内容。

甚至无法通过buffalo pop完整地展示整个表格。我不知道如何设置处理函数。

请帮我编写这样的处理函数,可以将数据传递给plush。剩下的部分我可以自己处理。

3 回复

更多关于Golang中buffalo数据库查询问题求助的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


非常感谢,效果非常完美。你真是太棒了。

很抱歉打扰你,但能否请你帮我正确设置表单?拜托了。

我只是希望布局采用 Materialize 风格,因为整个网站都是基于 Materialize 主题设计的,我只想从表单中获取数据。但我不清楚具体该如何操作。

有没有人之前做过这件事?我正在尝试在 Buffalo 框架中创建一个表单。看起来一切正常,但我无法将标签的默认布局从 Bootstrap 切换到 Materialize。

我不确定该更改哪个参数以及如何更改。我通过更改渲染函数成功移除了标签的 Bootstrap 布局,但之后我又不知道如何插入类和标签,并将其转换为 Materialize 风格。

请帮帮我。如果你有…

以下是使用Buffalo框架和Pop库从PostgreSQL数据库查询数据并传递给Plush模板的完整实现示例。假设您已正确配置数据库连接。

首先,在actions/app.go中注册路由和处理函数:

// actions/app.go
func App() *buffalo.App {
    app := buffalo.New(buffalo.Options{})
    // ... 其他配置

    // 注册自定义查询路由
    app.GET("/custom-data", CustomDataHandler)

    return app
}

创建处理函数CustomDataHandler,执行自定义查询并将数据传递给模板:

// actions/handlers.go
package actions

import (
    "github.com/gobuffalo/buffalo"
    "github.com/gobuffalo/pop/v5"
    "your_app/models"  // 替换为您的实际模型路径
)

// 自定义数据结构(根据查询结果调整字段)
type CustomData struct {
    ID    int    `json:"id"`
    Name  string `json:"name"`
    Email string `json:"email"`
}

func CustomDataHandler(c buffalo.Context) error {
    tx, ok := c.Value("tx").(*pop.Connection)
    if !ok {
        return c.Error(500, errors.New("数据库连接失败"))
    }

    // 执行自定义查询(示例:查询users表)
    customData := []CustomData{}
    err := tx.RawQuery(`
        SELECT id, name, email 
        FROM users 
        WHERE active = ? 
        ORDER BY created_at DESC
    `, true).All(&customData)
    
    if err != nil {
        return c.Error(500, err)
    }

    // 将数据传递给模板
    c.Set("data", customData)
    return c.Render(200, r.HTML("custom-data.html"))
}

创建对应的Plush模板文件templates/custom-data.html

<!-- templates/custom-data.html -->
<table border="1">
    <thead>
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Email</th>
        </tr>
    </thead>
    <tbody>
        <%= for (item) in data { %>
            <tr>
                <td><%= item.ID %></td>
                <td><%= item.Name %></td>
                <td><%= item.Email %></td>
            </tr>
        <% } %>
    </tbody>
</table>

如果需要查询多个表,可以使用更复杂的SQL查询:

// 多表查询示例
type ComplexData struct {
    UserID    int    `json:"user_id"`
    UserName  string `json:"user_name"`
    OrderID   int    `json:"order_id"`
    Product   string `json:"product"`
}

func ComplexQueryHandler(c buffalo.Context) error {
    tx, ok := c.Value("tx").(*pop.Connection)
    if !ok {
        return c.Error(500, errors.New("数据库连接失败"))
    }

    data := []ComplexData{}
    err := tx.RawQuery(`
        SELECT u.id as user_id, u.name as user_name,
               o.id as order_id, p.name as product
        FROM users u
        INNER JOIN orders o ON u.id = o.user_id
        INNER JOIN products p ON o.product_id = p.id
        WHERE u.active = ?
    `, true).All(&data)
    
    if err != nil {
        return c.Error(500, err)
    }

    c.Set("data", data)
    return c.Render(200, r.HTML("complex-data.html"))
}

app.go中注册这个新路由:

app.GET("/complex-data", ComplexQueryHandler)

确保您的数据库连接在database.yml中正确配置,并且模型已正确定义。运行应用后访问/custom-data/complex-data即可查看结果。

回到顶部