|
|
|
@ -0,0 +1,116 @@ |
|
|
|
GoFrame 标准结构 |
|
|
|
api/ dto 包 + 接口定义 定义前后端交互的接口规范(入参 / 出参) |
|
|
|
internal/ src/main/java 核心业务代码(Controller/Service/DAO 全在这) |
|
|
|
manifest/ resources/application.yml 项目配置文件(数据库 / 端口 / 环境配置) |
|
|
|
resource/ resources/static + templates 静态资源(前端页面 / CSS / 图片) |
|
|
|
utility/ utils 工具包 通用工具函数(加密 / 时间 / 字符串处理) |
|
|
|
hack/ script 脚本目录 编译 / CI/CD 辅助脚本(非业务代码) |
|
|
|
|
|
|
|
# 生成名为「gf_demo」的 GoFrame 项目(-u 强制拉取最新模板) |
|
|
|
gf init gf_demo -u |
|
|
|
数据库go连接 |
|
|
|
# 服务端口配置 |
|
|
|
server: |
|
|
|
address: ":8000" |
|
|
|
|
|
|
|
# 数据库配置(重点!) |
|
|
|
database: |
|
|
|
default: |
|
|
|
# 连接串格式:mysql:账号:密码@tcp(IP:端口)/数据库名?参数1&参数2 |
|
|
|
link: "mysql:root:root@tcp(127.0.0.1:3306)/gf_demo?charset=utf8mb4&parseTime=True&loc=Local" |
|
|
|
debug: true # 开启调试,控制台会打印执行的SQL(新手必备) |
|
|
|
maxIdle: 10 # 连接池最大空闲连接数 |
|
|
|
maxOpen: 100 # 连接池最大活跃连接数 |
|
|
|
package main |
|
|
|
|
|
|
|
import ( |
|
|
|
"context" |
|
|
|
// 1. 导入 MySQL 驱动(下划线表示只初始化,不直接调用) |
|
|
|
_ "github.com/gogf/gf/v2/contrib/drivers/mysql" |
|
|
|
// 2. 导入 GoFrame 核心包 |
|
|
|
"github.com/gogf/gf/v2/frame/g" |
|
|
|
"github.com/gogf/gf/v2/os/gctx" |
|
|
|
) |
|
|
|
|
|
|
|
func main() { |
|
|
|
// 初始化上下文(GoFrame 所有操作都需要 ctx) |
|
|
|
ctx := gctx.New() |
|
|
|
|
|
|
|
// 测试数据库连接(核心代码) |
|
|
|
testDBConnection(ctx) |
|
|
|
|
|
|
|
// 启动 GoFrame 服务(Web 项目必备) |
|
|
|
g.Server().Run() |
|
|
|
} |
|
|
|
|
|
|
|
// 定义和表对应的结构体(db标签映射数据库字段) |
|
|
|
type User struct { |
|
|
|
UserName string `json:"user_name" db:"user_name"` |
|
|
|
Password string `json:"pwd" db:"pwd"` |
|
|
|
Age int `json:"age" db:"age"` |
|
|
|
} |
|
|
|
|
|
|
|
// 新增用户 |
|
|
|
func addUser(ctx context.Context) { |
|
|
|
db := g.DB() |
|
|
|
// Insert():插入数据,支持结构体/Map |
|
|
|
result, err := db.Model("user").Ctx(ctx).Insert(User{ |
|
|
|
UserName: "小明", |
|
|
|
Password: "123456", |
|
|
|
Age: 18, |
|
|
|
}) |
|
|
|
if err != nil { |
|
|
|
g.Log().Error(ctx, "新增失败:", err) |
|
|
|
return |
|
|
|
} |
|
|
|
g.Log().Info(ctx, "新增成功,用户ID:", result.LastInsertId()) |
|
|
|
} |
|
|
|
// 查询所有用户 |
|
|
|
func queryUsers(ctx context.Context) { |
|
|
|
db := g.DB() |
|
|
|
var users []User |
|
|
|
// Scan():把查询结果赋值给切片 |
|
|
|
err := db.Model("user").Ctx(ctx).Scan(&users) |
|
|
|
if err != nil { |
|
|
|
g.Log().Error(ctx, "查询失败:", err) |
|
|
|
return |
|
|
|
} |
|
|
|
g.Log().Info(ctx, "查询到用户:", users) |
|
|
|
} |
|
|
|
// 条件查询(比如查年龄=18的用户) |
|
|
|
func queryUserByAge(ctx context.Context) { |
|
|
|
db := g.DB() |
|
|
|
var user User |
|
|
|
// Where():添加查询条件 |
|
|
|
err := db.Model("user").Ctx(ctx).Where("age", 18).Scan(&user) |
|
|
|
if err != nil { |
|
|
|
g.Log().Error(ctx, "条件查询失败:", err) |
|
|
|
return |
|
|
|
} |
|
|
|
g.Log().Info(ctx, "年龄18的用户:", user) |
|
|
|
} |
|
|
|
// 修改用户年龄 |
|
|
|
func updateUser(ctx context.Context) { |
|
|
|
db := g.DB() |
|
|
|
// Update():修改数据,参数是 Map(键=数据库字段) |
|
|
|
result, err := db.Model("user").Ctx(ctx). |
|
|
|
Where("user_name", "小明"). // 条件:用户名=小明 |
|
|
|
Update(g.Map{"age": 20}) // 修改:年龄=20 |
|
|
|
if err != nil { |
|
|
|
g.Log().Error(ctx, "修改失败:", err) |
|
|
|
return |
|
|
|
} |
|
|
|
g.Log().Info(ctx, "修改成功,影响行数:", result.RowsAffected()) |
|
|
|
} |
|
|
|
// 删除用户 |
|
|
|
func deleteUser(ctx context.Context) { |
|
|
|
db := g.DB() |
|
|
|
result, err := db.Model("user").Ctx(ctx). |
|
|
|
Where("user_name", "小明"). |
|
|
|
Delete() |
|
|
|
if err != nil { |
|
|
|
g.Log().Error(ctx, "删除失败:", err) |
|
|
|
return |
|
|
|
} |
|
|
|
g.Log().Info(ctx, "删除成功,影响行数:", result.RowsAffected()) |
|
|
|
} |