V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
Joker123456789
V2EX  ›  Go 编程语言

Go 开发的 数据库操作框架

  •  
  •   Joker123456789 · 2022-03-04 17:04:57 +08:00 · 2647 次点击
    这是一个创建于 755 天前的主题,其中的信息可能已经有所发展或是发生改变。

    项目简介

    • 项目名:Beerus-DB
    • 开发语言:golang

    Beerus-DB 是 Beerus 的一个数据库操作组件,支持多数据源,连接池管理,单表操作不需要写 sql ,查询结果可以映射到 struct ,废话不多说,直接上示例。

    单表操作

    根据条件查询单表数据

    conditions := builder.Create().
        Add("id > ?", 10).
        Add("and (user_name = ? or age > ?)", "bee", 18).
        Add("order by create_time desc", entity.NotWhere).
        Build()
    
    resultMap, err := operation.GetDBTemplate("Data source name").Select("table name", conditions)
    

    根据条件修改单表数据

    // 条件设定
    conditions := builder.Create().
        Add("id = ?", 1).
        Build()
    
    // 要修改的数据设定
    data := ResultStruct{UserName: "TestNoSqlUpdate"}
    
    // 执行修改操作
    result, err := operation.GetDBTemplate("Data source name").Update("table name", dbutil.StructToMapIgnore(&data, true),conditions)
    

    根据条件删除单表数据

    // 设定删除条件
    conditions := builder.Create().
        Add("id = ?", 2).
        Build()
    
    // 执行删除操作
    _, err := operation.GetDBTemplate("Data source name").Delete("table name", conditions)
    

    插入一条数据

    data := ResultStruct{
        UserName: "TestNoSqlInsert",
        UserEmail: "[email protected]",
        UpdateTime: "2021-12-09 13:50:00",
    }
    
    result, err := operation.GetDBTemplate("Data source name").Insert("table name", dbutil.StructToMapIgnore(&data, true))
    

    自定义 sql 操作

    根据数组参数查询

    param := make([]interface{}, 1)
    param[0] = 1
    
    // 查多条
    resultMap, err := operation.GetDBTemplate("Data source name").SelectList("select * from xt_message_board where id = ?", param)
    
    // 查一条
    resultMap, err := operation.GetDBTemplate("Data source name").SelectOne("select * from xt_message_board where id = ?", param)
    

    分页查询

    data := ResultStruct{
        UserName: "TestNoSqlInsert",
        UserEmail: "[email protected]",
    }
    
    // 创建分页参数
    param := entity.PageParam{
        CurrentPage: 1,  // 第几页
        PageSize: 20,  // 每页多少条
        Params: dbutil.StructToMap(&data), // 查询参数
    }
    
    // 执行查询操作
    result, err := operation.GetDBTemplate("Data source name").SelectPage("select * from xt_message_board where user_name = {user_name} and user_email = {user_email}", param)
    

    事务管理

    // 开启事务
    id, err := db.Transaction()
    if err != nil {
        t.Error("TestUpdateTx: " + err.Error())
        return
    }
    
    res := ResultStruct{Id: 1, UserName: "TestUpdateTx"}
    
    // 注:这里使用的不是 GetDBTemplate ,ExecByMap ,而是 GetDBTemplateTx 和 ExecByTxMap
    // 使用事务和不使用事务,在调用的函数上,区别就是多了个 Tx
    ss, err := operation.GetDBTemplateTx(id, "dbPoolTest").ExecByTxMap("update xt_message_board set user_name = {user_name} where id = {id}", dbutil.StructToMap(&res))
    
    if err != nil {
        // 如果有问题就回滚事务
        db.Rollback(id)
        t.Error("TestUpdateTx: " + err.Error())
        return
    }
    
    // 提交事务
    db.Commit(id)
    

    可以访问官网了解更多

    https://beeruscc.com/cn/

    13 条回复    2022-05-10 17:26:38 +08:00
    mmrx
        1
    mmrx  
       2022-03-04 17:16:35 +08:00
    和 Gorm 相比如何?
    Joker123456789
        2
    Joker123456789  
    OP
       2022-03-04 17:28:45 +08:00   ❤️ 1
    @mmrx 不敢跟 Gorm 比较,毕竟那是一个团队开发了很久的,而我这个框架 年龄还不到 1 岁。

    Beerus-DB 是主打 小型,快速开发的,不是 ORM 框架,主要还是以写 SQL 为主
    qq1340691923
        3
    qq1340691923  
       2022-03-04 17:43:30 +08:00
    已 star ,加油
    zoharSoul
        4
    zoharSoul  
       2022-03-04 18:03:52 +08:00
    感觉没有你发的那个 java 的舒服啊
    FrankAdler
        5
    FrankAdler  
       2022-03-04 21:10:10 +08:00
    那与 sqlx 相比呢
    roy2220
        6
    roy2220  
       2022-03-04 23:12:00 +08:00
    FrankAdler
        7
    FrankAdler  
       2022-03-05 00:51:21 +08:00
    @roy2220 #6 又杀出来一个,跟 sqlx 有啥区别,少了几个方法?
    roy2220
        8
    roy2220  
       2022-03-05 10:48:18 +08:00
    @FrankAdler 轻量——实现在 100 行代码以内,逻辑非常简单,没有使用反射
    gowk
        9
    gowk  
       2022-03-05 11:00:08 +08:00
    aurtech
        10
    aurtech  
       2022-03-10 17:51:31 +08:00
    坐标深圳,求一枚 Golang 大佬!!欢迎砸简历 V:Ifboredgunquxuexi.
    aurtech
        11
    aurtech  
       2022-03-10 18:13:54 +08:00
    @FrankAdler 坐标深圳,求一枚 Golang 大佬!!欢迎砸简历 V:Ifboredgunquxuexi.
    luckyman
        12
    luckyman  
       2022-05-10 11:25:41 +08:00 via iPhone
    个人感觉不如直接写 sql ,自己封装了反而不够灵活。如果仅仅为了提升开发效率可以用代码生成路线,我自己开发了一个有兴趣我可以发出来
    Joker123456789
        13
    Joker123456789  
    OP
       2022-05-10 17:26:38 +08:00
    @luckyman 本来就是直接写 sql 啊
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3310 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 13:20 · PVG 21:20 · LAX 06:20 · JFK 09:20
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.