• beego连接Mysql,生成WebAPI+CRUD和Swagger


    1.安装工具,下载 Go: https://studygolang.com/dl/golang/go1.13.6.windows-amd64.msi

    go get -u github.com/astaxie/beego
    go get -u github.com/beego/bee
    
    go get -u github.com/go-sql-driver/mysql

    2.创建项目(注:须进入创建的目录,如: cd beevue

    bee api beevue
    
    cd beevue

    3.创建DB,导入数据

    -- Dumping database structure for test
    CREATE DATABASE IF NOT EXISTS `test` /*!40100 DEFAULT CHARACTER SET utf8 */;
    USE `test`;
    
    -- Dumping structure for table test.langgen
    CREATE TABLE IF NOT EXISTS `langgen` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `pid` int(11) DEFAULT NULL,
      `langname` varchar(255) DEFAULT NULL,
      `module` varchar(255) DEFAULT NULL,
      `labelcode` varchar(255) DEFAULT NULL,
      `labelvalue` text,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;

    4.连接DB生成API

    bee generate appcode -driver="mysql" -conn= "root:root@tcp(127.0.0.1:3306)/test?charset=utf8"

    5.修改main.go,配置数据连接

    package main
    
    import (
        _ "beevue/routers"
        _ "github.com/go-sql-driver/mysql"
    
        "time"
        "github.com/astaxie/beego"
        "github.com/astaxie/beego/orm"
    )
    
    func init(){
        orm.RegisterDriver("mysql", orm.DRMySQL)
        orm.RegisterDataBase("default", "mysql", "root:root@tcp(127.0.0.1:3306)/iemes_v1?charset=utf8", 30, 30)
    
        orm.Debug = true
        orm.DefaultTimeLoc = time.UTC
    }
    
    func main() {
        if beego.BConfig.RunMode == "dev" {
            beego.BConfig.WebConfig.DirectoryIndex = true
            beego.BConfig.WebConfig.StaticDir["/swagger"] = "swagger"
        }
        beego.Run()
    }

    6.启动服务,浏览

    bee run -gendoc=true -downdoc=true
  • 相关阅读:
    django之认证权限和接口
    序列化组件
    django中cbv源码和restful规范
    AjaxControlToolKit--TabContainer控件的介绍收藏[摘录]
    sublime text 3 激活
    sublime 2激活和解决中文乱码
    sublime text2 保存文件时候名字后缀.dump问题解决
    选项卡 刷新回原来页面的处理方法:
    关于C#自定义控件【摘录】
    比较好的GridView固定问题(链接)
  • 原文地址:https://www.cnblogs.com/dzone/p/12215453.html
Copyright © 2020-2023  润新知