Skip to content

Commit 4a59f26

Browse files
committed
增加mapper文件扫描方法
1 parent eb8041e commit 4a59f26

File tree

4 files changed

+144
-0
lines changed

4 files changed

+144
-0
lines changed

sqlmanager.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
"github.com/xfali/gobatis/parsing/sqlparser"
1414
"github.com/xfali/gobatis/parsing/template"
1515
"github.com/xfali/gobatis/parsing/xml"
16+
"os"
17+
"path/filepath"
1618
)
1719

1820
type sqlManager struct {
@@ -65,4 +67,31 @@ func DynamicParserFactory(sql string) (sqlparser.SqlParser, error) {
6567

6668
func TemplateParserFactory(sql string) (sqlparser.SqlParser, error) {
6769
return template.CreateParser([]byte(sql))
70+
}
71+
72+
func ScanMapperFile(dir string) error {
73+
return filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
74+
if err != nil {
75+
return err
76+
}
77+
if !info.IsDir() {
78+
filename := filepath.Base(path)
79+
length := len(filename)
80+
if length > 4 {
81+
if filename[length-4:] == ".xml" {
82+
err := RegisterMapperFile(path)
83+
if err != nil {
84+
return err
85+
}
86+
}
87+
if filename[length-4:] == ".tpl" {
88+
err := RegisterTemplateFile(path)
89+
if err != nil {
90+
return err
91+
}
92+
}
93+
}
94+
}
95+
return nil
96+
})
6897
}

test/sqlmanager/manager_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright (C) 2019-2020, Xiongfa Li.
2+
// @author xiongfa.li
3+
// @version V1.0
4+
// Description:
5+
6+
package sqlmanager
7+
8+
import (
9+
"github.com/xfali/gobatis"
10+
"testing"
11+
)
12+
13+
func TestManager(t *testing.T) {
14+
err := gobatis.ScanMapperFile("./x")
15+
if err != nil {
16+
t.Fatal(err)
17+
}
18+
}

test/sqlmanager/x/tpl/x.tpl

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{{define "namespace"}}test{{end}}
2+
3+
{{define "selectTestTable"}}
4+
SELECT id,username,password,createtime FROM test_table
5+
{{where .Id "AND" "id = " (arg .Id) "" | where .Username "AND" "username = " (arg .Username) | where .Password "AND" "password = " (arg .Password) | where .Createtime "AND" "createtime = " (arg .Createtime)}}
6+
{{end}}
7+
8+
{{define "selectTestTableCount"}}
9+
SELECT COUNT(*) FROM test_table
10+
{{where .Id "AND" "id = " (arg .Id) "" | where .Username "AND" "username = " (arg .Username) | where .Password "AND" "password = " (arg .Password) | where .Createtime "AND" "createtime = " (arg .Createtime)}}
11+
{{end}}
12+
13+
{{define "insertTestTable"}}
14+
INSERT INTO test_table(id,username,password,createtime)
15+
VALUES(
16+
{{arg .Id}}, {{arg .Username}}, {{arg .Password}}, {{arg .Createtime}})
17+
{{end}}
18+
19+
{{define "insertBatchTestTable"}}
20+
{{$size := len . | add -1}}
21+
INSERT INTO test_table(id,username,password,createtime)
22+
VALUES {{range $i, $v := .}}
23+
({{arg $v.Id}}, {{arg $v.Username}}, {{arg $v.Password}}, {{arg $v.Createtime}}){{if lt $i $size}},{{end}}
24+
{{end}}
25+
{{end}}
26+
27+
{{define "updateTestTable"}}
28+
UPDATE test_table
29+
{{set .Id "id = " (arg .Id) "" | set .Username "username = " (arg .Username) | set .Password "password = " (arg .Password) | set .Createtime "createtime = " (arg .Createtime)}}
30+
{{where .Id "AND" "id = " (arg .Id) ""}}
31+
{{end}}
32+
33+
{{define "deleteTestTable"}}
34+
DELETE FROM test_table
35+
{{where .Id "AND" "id = " (arg .Id) "" | where .Username "AND" "username = " (arg .Username) | where .Password "AND" "password = " (arg .Password) | where .Createtime "AND" "createtime = " (arg .Createtime)}}
36+
{{end}}

test/sqlmanager/x/xml/x.xml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<mapper namespace="test">
2+
<sql id="columns_id">id,username,password,createtime</sql>
3+
4+
<select id="selectTestTable">
5+
SELECT <include refid="columns_id"> </include> FROM test_table
6+
<where>
7+
<if test="{TestTable.id} != nil and {TestTable.id} != 0">AND id = #{TestTable.id} </if>
8+
<if test="{TestTable.username} != nil">AND username = #{TestTable.username} </if>
9+
<if test="{TestTable.password} != nil">AND password = #{TestTable.password} </if>
10+
<if test="{TestTable.createtime} != nil">AND createtime = #{TestTable.createtime} </if>
11+
</where>
12+
</select>
13+
14+
<select id="selectTestTableCount">
15+
SELECT COUNT(*) FROM test_table
16+
<where>
17+
<if test="{TestTable.id} != nil and {TestTable.id} != 0">AND id = #{TestTable.id} </if>
18+
<if test="{TestTable.username} != nil">AND username = #{TestTable.username} </if>
19+
<if test="{TestTable.password} != nil">AND password = #{TestTable.password} </if>
20+
<if test="{TestTable.createtime} != nil">AND createtime = #{TestTable.createtime} </if>
21+
</where>
22+
</select>
23+
24+
<insert id="insertTestTable">
25+
INSERT INTO test_table (id,username,password,createtime)
26+
VALUES(
27+
#{TestTable.id},
28+
#{TestTable.username},
29+
#{TestTable.password},
30+
#{TestTable.createtime}
31+
)
32+
</insert>
33+
34+
<insert id="insertBatchTestTable">
35+
INSERT INTO test_table (id,username,password,createtime)
36+
VALUES
37+
<foreach item="item" index="index" collection="{0}" open="" separator="," close="">
38+
(#{item.TestTable.id},#{item.TestTable.username},#{item.TestTable.password},#{item.TestTable.createtime})
39+
</foreach>
40+
</insert>
41+
42+
<update id="updateTestTable">
43+
UPDATE test_table
44+
<set>
45+
<if test="{TestTable.username} != nil"> username = #{TestTable.username} </if>
46+
<if test="{TestTable.password} != nil"> password = #{TestTable.password} </if>
47+
<if test="{TestTable.createtime} != nil"> createtime = #{TestTable.createtime} </if>
48+
</set>
49+
WHERE id = #{TestTable.id}
50+
</update>
51+
52+
<delete id="deleteTestTable">
53+
DELETE FROM test_table
54+
<where>
55+
<if test="{TestTable.id} != nil and {TestTable.id} != 0">AND id = #{TestTable.id} </if>
56+
<if test="{TestTable.username} != nil">AND username = #{TestTable.username} </if>
57+
<if test="{TestTable.password} != nil">AND password = #{TestTable.password} </if>
58+
<if test="{TestTable.createtime} != nil">AND createtime = #{TestTable.createtime} </if>
59+
</where>
60+
</delete>
61+
</mapper>

0 commit comments

Comments
 (0)