Skip to content

Commit bd130b4

Browse files
committed
增加namespace支持
1 parent e93b084 commit bd130b4

File tree

6 files changed

+110
-72
lines changed

6 files changed

+110
-72
lines changed

README.md

Lines changed: 63 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ go get github.com/xfali/gobatis-cmd
1010

1111
## 使用
1212
```
13-
gobatis-cmd -driver=mysql -host=localhost -port=3306 -user=test -pw=test -db=testdb -pkg=test_package -mapper=xml -path=.
13+
gobatis-cmd -driver=mysql -host=localhost -port=3306 -user=test -pw=test -db=testdb -pkg=test_package -mapper=xml -namespace=test -path=.
1414
```
1515

1616
```
@@ -40,6 +40,8 @@ gobatis-cmd -driver=mysql -host=localhost -port=3306 -user=test -pw=test -db=tes
4040
mapper文件类型: xml | template | go (默认xml)
4141
-keyword bool
4242
是否自动添加转义符,默认false,如果为true则会根据driver名称辨识添加
43+
-namespace string
44+
添加namespace,避免同表名冲突,(默认为空)
4345
```
4446

4547
会在当前目录下生成1个目录及3个文件,分别为:
@@ -78,56 +80,64 @@ type TestTable struct {
7880

7981
例子:
8082
```
81-
<mapper namespace="test_package.TestTable">
82-
<sql id="columns_id">`id`,`username`,`password`,`update_time`</sql>
83+
<mapper namespace="test">
84+
<sql id="columns_id">id,username,password,createtime</sql>
8385
8486
<select id="selectTestTable">
85-
SELECT <include refid="columns_id"> </include> FROM `TEST_TABLE`
87+
SELECT <include refid="columns_id"> </include> FROM test_table
8688
<where>
87-
<if test="{TestTable.id} != nil and {TestTable.id} != 0">AND `id` = #{TestTable.id} </if>
88-
<if test="{TestTable.username} != nil">AND `username` = #{TestTable.username} </if>
89-
<if test="{TestTable.password} != nil">AND `password` = #{TestTable.password} </if>
90-
<if test="{TestTable.update_time} != nil">AND `update_time` = #{TestTable.update_time} </if>
89+
<if test="{TestTable.id} != nil and {TestTable.id} != 0">AND id = #{TestTable.id} </if>
90+
<if test="{TestTable.username} != nil">AND username = #{TestTable.username} </if>
91+
<if test="{TestTable.password} != nil">AND password = #{TestTable.password} </if>
92+
<if test="{TestTable.createtime} != nil">AND createtime = #{TestTable.createtime} </if>
9193
</where>
9294
</select>
9395
9496
<select id="selectTestTableCount">
95-
SELECT COUNT(*) FROM `TEST_TABLE`
97+
SELECT COUNT(*) FROM test_table
9698
<where>
97-
<if test="{TestTable.id} != nil and {TestTable.id} != 0">AND `id` = #{TestTable.id} </if>
98-
<if test="{TestTable.username} != nil">AND `username` = #{TestTable.username} </if>
99-
<if test="{TestTable.password} != nil">AND `password` = #{TestTable.password} </if>
100-
<if test="{TestTable.update_time} != nil">AND `update_time` = #{TestTable.update_time} </if>
99+
<if test="{TestTable.id} != nil and {TestTable.id} != 0">AND id = #{TestTable.id} </if>
100+
<if test="{TestTable.username} != nil">AND username = #{TestTable.username} </if>
101+
<if test="{TestTable.password} != nil">AND password = #{TestTable.password} </if>
102+
<if test="{TestTable.createtime} != nil">AND createtime = #{TestTable.createtime} </if>
101103
</where>
102104
</select>
103105
104106
<insert id="insertTestTable">
105-
INSERT INTO `TEST_TABLE` (`id`,`username`,`password`,`update_time`)
107+
INSERT INTO test_table (id,username,password,createtime)
106108
VALUES(
107109
#{TestTable.id},
108110
#{TestTable.username},
109111
#{TestTable.password},
110-
#{TestTable.update_time}
112+
#{TestTable.createtime}
111113
)
112114
</insert>
113115
116+
<insert id="insertBatchTestTable">
117+
INSERT INTO test_table (id,username,password,createtime)
118+
VALUES
119+
<foreach item="item" index="index" collection="{0}" open="" separator="," close="">
120+
(#{item.TestTable.id},#{item.TestTable.username},#{item.TestTable.password},#{item.TestTable.createtime})
121+
</foreach>
122+
</insert>
123+
114124
<update id="updateTestTable">
115-
UPDATE `TEST_TABLE`
125+
UPDATE test_table
116126
<set>
117-
<if test="{TestTable.username} != nil"> `username` = #{TestTable.username} </if>
118-
<if test="{TestTable.password} != nil"> `password` = #{TestTable.password} </if>
119-
<if test="{TestTable.update_time} != nil"> `update_time` = #{TestTable.update_time} </if>
127+
<if test="{TestTable.username} != nil"> username = #{TestTable.username} </if>
128+
<if test="{TestTable.password} != nil"> password = #{TestTable.password} </if>
129+
<if test="{TestTable.createtime} != nil"> createtime = #{TestTable.createtime} </if>
120130
</set>
121-
WHERE `id` = #{TestTable.id}
131+
WHERE id = #{TestTable.id}
122132
</update>
123133
124134
<delete id="deleteTestTable">
125-
DELETE FROM `TEST_TABLE`
135+
DELETE FROM test_table
126136
<where>
127-
<if test="{TestTable.id} != nil and {TestTable.id} != 0">AND `id` = #{TestTable.id} </if>
128-
<if test="{TestTable.username} != nil">AND `username` = #{TestTable.username} </if>
129-
<if test="{TestTable.password} != nil">AND `password` = #{TestTable.password} </if>
130-
<if test="{TestTable.update_time} != nil">AND `update_time` = #{TestTable.update_time} </if>
137+
<if test="{TestTable.id} != nil and {TestTable.id} != 0">AND id = #{TestTable.id} </if>
138+
<if test="{TestTable.username} != nil">AND username = #{TestTable.username} </if>
139+
<if test="{TestTable.password} != nil">AND password = #{TestTable.password} </if>
140+
<if test="{TestTable.createtime} != nil">AND createtime = #{TestTable.createtime} </if>
131141
</where>
132142
</delete>
133143
</mapper>
@@ -146,7 +156,7 @@ type TestTable struct {
146156

147157
例子:
148158
```
149-
package test_package
159+
package test
150160
151161
import (
152162
"github.com/xfali/gobatis"
@@ -155,38 +165,46 @@ import (
155165
func init() {
156166
modelV := TestTable{}
157167
gobatis.RegisterModel(&modelV)
158-
gobatis.RegisterMapperFile("c:/tmp/xml/test_table_mapper.xml")
168+
gobatis.RegisterTemplateFile("./template/test_table_mapper.tmpl")
159169
}
160170
161171
func SelectTestTable(sess *gobatis.Session, model TestTable) ([]TestTable, error) {
162172
var dataList []TestTable
163-
err := sess.Select("selectTestTable").Param(model).Result(&dataList)
173+
err := sess.Select("test.selectTestTable").Param(model).Result(&dataList)
164174
return dataList, err
165175
}
166176
167177
func SelectTestTableCount(sess *gobatis.Session, model TestTable) (int64, error) {
168178
var ret int64
169-
err := sess.Select("selectTestTableCount").Param(model).Result(&ret)
179+
err := sess.Select("test.selectTestTableCount").Param(model).Result(&ret)
170180
return ret, err
171181
}
172182
173183
func InsertTestTable(sess *gobatis.Session, model TestTable) (int64, int64, error) {
174184
var ret int64
175-
runner := sess.Insert("insertTestTable").Param(model)
185+
runner := sess.Insert("test.insertTestTable").Param(model)
186+
err := runner.Result(&ret)
187+
id := runner.LastInsertId()
188+
return ret, id, err
189+
}
190+
191+
func InsertBatchTestTable(sess *gobatis.Session, models []TestTable) (int64, int64, error) {
192+
var ret int64
193+
runner := sess.Insert("test.insertBatchTestTable").Param(models)
176194
err := runner.Result(&ret)
177195
id := runner.LastInsertId()
178196
return ret, id, err
179197
}
180198
181199
func UpdateTestTable(sess *gobatis.Session, model TestTable) (int64, error) {
182200
var ret int64
183-
err := sess.Update("updateTestTable").Param(model).Result(&ret)
201+
err := sess.Update("test.updateTestTable").Param(model).Result(&ret)
184202
return ret, err
185203
}
186204
187205
func DeleteTestTable(sess *gobatis.Session, model TestTable) (int64, error) {
188206
var ret int64
189-
err := sess.Delete("deleteTestTable").Param(model).Result(&ret)
207+
err := sess.Delete("test.deleteTestTable").Param(model).Result(&ret)
190208
return ret, err
191209
}
192210
```
@@ -196,39 +214,41 @@ func DeleteTestTable(sess *gobatis.Session, model TestTable) (int64, error) {
196214

197215
例子:
198216
```cassandraql
217+
{{define "namespace"}}test{{end}}
218+
199219
{{define "selectTestTable"}}
200-
SELECT "id","username","password","createtime" FROM "test_table"
201-
{{where .Id "AND" "\"id\" = " (arg .Id) "" | where .Username "AND" "\"username\" = " (arg .Username) | where .Password "AND" "\"password\" = " (arg .Password) | where .Createtime "AND" "\"createtime\" = " (arg .Createtime)}}
220+
SELECT id,username,password,createtime FROM test_table
221+
{{where .Id "AND" "id = " (arg .Id) "" | where .Username "AND" "username = " (arg .Username) | where .Password "AND" "password = " (arg .Password) | where .Createtime "AND" "createtime = " (arg .Createtime)}}
202222
{{end}}
203223
204224
{{define "selectTestTableCount"}}
205-
SELECT COUNT(*) FROM "test_table"
206-
{{where .Id "AND" "\"id\" = " (arg .Id) "" | where .Username "AND" "\"username\" = " (arg .Username) | where .Password "AND" "\"password\" = " (arg .Password) | where .Createtime "AND" "\"createtime\" = " (arg .Createtime)}}
225+
SELECT COUNT(*) FROM test_table
226+
{{where .Id "AND" "id = " (arg .Id) "" | where .Username "AND" "username = " (arg .Username) | where .Password "AND" "password = " (arg .Password) | where .Createtime "AND" "createtime = " (arg .Createtime)}}
207227
{{end}}
208228
209229
{{define "insertTestTable"}}
210-
INSERT INTO "test_table"("id","username","password","createtime")
230+
INSERT INTO test_table(id,username,password,createtime)
211231
VALUES(
212232
{{arg .Id}}, {{arg .Username}}, {{arg .Password}}, {{arg .Createtime}})
213233
{{end}}
214234
215235
{{define "insertBatchTestTable"}}
216236
{{$size := len . | add -1}}
217-
INSERT INTO "test_table"("id","username","password","createtime")
237+
INSERT INTO test_table(id,username,password,createtime)
218238
VALUES {{range $i, $v := .}}
219239
({{arg $v.Id}}, {{arg $v.Username}}, {{arg $v.Password}}, {{arg $v.Createtime}}){{if lt $i $size}},{{end}}
220240
{{end}}
221241
{{end}}
222242
223243
{{define "updateTestTable"}}
224-
UPDATE "test_table"
225-
{{set .Id "\"id\" = " (arg .Id) "" | set .Username "\"username\" = " (arg .Username) | set .Password "\"password\" = " (arg .Password) | set .Createtime "\"createtime\" = " (arg .Createtime)}}
226-
{{where .Id "AND" "\"id\" = " (arg .Id) ""}}
244+
UPDATE test_table
245+
{{set .Id "id = " (arg .Id) "" | set .Username "username = " (arg .Username) | set .Password "password = " (arg .Password) | set .Createtime "createtime = " (arg .Createtime)}}
246+
{{where .Id "AND" "id = " (arg .Id) ""}}
227247
{{end}}
228248
229249
{{define "deleteTestTable"}}
230-
DELETE FROM "test_table"
231-
{{where .Id "AND" "\"id\" = " (arg .Id) "" | where .Username "AND" "\"username\" = " (arg .Username) | where .Password "AND" "\"password\" = " (arg .Password) | where .Createtime "AND" "\"createtime\" = " (arg .Createtime)}}
250+
DELETE FROM test_table
251+
{{where .Id "AND" "id = " (arg .Id) "" | where .Username "AND" "username = " (arg .Username) | where .Password "AND" "password = " (arg .Password) | where .Createtime "AND" "createtime = " (arg .Createtime)}}
232252
{{end}}
233253
```
234254

gen_proxy_v2.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ func genV2Proxy(config Config, tableName string, models []common.ModelInfo) {
2525
if err == nil {
2626
defer mapperFile.Close()
2727

28+
namespace := ""
29+
if config.Namespace != "" {
30+
namespace = config.Namespace + "."
31+
}
2832
modelName := common.TableName2ModelName(tableName)
2933
builder := strings.Builder{}
3034
builder.WriteString(fmt.Sprintf("//This file was generated by xfali/gobatis-cmd at"))
@@ -92,7 +96,7 @@ func genV2Proxy(config Config, tableName string, models []common.ModelInfo) {
9296
builder.WriteString(common.Newline())
9397

9498
builder.WriteString(common.ColumnSpace())
95-
builder.WriteString(fmt.Sprintf(`err := sess.Select("select%s").Param(model).Result(&dataList)`, modelName))
99+
builder.WriteString(fmt.Sprintf(`err := sess.Select("%sselect%s").Param(model).Result(&dataList)`, namespace, modelName))
96100
builder.WriteString(common.Newline())
97101

98102
builder.WriteString(common.ColumnSpace())
@@ -113,7 +117,7 @@ func genV2Proxy(config Config, tableName string, models []common.ModelInfo) {
113117
builder.WriteString(common.Newline())
114118

115119
builder.WriteString(common.ColumnSpace())
116-
builder.WriteString(fmt.Sprintf(`err := sess.Select("select%sCount").Param(model).Result(&ret)`, modelName))
120+
builder.WriteString(fmt.Sprintf(`err := sess.Select("%sselect%sCount").Param(model).Result(&ret)`, namespace, modelName))
117121
builder.WriteString(common.Newline())
118122

119123
builder.WriteString(common.ColumnSpace())
@@ -134,7 +138,7 @@ func genV2Proxy(config Config, tableName string, models []common.ModelInfo) {
134138
builder.WriteString(common.Newline())
135139

136140
builder.WriteString(common.ColumnSpace())
137-
builder.WriteString(fmt.Sprintf(`runner := sess.Insert("insert%s").Param(model)`, modelName))
141+
builder.WriteString(fmt.Sprintf(`runner := sess.Insert("%sinsert%s").Param(model)`, namespace, modelName))
138142
builder.WriteString(common.Newline())
139143

140144
builder.WriteString(common.ColumnSpace())
@@ -163,7 +167,7 @@ func genV2Proxy(config Config, tableName string, models []common.ModelInfo) {
163167
builder.WriteString(common.Newline())
164168

165169
builder.WriteString(common.ColumnSpace())
166-
builder.WriteString(fmt.Sprintf(`runner := sess.Insert("insertBatch%s").Param(models)`, modelName))
170+
builder.WriteString(fmt.Sprintf(`runner := sess.Insert("%sinsertBatch%s").Param(models)`, namespace, modelName))
167171
builder.WriteString(common.Newline())
168172

169173
builder.WriteString(common.ColumnSpace())
@@ -192,7 +196,7 @@ func genV2Proxy(config Config, tableName string, models []common.ModelInfo) {
192196
builder.WriteString(common.Newline())
193197

194198
builder.WriteString(common.ColumnSpace())
195-
builder.WriteString(fmt.Sprintf(`err := sess.Update("update%s").Param(model).Result(&ret)`, modelName))
199+
builder.WriteString(fmt.Sprintf(`err := sess.Update("%supdate%s").Param(model).Result(&ret)`, namespace, modelName))
196200
builder.WriteString(common.Newline())
197201

198202
builder.WriteString(common.ColumnSpace())
@@ -213,7 +217,7 @@ func genV2Proxy(config Config, tableName string, models []common.ModelInfo) {
213217
builder.WriteString(common.Newline())
214218

215219
builder.WriteString(common.ColumnSpace())
216-
builder.WriteString(fmt.Sprintf(`err := sess.Delete("delete%s").Param(model).Result(&ret)`, modelName))
220+
builder.WriteString(fmt.Sprintf(`err := sess.Delete("%sdelete%s").Param(model).Result(&ret)`, namespace, modelName))
217221
builder.WriteString(common.Newline())
218222

219223
builder.WriteString(common.ColumnSpace())

gen_template.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ func buildTmplMapper(builder *strings.Builder, config Config, tableName string,
4545
builder.WriteString(common.Newline())
4646
builder.WriteString(common.Newline())
4747

48+
//namespace
49+
builder.WriteString(`{{define "namespace"}}`)
50+
51+
builder.WriteString(config.Namespace)
52+
53+
builder.WriteString(`{{end}}`)
54+
builder.WriteString(common.Newline())
55+
builder.WriteString(common.Newline())
56+
4857
//select
4958
builder.WriteString(fmt.Sprintf(`{{define "select%s"}}`, modelName))
5059
builder.WriteString(common.Newline())

gen_xml.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* All right reserved.
44
* @author xiongfa.li
55
* @version V1.0
6-
* Description:
6+
* Description:
77
*/
88

99
package main
@@ -74,7 +74,7 @@ func buildMapper(builder *strings.Builder, config Config, tableName string, mode
7474
builder.WriteString(common.Newline())
7575
builder.WriteString(common.Newline())
7676

77-
builder.WriteString(fmt.Sprintf("<mapper namespace=\"%s.%s\">", config.PackageName, modelName))
77+
builder.WriteString(fmt.Sprintf("<mapper namespace=\"%s\">", config.Namespace))
7878
builder.WriteString(common.Newline())
7979

8080
builder.WriteString(common.ColumnSpace())

generator.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,28 @@ package main
1010

1111
import (
1212
"github.com/xfali/gobatis-cmd/common"
13+
"log"
14+
"os"
1315
)
1416

17+
18+
func genOneTable(config Config, db common.DBDriver, dbName, table string) {
19+
models, err := db.QueryTableInfo(dbName, table)
20+
if err != nil {
21+
log.Print(err)
22+
os.Exit(-3)
23+
}
24+
//conf := config
25+
//if conf.Namespace == "" {
26+
// conf.Namespace = config.PackageName + "." + common.TableName2ModelName(table)
27+
//}
28+
err2 := generate(config, models, table)
29+
if err2 != nil {
30+
log.Print(err2)
31+
os.Exit(-2)
32+
}
33+
}
34+
1535
func generate(config Config, models []common.ModelInfo, tableName string) (err error) {
1636
genModel(config, tableName, models)
1737
if config.MapperFile == "template" {

0 commit comments

Comments
 (0)