# gobatis **Repository Path**: gitxuchang/gobatis ## Basic Information - **Project Name**: gobatis - **Description**: Golang 关系数据库自动映射框架 - **Primary Language**: Go - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: https://go-aurora-engine.github.io/orm/sgo.html - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 21 - **Created**: 2022-12-13 - **Last Updated**: 2022-12-13 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # GoBatis [![Go Report Card](https://goreportcard.com/badge/gitee.com/aurora-engine/sgo)](https://goreportcard.com/report/gitee.com/aurora-engine/sgo)
## version ```shell go1.19 ``` `GoBatis` 是参考 `MyBatis` 编写的sql标签解析,`GoBatis`仅提供对 mapper 的上下文数据解析填充,并不保证对 sql 语句的语法检查。 ## XML 解析规则 `GoBatis` 解析 xml 文件中的sql语句,会严格检查上下文中的数据类型,字符串类型参数会自定添加 ` '' ` 单引号,其他基础数据类型不会添加,对于复杂数据结构(复合结构,泛型结构体等)会持续跟进 ,目前仅支持基础数据类型。 ### 上下文数据 上下文数据是由用户调用时候传递接,仅接受 map 或者结构体如下: ### 标签详情 |标签|描述|功能| |:-|:-|:-| |``|根节点|| |``|insert语句|生成插入语句| |` select * from student where sss={name} and {obj} and abc = 1 and 1=1 or 1!=1 or cba=1 or name = {name} and 1=1 ``` ### xml解析 #### 第一层 ``标签是整个xml的根 `namespace` 属性定义了 xml的标识符,调用阶段 `namespace`的属性至关重要 #### 第二层 ` select * from comm_user where user_id={id} ``` 创建的 xml 文件 `` `namespace` 属性一定要和 Mapper 结构体的名称一样,区分大小写,` select * from student where id={id} ``` ```go package main import ( "database/sql" "fmt" "gitee.com/aurora-engine/gobatis" _ "github.com/go-sql-driver/mysql" "time" ) type Student struct { Id string `column:"id"` Name string `column:"name"` Age int `column:"age"` CreateTime string `column:"create_time"` } type StudentMapper struct { InsertOne func(any) (int64, error) InsertArr func(any) (int64, error) SelectById func(any) (Student, error) } func main() { ctx := map[string]any{ "arr": []map[string]any{ { "id": "1", "name": "test1", "age": 19, "time": time.Now().Format("2006-01-02 15:04:05"), }, { "id": "2", "name": "test2", "age": 19, "time": time.Now().Format("2006-01-02 15:04:05"), }, { "id": "3", "name": "test3", "age": 19, "time": time.Now().Format("2006-01-02 15:04:05"), }, }, "id": "1", "ids": []string{"1", "2"}, } open, err := sql.Open("mysql", "xxxx") if err != nil { fmt.Println(err.Error()) return } build := gobatis.New(open) build.Source("/") mapper := &StudentMapper{} build.ScanMappers(mapper) stu, err := mapper.SelectById(ctx) if err != nil { fmt.Println(err.Error()) return } fmt.Println(stu) } ``` ### 查询多条数据 ```go type StudentMapper struct { InsertOne func(any) (int64, error) InsertArr func(any) (int64, error) SelectById func(any) (Student, error) SelectAll func() ([]Student, error) } ``` ```xml ``` ```go package main import ( "database/sql" "fmt" "gitee.com/aurora-engine/gobatis" _ "github.com/go-sql-driver/mysql" "time" ) type Student struct { Id string `column:"id"` Name string `column:"name"` Age int `column:"age"` CreateTime string `column:"create_time"` } type StudentMapper struct { InsertOne func(any) (int64, error) InsertArr func(any) (int64, error) SelectById func(any) (Student, error) SelectAll func() ([]Student, error) } func main() { open, err := sql.Open("mysql", "xxxx") if err != nil { fmt.Println(err.Error()) return } build := gobatis.New(open) build.Source("/") mapper := &StudentMapper{} build.ScanMappers(mapper) stu, err := mapper.SelectAll() if err != nil { fmt.Println(err.Error()) return } fmt.Println(stu) } ``` ### 批量查询 ```go type StudentMapper struct { InsertOne func(any) (int64, error) InsertArr func(any) (int64, error) SelectById func(any) (Student, error) SelectAll func() ([]Student, error) SelectByIds func(any) ([]Student, error) } ``` ```xml ``` ```go package main import ( "database/sql" "fmt" "gitee.com/aurora-engine/gobatis" _ "github.com/go-sql-driver/mysql" "time" ) type Student struct { Id string `column:"id"` Name string `column:"name"` Age int `column:"age"` CreateTime string `column:"create_time"` } type StudentMapper struct { InsertOne func(any) (int64, error) InsertArr func(any) (int64, error) SelectById func(any) (Student, error) SelectAll func() ([]Student, error) SelectByIds func(any) ([]Student, error) } func main() { ctx := map[string]any{ "arr": []map[string]any{ { "id": "1", "name": "test1", "age": 19, "time": time.Now().Format("2006-01-02 15:04:05"), }, { "id": "2", "name": "test2", "age": 19, "time": time.Now().Format("2006-01-02 15:04:05"), }, { "id": "3", "name": "test3", "age": 19, "time": time.Now().Format("2006-01-02 15:04:05"), }, }, "id": "1", "ids": []string{"1", "2"}, } open, err := sql.Open("mysql", "xxxxxxx") if err != nil { fmt.Println(err.Error()) return } build := gobatis.New(open) build.Source("/") mapper := &StudentMapper{} build.ScanMappers(mapper) stu, err := mapper.SelectByIds(ctx) if err != nil { fmt.Println(err.Error()) return } fmt.Println(stu) } ``` ## Update 同上... ## Delete 同上...