Skip to content

Commit c177078

Browse files
committed
update Readme
1 parent 3e63403 commit c177078

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

README.md

+53-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
gobatis是一个golang的ORM框架,类似Java的Mybatis。支持直接执行sql语句以及动态sql。
88

9+
910
建议配合[gobatis-cmd](https://github.com/xfali/gobatis-cmd)自动代码、sql生成工具使用。
1011

1112
支持的动态sql标签:
@@ -19,6 +20,8 @@ include | 使用sql标签定义的语句替换。
1920
choose<br>when<br>otherwise | 有时我们不想应用到所有的条件语句,而只想从中择其一项。针对这种情况,gobatis 提供了 choose 元素,它有点像switch 语句。
2021
foreach | foreach 允许指定一个集合,声明可以在元素体内使用的集合项(item)和索引(index)变量。
2122

23+
除了xml之外,gobatis也支持使用go template的mapper格式。
24+
2225
## 待完成项
2326

2427
* 继续完善动态sql支持(trim)
@@ -235,11 +238,59 @@ gobatis.RegisterMapperFile(filePath)
235238
</mapper>
236239
```
237240

238-
### 8、gobatis-cmd生成文件使用示例
241+
### 8、template
242+
243+
gobatis也支持go template的sql解析及动态sql
244+
245+
1. 注册template
246+
247+
```
248+
gobatis.RegisterTemplateData([]byte(main_xml))
249+
```
250+
251+
252+
253+
```
254+
gobatis.RegisterTemplateFile(filePath)
255+
```
256+
257+
2. template示例
258+
259+
```
260+
{{define "selectTestTable"}}
261+
{{$COLUMNS := "id, username, password"}}
262+
SELECT {{$COLUMNS}} FROM test_table
263+
{{where (ne .Username "") "AND" "username" .Username "" | where (ne .Password "pw") "AND" "password" .Password}}
264+
{{end}}
265+
266+
{{define "insertTestTable"}}
267+
{{$COLUMNS := "id, username, password"}}
268+
INSERT INTO test_table ({{$COLUMNS}})
269+
VALUES(
270+
{{.Id}},
271+
'{{.Username}}',
272+
'{{.Password}}'
273+
)
274+
{{end}}
275+
276+
{{define "updateTestTable"}}
277+
UPDATE test_table
278+
{{set (ne .Username "") "username" .Username "" | set (ne .Password "") "password" .Password}}
279+
{{where (ne .Id 0) "AND" "id" .Id ""}}
280+
{{end}}
281+
282+
{{define "deleteTestTable"}}
283+
DELETE FROM test_table
284+
{{where (ne .Id 0) "AND" "id" .Id "" | where (ne .Username "") "AND" "username" .Username | where (ne .Password "") "AND" "password" .Password}}
285+
{{end}}
286+
```
287+
其中where和set是gobatis的自定义函数,用于智能的生成动态sql
288+
289+
### 9、gobatis-cmd生成文件使用示例
239290

240291
参考[cmd_test](https://github.com/xfali/gobatis/tree/master/test/cmd)
241292

242-
### 9、 SQL语句构建器
293+
### 10、 SQL语句构建器
243294

244295
gobatis xml特性有非常强大的动态SQL生成方案,当需要在代码中嵌入SQL语句时,也可使用SQL语句构建器:
245296
```

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ go 1.11
44

55
require (
66
github.com/go-sql-driver/mysql v1.4.1 // indirect
7-
github.com/lib/pq v1.3.0
7+
github.com/lib/pq v1.3.0 // indirect
88
github.com/mattn/go-sqlite3 v1.10.0 // indirect
99
)

0 commit comments

Comments
 (0)