在Revel中使用Gorm查询表

I am giving Revel a try to build a small web app. I'm Having trouble though understanding how to Query a DB and display the result in the HTML View.

I Already have a PostgreSQL DB named "shop" with a table called "books". This Table contains the fields "id", "booknum", "bookname", "author" and "category".

I have managed to get a db connection and created the model struct. Now im lost as to how to create the function for a query ie*("SELECT * FROM books;") and display the result in the index view file.

With a clean revel build i have added the following code.

app/controller/app.go

package controllers

import (
    "github.com/revel/revel"
    "github.com/test/testapp/app/models"
)

type App struct {
    *revel.Controller
    GormController
}

func (c App) Index() revel.Result {
    books := models.Books{}
    return c.Render(books)
}

app/controller/gorm.go

package controllers

import (
    "github.com/jinzhu/gorm"
    _ "github.com/lib/pq" 
    r "github.com/revel/revel"
    "github.com/test/testapp/app/models"
)

var Gdb *gorm.DB

func initDB() {
  Gdb, err := gorm.Open("postgres", "host=localhost user=username dbname=shop sslmode=disable password=password")
  if err != nil {
   panic("failed to connect database")
 }
 Gdb.AutoMigrate(&models.Books{})
 }

 type GormController struct {
    *r.Controller
    Txn *gorm.DB
 }

app/controller/init.go

package controllers

import "github.com/revel/revel"

func init() {
    revel.OnAppStart(initDB) // invoke InitDB function before
 }

app/models/books.go

package models

type Sched struct {          // example user fields
    Id                   int64
    Booknum              string `sql:"type:VARCHAR(255)"`
    Bookname             string `sql:"type:VARCHAR(255)"`
    Author               string `sql:"type:VARCHAR(255)"`
    Category             string `sql:"type:VARCHAR(255)"`
}

Any feedbcak or help would be appreciated greatly.. Thanks

To start you can use this in your app.go file

result := c.DB.Find(books)
return c.RenderJSON(result)

the first line queries the database for all books (same as select * from books;) The 2nd returns JSON allowing you to verify you're getting the data you expect.

Next step is to take result and apply it to a template.