Revel does not come configured with a database or ORM interface. There are modules likeGORM that can be used to provide thisfunctionality. But ultimately it’s up to the developer what to use and how to use.

funcInitDB(){driver:=revel.Config.StringDefault("db.driver","mysql")connect_string:=revel.Config.StringDefault("db.connect","root:root@locahost/test")Db,err=sql.Open(driver,connect_string)….}

Example Db Setup

Create an InitDB() function in for example github.com/username/my-app/app/init.go.

  1. packageappimport("github.com/revel/revel"_"github.com/lib/pq""database/sql")varDB*sql.DBfuncInitDB(){connstring:=fmt.Sprintf("user=%s password='%s' dbname=%s sslmode=disable","user","pass","database")varerrerrorDB,err=sql.Open("postgres",connstring)iferr!=nil{revel.INFO.Println("DB Error",err)}revel.INFO.Println("DB Connected")}funcinit(){revel.Filters=[]revel.Filter{revel.PanicFilter,// Recover from panics and display an error page instead....snipped...revel.CompressFilter,// Compress the result.revel.ActionInvoker,// Invoke the action.}revel.OnAppStart(InitDB)...}

This can then be used in code

  1. import("github.com/username/my-app/app")funcGetStuff()MyStruct{sql:="SELECT id, name from my_table "rows,err:=app.DB.Query(sql)...dostuffhere...}
GoDoc Reference
GitHub Labels

原文: https://revel.github.io/manual/database.html