Skip to content

Commit 83b8a82

Browse files
committed
增加namespace支持,注意:所有关联id都必须增加"namespace."否则可能无法找到该sql。如果不需要namespace,请将mapper文件namespace置为空
1 parent 64ca826 commit 83b8a82

File tree

10 files changed

+159
-80
lines changed

10 files changed

+159
-80
lines changed

README.md

+52-24
Original file line numberDiff line numberDiff line change
@@ -211,60 +211,76 @@ gobatis.RegisterMapperFile(filePath)
211211
2. xml示例
212212

213213
```
214-
<mapper namespace="test_package.TestTable">
215-
<sql id="columns_id">`id`,`username`,`password`,`update_time`</sql>
214+
<mapper namespace="test">
215+
<sql id="columns_id">id,username,password,createtime</sql>
216216
217217
<select id="selectTestTable">
218-
SELECT <include refid="columns_id"> </include> FROM `TEST_TABLE`
218+
SELECT <include refid="columns_id"> </include> FROM test_table
219219
<where>
220-
<if test="{TestTable.id} != nil and {TestTable.id} != 0">AND `id` = #{TestTable.id} </if>
221-
<if test="{TestTable.username} != nil">AND `username` = #{TestTable.username} </if>
222-
<if test="{TestTable.password} != nil">AND `password` = #{TestTable.password} </if>
223-
<if test="{TestTable.update_time} != nil">AND `update_time` = #{TestTable.update_time} </if>
220+
<if test="{TestTable.id} != nil and {TestTable.id} != 0">AND id = #{TestTable.id} </if>
221+
<if test="{TestTable.username} != nil">AND username = #{TestTable.username} </if>
222+
<if test="{TestTable.password} != nil">AND password = #{TestTable.password} </if>
223+
<if test="{TestTable.createtime} != nil">AND createtime = #{TestTable.createtime} </if>
224224
</where>
225225
</select>
226226
227227
<select id="selectTestTableCount">
228-
SELECT COUNT(*) FROM `TEST_TABLE`
228+
SELECT COUNT(*) FROM test_table
229229
<where>
230-
<if test="{TestTable.id} != nil and {TestTable.id} != 0">AND `id` = #{TestTable.id} </if>
231-
<if test="{TestTable.username} != nil">AND `username` = #{TestTable.username} </if>
232-
<if test="{TestTable.password} != nil">AND `password` = #{TestTable.password} </if>
233-
<if test="{TestTable.update_time} != nil">AND `update_time` = #{TestTable.update_time} </if>
230+
<if test="{TestTable.id} != nil and {TestTable.id} != 0">AND id = #{TestTable.id} </if>
231+
<if test="{TestTable.username} != nil">AND username = #{TestTable.username} </if>
232+
<if test="{TestTable.password} != nil">AND password = #{TestTable.password} </if>
233+
<if test="{TestTable.createtime} != nil">AND createtime = #{TestTable.createtime} </if>
234234
</where>
235235
</select>
236236
237237
<insert id="insertTestTable">
238-
INSERT INTO `TEST_TABLE` (`id`,`username`,`password`,`update_time`)
238+
INSERT INTO test_table (id,username,password,createtime)
239239
VALUES(
240240
#{TestTable.id},
241241
#{TestTable.username},
242242
#{TestTable.password},
243-
#{TestTable.update_time}
243+
#{TestTable.createtime}
244244
)
245245
</insert>
246246
247+
<insert id="insertBatchTestTable">
248+
INSERT INTO test_table (id,username,password,createtime)
249+
VALUES
250+
<foreach item="item" index="index" collection="{0}" open="" separator="," close="">
251+
(#{item.TestTable.id},#{item.TestTable.username},#{item.TestTable.password},#{item.TestTable.createtime})
252+
</foreach>
253+
</insert>
254+
247255
<update id="updateTestTable">
248-
UPDATE `TEST_TABLE`
256+
UPDATE test_table
249257
<set>
250-
<if test="{TestTable.username} != nil"> `username` = #{TestTable.username} </if>
251-
<if test="{TestTable.password} != nil"> `password` = #{TestTable.password} </if>
252-
<if test="{TestTable.update_time} != nil"> `update_time` = #{TestTable.update_time} </if>
258+
<if test="{TestTable.username} != nil"> username = #{TestTable.username} </if>
259+
<if test="{TestTable.password} != nil"> password = #{TestTable.password} </if>
260+
<if test="{TestTable.createtime} != nil"> createtime = #{TestTable.createtime} </if>
253261
</set>
254-
WHERE `id` = #{TestTable.id}
262+
WHERE id = #{TestTable.id}
255263
</update>
256264
257265
<delete id="deleteTestTable">
258-
DELETE FROM `TEST_TABLE`
266+
DELETE FROM test_table
259267
<where>
260-
<if test="{TestTable.id} != nil and {TestTable.id} != 0">AND `id` = #{TestTable.id} </if>
261-
<if test="{TestTable.username} != nil">AND `username` = #{TestTable.username} </if>
262-
<if test="{TestTable.password} != nil">AND `password` = #{TestTable.password} </if>
263-
<if test="{TestTable.update_time} != nil">AND `update_time` = #{TestTable.update_time} </if>
268+
<if test="{TestTable.id} != nil and {TestTable.id} != 0">AND id = #{TestTable.id} </if>
269+
<if test="{TestTable.username} != nil">AND username = #{TestTable.username} </if>
270+
<if test="{TestTable.password} != nil">AND password = #{TestTable.password} </if>
271+
<if test="{TestTable.createtime} != nil">AND createtime = #{TestTable.createtime} </if>
264272
</where>
265273
</delete>
266274
</mapper>
267275
```
276+
3. namespace
277+
278+
xml数据或文件注册之后,session参数sqlid与xml action对应关系为:${NAMESPACE}+"."+${ACTION_ID}
279+
280+
以2中的xml为例,调用select的方式为:
281+
```cassandraql
282+
sess.Select("test.selectTestTable").Param(model).Result(&dataList)
283+
```
268284

269285
### 8、template
270286

@@ -285,6 +301,8 @@ gobatis.RegisterTemplateFile(filePath)
285301
2. template示例
286302

287303
```
304+
{{define "namespace"}}test{{end}}
305+
288306
{{define "selectTestTable"}}
289307
SELECT id,username,password,createtime FROM test_table
290308
{{where .Id "AND" "id = " (arg .Id) "" | where .Username "AND" "username = " (arg .Username) | where .Password "AND" "password = " (arg .Password) | where .Createtime "AND" "createtime = " (arg .Createtime)}}
@@ -320,6 +338,16 @@ DELETE FROM test_table
320338
{{where .Id "AND" "id = " (arg .Id) "" | where .Username "AND" "username = " (arg .Username) | where .Password "AND" "password = " (arg .Password) | where .Createtime "AND" "createtime = " (arg .Createtime)}}
321339
{{end}}
322340
```
341+
3. namespace
342+
343+
template数据或文件可定义一个名称为namespace的子模版,用以定义namespace。
344+
345+
template数据或文件注册之后,session参数sql id与模板对应关系为:${NAMESPACE}+"."+${ACTION_ID}
346+
347+
以2中的template为例,调用select的方式为:
348+
```cassandraql
349+
sess.Select("test.selectTestTable").Param(model).Result(&dataList)
350+
```
323351

324352
### 9、gobatis-cmd生成文件使用示例
325353

parsing/template/parse.go

+31-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ import (
1515
"text/template"
1616
)
1717

18+
const (
19+
namespaceTmplName = "namespace"
20+
)
21+
1822
type Parser struct {
23+
//template
1924
tpl *template.Template
2025
}
2126

@@ -81,9 +86,12 @@ func (m *Manager) RegisterData(data []byte) error {
8186
return err
8287
}
8388

89+
ns := getNamespace(tpl)
8490
tpls := tpl.Templates()
8591
for _, v := range tpls {
86-
m.sqlMap[v.Name()] = &Parser{v}
92+
if v.Name() != "" && v.Name() != namespaceTmplName {
93+
m.sqlMap[ns + v.Name()] = &Parser{tpl: v}
94+
}
8795
}
8896

8997
return nil
@@ -106,14 +114,35 @@ func (m *Manager) RegisterFile(file string) error {
106114
return err
107115
}
108116

117+
ns := getNamespace(tpl)
109118
tpls := tpl.Templates()
110119
for _, v := range tpls {
111-
m.sqlMap[v.Name()] = &Parser{v}
120+
if v.Name() != "" && v.Name() != namespaceTmplName {
121+
m.sqlMap[ns + v.Name()] = &Parser{tpl: v}
122+
}
112123
}
113124

114125
return nil
115126
}
116127

128+
func getNamespace(tpl *template.Template) string {
129+
ns := strings.Builder{}
130+
nsTpl := tpl.Lookup(namespaceTmplName)
131+
if nsTpl != nil {
132+
err := nsTpl.Execute(&ns, nil)
133+
if err != nil {
134+
ns.Reset()
135+
}
136+
}
137+
138+
ret := strings.TrimSpace(ns.String())
139+
140+
if ret != "" {
141+
ret = ret + "."
142+
}
143+
return ret
144+
}
145+
117146
func (m *Manager) FindSqlParser(sqlId string) (*Parser, bool) {
118147
m.lock.Lock()
119148
defer m.lock.Unlock()

parsing/xml/root.go

+15-7
Original file line numberDiff line numberDiff line change
@@ -27,40 +27,48 @@ type Mapper struct {
2727

2828
func (m *Mapper) Format() map[string]*parsing.DynamicData {
2929
ret := map[string]*parsing.DynamicData{}
30+
keyPre := strings.TrimSpace(m.Namespace)
31+
if keyPre != "" {
32+
keyPre = keyPre + "."
33+
}
3034
for _, v := range m.Insert {
31-
if d, ok := ret[v.Id]; ok {
35+
key := keyPre + v.Id
36+
if d, ok := ret[key]; ok {
3237
logging.Warn("Insert Sql id is duplicates, id: %s, before: %s, after %s\n", v.Id, d, v.Data)
3338
}
3439
d, err := ParseDynamic(strings.TrimSpace(v.Data), m.Sql)
3540
if err == nil {
36-
ret[v.Id] = d
41+
ret[key] = d
3742
}
3843
}
3944
for _, v := range m.Update {
40-
if d, ok := ret[v.Id]; ok {
45+
key := keyPre + v.Id
46+
if d, ok := ret[key]; ok {
4147
logging.Warn("Update Sql id is duplicates, id: %s, before: %s, after %s\n", v.Id, d, v.Data)
4248
}
4349
d, err := ParseDynamic(strings.TrimSpace(v.Data), m.Sql)
4450
if err == nil {
45-
ret[v.Id] = d
51+
ret[key] = d
4652
}
4753
}
4854
for _, v := range m.Select {
49-
if d, ok := ret[v.Id]; ok {
55+
key := keyPre + v.Id
56+
if d, ok := ret[key]; ok {
5057
logging.Warn("Select Sql id is duplicates, id: %s, before: %s, after %s\n", v.Id, d, v.Data)
5158
}
5259
d, err := ParseDynamic(strings.TrimSpace(v.Data), m.Sql)
5360
if err == nil {
54-
ret[v.Id] = d
61+
ret[key] = d
5562
}
5663
}
5764
for _, v := range m.Delete {
65+
key := keyPre + v.Id
5866
if d, ok := ret[v.Id]; ok {
5967
logging.Warn("Delete Sql id is duplicates, id: %s, before: %s, after %s\n", v.Id, d, v.Data)
6068
}
6169
d, err := ParseDynamic(strings.TrimSpace(v.Data), m.Sql)
6270
if err == nil {
63-
ret[v.Id] = d
71+
ret[key] = d
6472
}
6573
}
6674
return ret

0 commit comments

Comments
 (0)