
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)) 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) 1 mmrx 2022 年 3 月 4 日 和 Gorm 相比如何? |
2 Joker123456789 OP |
3 qq1340691923 2022 年 3 月 4 日 已 star ,加油 |
4 zoharSoul 2022 年 3 月 4 日 感觉没有你发的那个 java 的舒服啊 |
5 FrankAdler 2022 年 3 月 4 日 那与 sqlx 相比呢 |
6 roy2220 2022 年 3 月 4 日 @FrankAdler 来看看 sqlz https://github.com/go-tk/sqlz |
7 FrankAdler 2022 年 3 月 5 日 @roy2220 #6 又杀出来一个,跟 sqlx 有啥区别,少了几个方法? |
8 roy2220 2022 年 3 月 5 日 @FrankAdler 轻量实现在 100 行代码以内,逻辑非常简单,没有使用反射 |
9 gowk 2022 年 3 月 5 日 |
10 aurtech 2022 年 3 月 10 日 坐标深圳,求一枚 Golang 大佬!!欢迎砸简历 V:Ifboredgunquxuexi. |
11 aurtech 2022 年 3 月 10 日 @FrankAdler 坐标深圳,求一枚 Golang 大佬!!欢迎砸简历 V:Ifboredgunquxuexi. |
12 luckyman 2022 年 5 月 10 日 via iPhone 个人感觉不如直接写 sql ,自己封装了反而不够灵活。如果仅仅为了提升开发效率可以用代码生成路线,我自己开发了一个有兴趣我可以发出来 |
13 Joker123456789 OP @luckyman 本来就是直接写 sql 啊 |