-
-
Notifications
You must be signed in to change notification settings - Fork 98
/
GoMybatisTempleteDecoder_test.go
306 lines (275 loc) · 9.37 KB
/
GoMybatisTempleteDecoder_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package GoMybatis
import (
"fmt"
"github.com/zhuxiujia/GoMybatis/example"
"github.com/zhuxiujia/GoMybatis/stmt"
"github.com/zhuxiujia/GoMybatis/tx"
"testing"
"time"
)
type ExampleActivityMapper struct {
SelectTemplate func(name string, session Session) ([]example.Activity, error) `args:"name,session"`
InsertTemplate func(args []example.Activity, session Session) (int64, error) `args:"args,session"`
UpdateTemplate func(arg example.Activity, session Session) (int64, error) `args:"name,session"`
DeleteTemplate func(name string, session Session) (int64, error) `args:"name,session"`
}
//初始化mapper文件和结构体
var exampleActivityMapper = ExampleActivityMapper{}
func getMapper() ExampleActivityMapper {
initMapper()
return exampleActivityMapper
}
func initMapper() {
bytes := []byte(`<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://raw.githubusercontent.com/zhuxiujia/GoMybatis/master/mybatis-3-mapper.dtd">
<mapper>
<!--logic_delete_key 逻辑删除字段-->
<!--logic_deleted_value 逻辑删除已删除字段-->
<!--logic_undelete_value 逻辑删除 未删除字段-->
<!--version_key 乐观锁版本字段,支持int,int8,int16,int32,int64-->
<resultMap id="BaseResultMap">
<id column="id" langType="id"/>
<result column="name" property="name" langType="string"/>
<result column="pc_link" property="pcLink" langType="string"/>
<result column="h5_link" property="h5Link" langType="string"/>
<result column="remark" property="remark" langType="string"/>
<result column="version" property="version" langType="int" version_enable="true"/>
<result column="create_time" property="createTime" langType="time.Time"/>
<result column="delete_flag" property="deleteFlag" langType="int" logic_enable="true" logic_undelete="1" logic_deleted="0"/>
</resultMap>
<!--模板标签: columns wheres sets 支持逗号','分隔表达式,name?name = #{name}为判空表达式-->
<!--插入模板:默认id="insertTemplate,test="field != null",where自动设置逻辑删除字段" -->
<!--查询模板:默认id="selectTemplate,where自动设置逻辑删除字段-->
<!--更新模板:默认id="updateTemplate,set自动设置乐观锁版本号-->
<!--删除模板:默认id="deleteTemplate,where自动设置逻辑删除字段-->
<insertTemplate tables="biz_activity" />
<selectTemplate tables="biz_activity" wheres="name?name = #{name}" columns=""/>
<updateTemplate tables="biz_activity" sets="name?name = #{name}" wheres="name?name = #{name}"/>
<deleteTemplate tables="biz_activity" wheres="name?name = #{name}"/>
</mapper>
`)
var err error
var xmlItems = LoadMapperXml(bytes)
if xmlItems == nil {
panic(`Test_Load_Xml fail,LoadMapperXml "example/Example_ActivityMapper.xml"`)
}
var decoder = GoMybatisTemplateDecoder{}
err = decoder.DecodeTree(xmlItems, nil)
if err != nil {
panic(err)
}
var engine = GoMybatisEngine{}.New()
//mysql链接格式为 用户名:密码@(数据库链接地址:端口)/数据库名称 例如root:123456@(***.mysql.rds.aliyuncs.com:3306)/test
_, err = engine.Open("mysql", "root:123456@(localhost:3306)/test") //此处请按格式填写你的mysql链接,这里用*号代替
if err != nil {
panic(err)
}
engine.SetLogEnable(false)
engine.WriteMapperPtr(&exampleActivityMapper, bytes)
}
type TemplateSession struct {
Session
}
func (it *TemplateSession) Id() string {
return "sadf"
}
func (it *TemplateSession) Query(sqlorArgs string) ([]map[string][]byte, error) {
resultsSlice := make([]map[string][]byte, 0)
result := make(map[string][]byte)
result["name"] = []byte("活动1")
result["id"] = []byte("125")
result["pc_link"] = []byte("http://www.baidu.com")
result["h5_link"] = []byte("http://www.baidu.com")
result["remark"] = []byte("活动1")
resultsSlice = append(resultsSlice, result)
return resultsSlice, nil
}
func (it *TemplateSession) Exec(sqlorArgs string) (*Result, error) {
var result = Result{
LastInsertId: 1,
RowsAffected: 1,
}
return &result, nil
}
func (it *TemplateSession) QueryPrepare(sqlorArgs string, args ...interface{}) ([]map[string][]byte, error) {
return nil, nil
}
func (it *TemplateSession) ExecPrepare(sqlorArgs string, args ...interface{}) (*Result, error) {
var result = Result{
LastInsertId: 1,
RowsAffected: 1,
}
return &result, nil
}
func (it *TemplateSession) Rollback() error {
return nil
}
func (it *TemplateSession) Commit() error {
return nil
}
func (it *TemplateSession) Begin(p *tx.Propagation) error {
return nil
}
func (it *TemplateSession) Close() {
}
func (it *TemplateSession) StmtConvert() (stmt.StmtIndexConvert, error) {
return stmt.BuildStmtConvert("mysql")
}
type El struct {
Els []El
String string
test string
prefix string
suffix string
suffixOverrides string
separator string
collection string
item string
index string
open string
}
func Test_create_conf(t *testing.T) {
var els = []El{
{
test: "name != null",
String: "and name like #{pattern}",
},
{
test: "startTime != null",
String: "and startTime = #{startTime}",
},
{
String: "order by desc",
},
{
test: "page != null and size != null",
String: "limit #{page}, #{size}",
},
{
prefix: "(",
suffix: ")",
suffixOverrides: ",",
Els: []El{
{
test: "page != null and size != null",
String: "limit #{page}, #{size}",
},
},
},
{
open: "(",
collection: "ids",
},
}
fmt.Println(els[0].test)
}
func TestGoMybatisTemplateDecoder_Create(t *testing.T) {
var act = example.Activity{
Id: "123",
Uuid: "uu",
Name: "test",
PcLink: "pc",
H5Link: "h5",
Remark: "remark",
Version: 0,
CreateTime: time.Now(),
DeleteFlag: 1,
}
var args = []example.Activity{
act,
act,
act,
act,
act,
}
var session = TemplateSession{}
n, err := getMapper().InsertTemplate(args, &session)
if err != nil {
t.Fatal(err)
}
fmt.Println("updateNum", n)
time.Sleep(time.Second)
}
func TestGoMybatisTemplateDecoder_Select(t *testing.T) {
var session = TemplateSession{}
n, err := getMapper().SelectTemplate("test", &session)
if err != nil {
t.Fatal(err)
}
fmt.Println("updateNum", n)
time.Sleep(time.Second)
}
func TestGoMybatisTemplateDecoder_Update(t *testing.T) {
var act = example.Activity{
Id: "123",
Name: "test",
}
var session = TemplateSession{}
n, err := getMapper().UpdateTemplate(act, &session)
if err != nil {
t.Fatal(err)
}
fmt.Println("updateNum", n)
time.Sleep(time.Second)
}
func TestGoMybatisTemplateDecoder_Delete(t *testing.T) {
var session = TemplateSession{}
n, err := getMapper().DeleteTemplate("test", &session)
if err != nil {
t.Fatal(err)
}
fmt.Println("updateNum", n)
time.Sleep(time.Second)
}
func TestInit(t *testing.T) {
initMapperTest()
}
func initMapperTest() {
bytes := []byte(`<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://raw.githubusercontent.com/zhuxiujia/GoMybatis/master/mybatis-3-mapper.dtd">
<mapper>
<!--logic_delete_key 逻辑删除字段-->
<!--logic_deleted_value 逻辑删除已删除字段-->
<!--logic_undelete_value 逻辑删除 未删除字段-->
<!--version_key 乐观锁版本字段,支持int,int8,int16,int32,int64-->
<resultMap id="BaseResultMap">
<id column="id" langType="id"/>
<result column="name" property="name" langType="string"/>
<result column="pc_link" property="pcLink" langType="string"/>
<result column="h5_link" property="h5Link" langType="string"/>
<result column="remark" property="remark" langType="string"/>
<result column="version" property="version" langType="int" version_enable="true"/>
<result column="create_time" property="createTime" langType="time.Time"/>
</resultMap>
<!--模板标签: columns wheres sets 支持逗号','分隔表达式,name?name = #{name}为判空表达式-->
<!--插入模板:默认id="insertTemplate,test="field != null",where自动设置逻辑删除字段" -->
<!--查询模板:默认id="selectTemplate,where自动设置逻辑删除字段-->
<!--更新模板:默认id="updateTemplate,set自动设置乐观锁版本号-->
<!--删除模板:默认id="deleteTemplate,where自动设置逻辑删除字段-->
<insertTemplate tables="biz_activity" />
<selectTemplate tables="biz_activity" wheres="name?name = #{name}" columns=""/>
<updateTemplate tables="biz_activity" sets="name?name = #{name}" wheres="name?name = #{name}"/>
<deleteTemplate tables="biz_activity" wheres="name?name = #{name}"/>
</mapper>
`)
var err error
var xmlItems = LoadMapperXml(bytes)
if xmlItems == nil {
panic(`Test_Load_Xml fail,LoadMapperXml "example/Example_ActivityMapper.xml"`)
}
var decoder = GoMybatisTemplateDecoder{}
err = decoder.DecodeTree(xmlItems, nil)
if err != nil {
panic(err)
}
var engine = GoMybatisEngine{}.New()
//mysql链接格式为 用户名:密码@(数据库链接地址:端口)/数据库名称 例如root:123456@(***.mysql.rds.aliyuncs.com:3306)/test
_, err = engine.Open("mysql", "root:123456@(localhost:3306)/test") //此处请按格式填写你的mysql链接,这里用*号代替
if err != nil {
panic(err)
}
engine.SetLogEnable(false)
engine.WriteMapperPtr(&exampleActivityMapper, bytes)
}