Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
dapeng committed May 10, 2024
1 parent a2383a1 commit 9f53326
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docs/.vuepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ module.exports = {
text: '支持配置文件',
link: '/zh/guide/config.md',
}, {
text: '数据库连接',
text: '操作数据库',
link: '/zh/guide/xorm.md',
}],
},
Expand Down
2 changes: 2 additions & 0 deletions docs/zh/guide/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ sidebar: auto

# 通过内置Goners支持配置文件

[[toc]]

在gone中提供了通过内置Goners读取配置文件的方法,配置文件格式暂时只支持`.properties`


Expand Down
1 change: 1 addition & 0 deletions docs/zh/guide/core-concept.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ sidebar: auto
# Gone的核心概念
“我们编写的代码,终究只是死物,除非他在**天国****复活**,为此我们需要将他**埋葬****墓园**。”

[[toc]]

## Goner(逝者)
在Gone框架中,最基本的定义和最核心的概念就是 Goner,**Goner 是指的匿名嵌入了 `gone.Flag` 的结构体**。举个例子:
Expand Down
2 changes: 2 additions & 0 deletions docs/zh/guide/goner-inject.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ sidebar: auto

# Gone支持哪些方式注入?

[[toc]]

## 共有属性注入 vs 私有属性注入
在Gone中,及支持共有属性注入也支持私有属性注入。依赖注入,一般情况依赖注入的“对象”都只应该局限于模块内部使用,基于“开放封闭”原则,我推荐使用私有属性注入。

Expand Down
2 changes: 2 additions & 0 deletions docs/zh/guide/innner-goner.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ sidebar: auto

# 如何优雅使用内置Goners?

[[toc]]

## 内置Goners

为了降低Gone的使用门槛,我们开发了一些[内置Goners](/zh/goners/#框架内置goners),用于提供一些基础的功能。
Expand Down
205 changes: 201 additions & 4 deletions docs/zh/guide/xorm.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,205 @@
sidebar: auto
---

# 通过内置Goners支持数据库连接
# 通过内置Goners支持数据库操作

::: tip
todo
:::
[[toc]]


## 以Mysql举个例子,源代码在[这里](https://github.com/gone-io/gone/tree/main/example/use-rdb)可以找到
### 1.准备mysql服务,可以使用docker-compose快速启动一个Mysql服务

docker-compose.yaml 内容如下:
```yaml
version: "3.5"

services:
mysql:
image: mysql:8.3
restart: unless-stopped
environment:
MYSQL_ROOT_PASSWORD: 123456
MYSQL_DATABASE: demo
volumes:
- ./data/mysql:/var/lib/mysql
ports:
- "3306:3306"
```
命令行执行下面代码,启动mysql服务:
```bash
docker-compose -f docker-compose.yaml up -d
```
### 2. 创建mod和配置
执行如下命令:
```bash
mkdir use-rdb
cd use-rdb

# 创建mod
go mod init use-rdb

# 创建数据库配置
mkdir config
cat >> config/default.properties<<eof
# 配置使用mysql数据库
database.driver-name=mysql
#数据源
database.dsn=root:123456@tcp(localhost:3306)/demo?charset=utf8mb4&loc=Local
eof
```

### 3. 编写代码如下:

文件名:main.go
```go
package main

import (
"fmt"
_ "github.com/go-sql-driver/mysql" #📢⚠️需要特别注意,需要引入mysql的驱动
"github.com/gone-io/gone"
"github.com/gone-io/gone/goner"
"github.com/gone-io/gone/goner/xorm" #📢⚠️需要特别注意,导入的是gone封装的xorm包
)

type Demo struct {
Id int64
Data string
}

type SqlExecutor struct {
gone.Flag
db xorm.Engine `gone:"gone-xorm"`
}

func (e *SqlExecutor) Execute() {

// 同步表结构
err := e.db.Sync(new(Demo))
if err != nil {
println(err.Error())
return
}

demo := Demo{Data: "hello gone"}

// 插入数据
_, err = e.db.Insert(
&demo,
Demo{Data: "The most Spring programmer-friendly Golang framework, dependency injection, integrates Web. "},
)

if err != nil {
println(err.Error())
return
}

fmt.Printf("demo: %v\n", demo)


var list []Demo

// 读取数据
err = e.db.Find(&list)
if err != nil {
println(err.Error())
return
}

fmt.Printf("demo records:%v\n", list)

}

func main() {
gone.
Prepare(func(cemetery gone.Cemetery) error {
// 通过 goner.XormPriest 将xorm相关的Goner埋葬到 cemetery
_ = goner.XormPriest(cemetery)

cemetery.Bury(&SqlExecutor{})
return nil
}).
// 在Gone启动完成后执行
AfterStart(func(in struct {
e SqlExecutor `gone:"*"`
}) {
in.e.Execute()
}).
Run()
}
```

### 3. 运行代码
执行如下命令:
```bash
go mod tidy
go run main.go
```

运行结果如下:
```log {18-19}
2024-05-10 16:56:52.767|INFO|Init|Revive github.com/gone-io/gone/heaven
2024-05-10 16:56:52.768|INFO|Init|Revive github.com/gone-io/gone/cemetery
2024-05-10 16:56:52.768|INFO|Init|Revive github.com/gone-io/gone/goner/tracer/tracer
2024-05-10 16:56:52.768|INFO|Init|Revive github.com/gone-io/gone/goner/logrus/logger
2024-05-10 16:56:52.768|INFO|Init|Revive github.com/gone-io/gone/goner/config/config
2024-05-10 16:56:52.768|INFO|Init|Revive github.com/gone-io/gone/goner/config/propertiesConfigure
2024-05-10 16:56:52.768|INFO|Init|Revive github.com/gone-io/gone/goner/logrus/logger
2024-05-10 16:56:52.768|INFO|Init|==>Use Env: local
2024-05-10 16:56:52.768|WARNING|Init|properties: /var/folders/jv/rn9b7nhs2ls1n1j_lqj005r80000gn/T/go-build1582606196/b001/exe/config/default.properties not found. skipping
2024-05-10 16:56:52.768|WARNING|Init|properties: /var/folders/jv/rn9b7nhs2ls1n1j_lqj005r80000gn/T/go-build1582606196/b001/exe/config/local.properties not found. skipping
2024-05-10 16:56:52.768|WARNING|Init|properties: /Users/jim/works/gone-io/gone/example/use-rdb/config/local.properties not found. skipping
2024-05-10 16:56:52.768|INFO|Init|Revive github.com/gone-io/gone/goner/config/config
2024-05-10 16:56:52.768|INFO|Init|Revive github.com/gone-io/gone/goner/config/propertiesConfigure
2024-05-10 16:56:52.768|INFO|Init|Revive github.com/gone-io/gone/goner/xorm/engine
2024-05-10 16:56:52.768|INFO|Init|Revive main/SqlExecutor
2024-05-10 16:56:52.769|INFO|/Users/jim/go/pkg/mod/xorm.io/[email protected]/log/logger_context.go:90||PING DATABASE mysql
2024-05-10 16:56:52.783|INFO|/Users/jim/works/gone-io/gone/cemetery.go:307||Revive /
demo:{1 hello gone}
demo records:[{1 hello gone} {2 The most Spring programmer-friendly Golang framework, dependency injection, integrates Web. }]
```

## 需要注意的点
### 1. `github.com/gone-io/gone/goner` 是对 `xorm.io/xorm` 的封装
封装的大概逻辑是:将xorm包装为一个[Angel Goner](https://goner.fun/zh/guide/core-concept.html#%F0%9F%98%87angel-%E5%A4%A9%E4%BD%BF);在Start方法中完成数据的连接;在Stop方法中关闭数据库连接。使用时不需要导入`xorm.io/xorm`包,只需要导入`github.com/gone-io/gone/goner`即可。封装中依赖了`github.com/gone-io/gone/tree/main/goner/config`来实现配置,在`goner.XormPriest`函数中已经自动完成了配置相关的Goner的[埋葬](https://goner.fun/zh/guide/core-concept.html#bury-%E5%9F%8B%E8%91%AC)
配置文件中支持的配置项:

- database.driver-name 驱动的名称,支持 mysql、postgres 等主流数据库,具体可以参考 [xorm文档](https://xorm.io/zh/docs/chapter-01/1.engine/)
- database.dsn 数据源,参考 [xorm文档](https://xorm.io/zh/docs/chapter-01/1.engine/)
- database.max-idle-count 连接池,最大空闲连接数
- database.max-open 连接池,最大连接数
- database.max-lifetime 最大生存时间
- database.showSql 是否打印执行的SQL

### 2. 通过`gone-xorm`**GonerId**注入
在需要使用xorm引擎的Goner中,使用`gone:"gone-xorm"`标签注入`xorm.Engine`,属性可以具名也可以匿名:
```go
type XormUser struct {
//具名
db xorm.Engine `gone:"gone-xorm"`
}

// 或者
type XormUser struct {
//匿名
xorm.Engine `gone:"gone-xorm"`
}
```
`xorm.Engine`中“继承”了`xorm.io/xorm.EngineInterface`接口,所以`xorm.Engine`支持[xorm文档](https://xorm.io/zh/docs/)中的所有方法。

### 3. 注意导入的包
- 需要导入数据库的驱动,不同数据库导入不一样:
```go
import (
_ "github.com/go-sql-driver/mysql"
)
```

- 导入的xorm包是:`github.com/gone-io/gone/goner/xorm`,不是`xorm.io/xorm`:
```go
import (
"github.com/gone-io/gone/goner/xorm" #📢⚠️需要特别注意,导入的是gone封装的xorm包
)
```

0 comments on commit 9f53326

Please sign in to comment.