beego从拿不起来到彻底放弃——使用Gorm连接PostgreSQL

  1. 在beego的conf中添加数据库连接配置
# pg数据库配置
#连接数据库账号
 pg_user = "postgres"
#连接数据库密码
 pg_password = "123456"
#连接数据库的ip
 pg_host = "127.0.0.1"
#数据库的端口
 pg_port = "5432"
#连接的数据库名
 pg_db = "RecruitStudents"
#设置最大打开的连接数,默认值为0表示不限制
 pg_max_open_conns = 1000
#用于设置闲置的连接数。
 pg_max_idle_conns = 20

2.要连接到数据库首先要导入驱动程序。

import _ "github.com/jinzhu/gorm/dialects/postgres"

3.在models中连接pg数据库


package models

import (
	"github.com/astaxie/beego"
	"time"

	"github.com/jinzhu/gorm"
	_"github.com/jinzhu/gorm/dialects/postgres"
	//"time"
)
//声明orm.Ormer 全局使用
var (Db *gorm.DB)

type Photo struct {
   
	Id			int        `gorm:"column:id"` // ID字段映射 
	XueHao      string
	PhotoURL    string
	Leixing     string
	is_del      int
	memo        string
}
func (u *Photo) TableName() string {
    //表名映射
	
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值