您现在的位置是:首页 > 文章详情

Gdao v1.2.0:Go 语言高效 ORM 框架

日期:2024-09-27点击:149
gdao是一个全面的go持久层解决方案。主要目的在于 减少编程量,提高生产力,提高性能,支持多数据源整合操作,支持数据读写分离,制定持久层编程规范。 灵活运用 gdao,可以在持久层设计上,减少30%甚至50%以上的编程量,同时形成持久层的统一编程规范,减少持久层错误,同时易于维护和扩展。   gdao对于go语言,相当于 hibernate+ myBatis对于java语言
gdao完整地在go语言中实现 myBatis的核心功能,实现SQL与程序分离,实现强大的动态SQL功能

GitHub :  Gdao Repository

示例程序:  Gdaodemo

使用文档:  Gdaodoc

 

同类型框架性能压测 对比

性能压测数据《Gdao—orm框架性能压测 gdao+gorm+sqlx+原生sql执行》


特点

  • 规范性:用标准化结构体统一映射表增删改查操作
  • 高效性:比直接调用驱动执行SQL的性能更高
  • 丰富动态SQL支持:在go语言上实现了myBatis 映射核心功能
  • 实用性:支持读写分离,数据缓存等高级orm功能,支持高效序列化

主要功能

  1. 生成代码:运行gdao代码生成工具,创建数据库表的标准化实体类。类似thrift/protobuf
  2. 高效序列化:表的标准化实体类实现了高效的序列化与反序列化。性能更高,数据体积更小。
  3. 读写分离:gdao支持绑定多个备库数据源,并对绑定的表或SQL实现数据读写分离
  4. 数据缓存:gdao支持数据缓存,并支持对缓存数据时效,与数据回收等特性进行细致控制
  5. 广泛兼容性:gdao理论上支持所有实现 go 数据库驱动接口的数据库
  6. 高级特性:支持事务,存储过程,批处理等数据库操作
  7. 动态SQL:gdao实现丰富的动态SQL构建功能。支持动态SQL标签映射构建与原生SQL动态构建等多种模式。
  8. myBatis特性:Gdao的映射模块是myBatis在go上的实现。也是所知的go orm中,唯一支持SQL与程序分离的orm。

快速入门

1. 安装

 go get github.com/donnie4w/gdao

2. 配置数据源

 gdao.Init(mysqldb,gdao.MYSQL) //gdao初始化数据源 mysqldb 为sql.DB对象 , gdao.MYSQL 为数据库类型

3. 标准化结构体操作

 // 读取 hs := dao.NewHstest() hs.Where(hs.Id.EQ(10)) h, err := hs.Select(hs.Id, hs.Value, hs.Rowname) //[DEBUG][SELETE ONE][ select id,value,rowname from hstest where id=?][10] 
 // 更新 hs := dao.NewHstest() hs.SetRowname("hello10") hs.Where(hs.Id.EQ(10)) hs.Update() //[DEBUG][UPDATE][update hstest set rowname=? where id=?][hello10 10]
 // 删除 hs := dao.NewHstest() hs.Where(hs.Id.EQ(10)) hs.delete() //[DEBUG][UPDATE][delete from hstest where id=?][10]
 //新增 hs := dao.NewHstest() hs.SetValue("hello123") hs.SetLevel(12345) hs.SetBody([]byte("hello")) hs.SetRowname("hello1234") hs.SetUpdatetime(time.Now()) hs.Insert() //[DEBUG][INSERT][insert into hstest(floa,age,value,level,body,rowname,updatetime )values(?,?,?,?,?)][hello123 12345 hello hello1234 2024-07-17 19:36:44]

4. 原生SQL操作

 //查询,返回单条 gdao是原生SQL操作入口 gdao.ExecuteQueryBean("select id,value,rowname from hstest where id=?", 10) //insert gdao.ExecuteUpdate("insert into hstest2(rowname,value) values(?,?)", "helloWorld", "123456789"); //update gdao.ExecuteUpdate("update hstest set value=? where id=1", "hello"); //delete gdao.ExecuteUpdate("delete from hstest where id = ?", 1);

5. 配置缓存

 //绑定Hstest 启用缓存, 缓存默认时效为300秒, gdaoCache为缓存操作入口 gdaoCache.BindClass[dao.Hstest]()

6. 读写分离

 mysqldb := getDataSource("mysql.json") // 获取备库数据源:mysqldb gdaoSlave.BindClass[dao.Hstest](mysqldb, gdao.MYSQL) //这里主数据库为sqlite,备数据库为mysql,Hstest读取数据源为mysql, gdaoSlave为读写分离操作入口

7. SQL映射

 <!-- MyBatis 风格的 XML 配置文件 --> <mapper namespace="user"> <select id="selectHstest1" parameterType="int64" resultType="hstest1"> SELECT * FROM hstest1 order by id desc limit #{limit} </select> </mapper>
 //读取解析xml配置 hs1, _ := gdaoMapper.Select[dao.Hstest1]("user.selectHstest1", 1) fmt.Println(hs1)

8. 动态SQL映射

  <!-- where if --> <select id="demo1" resultType="Hstest"> SELECT * FROM hstest <where> <if test="rowname== 'hello'"> and rowname = #{rowname} </if> <if test="id >0"> AND id = #{id} </if> </where> </select>
  hs := dao.NewHstest() hs.SetId(12) gdaoMapper.SelectBean("dynamic.demo1", hs) //执行映射id [SELECT * FROM hstest WHERE id = ?]ARGS[12] 

9. SqlBuilder 动态SQL

 func Test_sqlBuilder_if(t *testing.T) { context := map[string]any{"id": 12} builder := &SqlBuilder{} builder.Append("SELECT * FROM hstest where 1=1"). AppendIf("id>0", context, "and id=?", context["id"]) //动态SQL,当id>0时,动态添加 and id=? bean := builder.SelectOne() //查询SQL: SELECT * FROM hstest where 1=1 and id=? [ARGS][12] logger.Debug(bean) }
原文链接:https://www.oschina.net/news/313754/gdao-1-2-0-released
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章