Skip to content

Commit 1075e5d

Browse files
committed
修改factory 接口,增加可扩展性
1 parent 979d79f commit 1075e5d

File tree

3 files changed

+79
-67
lines changed

3 files changed

+79
-67
lines changed

README.md

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ session.Select("select * from test_table where username = #{TestTable.username}"
126126

127127
使用
128128
```
129-
mgr.NewSession().Tx(func(session *gobatis.Session) bool {
129+
mgr.NewSession().Tx(func(session *gobatis.Session) error {
130130
ret := 0
131131
session.Insert("insert_id").Param(testV).Result(&ret)
132132
@@ -138,11 +138,11 @@ session.Select("select * from test_table where username = #{TestTable.username}"
138138
t.Logf("data: %v", v)
139139
}
140140
//commit
141-
return true
141+
return nil
142142
})
143143
```
144-
1. 当参数的func返回true,则提交
145-
2. 当参数的func返回false,则回滚
144+
1. 当参数的func返回nil,则提交
145+
2. 当参数的func返回非nil的错误,则回滚
146146
3. 当参数的func内抛出panic,则回滚
147147

148148
### 7、xml
@@ -165,30 +165,30 @@ gobatis.RegisterMapperFile(filePath)
165165

166166
```
167167
<mapper namespace="test_package.TestTable">
168-
<sql id="columns_id">id,username,password,update_time</sql>
168+
<sql id="columns_id">`id`,`username`,`password`,`update_time`</sql>
169169
170170
<select id="selectTestTable">
171-
SELECT <include refid="columns_id"> </include> FROM test_table
171+
SELECT <include refid="columns_id"> </include> FROM `TEST_TABLE`
172172
<where>
173-
<if test="{TestTable.id} != nil and {TestTable.id} != 0">AND id = #{TestTable.id} </if>
174-
<if test="{TestTable.username} != nil">AND username = #{TestTable.username} </if>
175-
<if test="{TestTable.password} != nil">AND password = #{TestTable.password} </if>
176-
<if test="{TestTable.update_time} != nil">AND update_time = #{TestTable.update_time} </if>
173+
<if test="{TestTable.id} != nil and {TestTable.id} != 0">AND `id` = #{TestTable.id} </if>
174+
<if test="{TestTable.username} != nil">AND `username` = #{TestTable.username} </if>
175+
<if test="{TestTable.password} != nil">AND `password` = #{TestTable.password} </if>
176+
<if test="{TestTable.update_time} != nil">AND `update_time` = #{TestTable.update_time} </if>
177177
</where>
178178
</select>
179179
180180
<select id="selectTestTableCount">
181-
SELECT COUNT(*) FROM test_table
181+
SELECT COUNT(*) FROM `TEST_TABLE`
182182
<where>
183-
<if test="{TestTable.id} != nil and {TestTable.id} != 0">AND id = #{TestTable.id} </if>
184-
<if test="{TestTable.username} != nil">AND username = #{TestTable.username} </if>
185-
<if test="{TestTable.password} != nil">AND password = #{TestTable.password} </if>
186-
<if test="{TestTable.update_time} != nil">AND update_time = #{TestTable.update_time} </if>
183+
<if test="{TestTable.id} != nil and {TestTable.id} != 0">AND `id` = #{TestTable.id} </if>
184+
<if test="{TestTable.username} != nil">AND `username` = #{TestTable.username} </if>
185+
<if test="{TestTable.password} != nil">AND `password` = #{TestTable.password} </if>
186+
<if test="{TestTable.update_time} != nil">AND `update_time` = #{TestTable.update_time} </if>
187187
</where>
188188
</select>
189189
190190
<insert id="insertTestTable">
191-
INSERT INTO test_table (id,username,password,update_time)
191+
INSERT INTO `TEST_TABLE` (`id`,`username`,`password`,`update_time`)
192192
VALUES(
193193
#{TestTable.id},
194194
#{TestTable.username},
@@ -198,22 +198,22 @@ gobatis.RegisterMapperFile(filePath)
198198
</insert>
199199
200200
<update id="updateTestTable">
201-
UPDATE test_table
201+
UPDATE `TEST_TABLE`
202202
<set>
203-
<if test="{TestTable.username} != nil"> username = #{TestTable.username} </if>
204-
<if test="{TestTable.password} != nil"> password = #{TestTable.password} </if>
205-
<if test="{TestTable.update_time} != nil"> update_time = #{TestTable.update_time} </if>
203+
<if test="{TestTable.username} != nil"> `username` = #{TestTable.username} </if>
204+
<if test="{TestTable.password} != nil"> `password` = #{TestTable.password} </if>
205+
<if test="{TestTable.update_time} != nil"> `update_time` = #{TestTable.update_time} </if>
206206
</set>
207-
WHERE id = #{TestTable.id}
207+
WHERE `id` = #{TestTable.id}
208208
</update>
209209
210210
<delete id="deleteTestTable">
211-
DELETE FROM test_table
211+
DELETE FROM `TEST_TABLE`
212212
<where>
213-
<if test="{TestTable.id} != nil and {TestTable.id} != 0">AND id = #{TestTable.id} </if>
214-
<if test="{TestTable.username} != nil">AND username = #{TestTable.username} </if>
215-
<if test="{TestTable.password} != nil">AND password = #{TestTable.password} </if>
216-
<if test="{TestTable.update_time} != nil">AND update_time = #{TestTable.update_time} </if>
213+
<if test="{TestTable.id} != nil and {TestTable.id} != 0">AND `id` = #{TestTable.id} </if>
214+
<if test="{TestTable.username} != nil">AND `username` = #{TestTable.username} </if>
215+
<if test="{TestTable.password} != nil">AND `password` = #{TestTable.password} </if>
216+
<if test="{TestTable.update_time} != nil">AND `update_time` = #{TestTable.update_time} </if>
217217
</where>
218218
</delete>
219219
</mapper>

factory/default_factory.go

Lines changed: 44 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,56 +9,64 @@
99
package factory
1010

1111
import (
12-
"database/sql"
13-
"github.com/xfali/gobatis/datasource"
14-
"github.com/xfali/gobatis/executor"
15-
"github.com/xfali/gobatis/logging"
16-
"github.com/xfali/gobatis/session"
17-
"github.com/xfali/gobatis/transaction"
12+
"database/sql"
13+
"github.com/xfali/gobatis/datasource"
14+
"github.com/xfali/gobatis/executor"
15+
"github.com/xfali/gobatis/logging"
16+
"github.com/xfali/gobatis/session"
17+
"github.com/xfali/gobatis/transaction"
1818
)
1919

2020
type DefaultFactory struct {
21-
Host string
22-
Port int
23-
DBName string
24-
Username string
25-
Password string
26-
Charset string
21+
Host string
22+
Port int
23+
DBName string
24+
Username string
25+
Password string
26+
Charset string
2727

28-
MaxConn int
29-
MaxIdleConn int
30-
Log logging.LogFunc
28+
MaxConn int
29+
MaxIdleConn int
30+
Log logging.LogFunc
3131

32-
ds datasource.DataSource
33-
db *sql.DB
32+
ds datasource.DataSource
33+
db *sql.DB
3434
}
3535

3636
func (f *DefaultFactory) InitDB() error {
37-
f.ds = &datasource.MysqlDataSource{
38-
Host: f.Host,
39-
Port: f.Port,
40-
DBName: f.DBName,
41-
Username: f.Username,
42-
Password: f.Password,
43-
Charset: f.Charset,
44-
}
37+
f.ds = &datasource.MysqlDataSource{
38+
Host: f.Host,
39+
Port: f.Port,
40+
DBName: f.DBName,
41+
Username: f.Username,
42+
Password: f.Password,
43+
Charset: f.Charset,
44+
}
4545

46-
db, err := sql.Open(f.ds.DriverName(), f.ds.Url())
47-
db.SetMaxOpenConns(f.MaxConn)
48-
db.SetMaxIdleConns(f.MaxIdleConn)
49-
if err != nil {
50-
return err
51-
}
46+
db, err := sql.Open(f.ds.DriverName(), f.ds.Url())
47+
db.SetMaxOpenConns(f.MaxConn)
48+
db.SetMaxIdleConns(f.MaxIdleConn)
49+
if err != nil {
50+
return err
51+
}
5252

53-
f.db = db
54-
return nil
53+
f.db = db
54+
return nil
55+
}
56+
57+
func (f *DefaultFactory) CreateTransaction() transaction.Transaction {
58+
return transaction.NewMysqlTransaction(f.ds, f.db)
59+
}
60+
61+
func (f *DefaultFactory) CreateExecutor(transaction transaction.Transaction) executor.Executor {
62+
return executor.NewSimpleExecutor(transaction)
5563
}
5664

5765
func (f *DefaultFactory) CreateSession() session.SqlSession {
58-
tx := transaction.NewMysqlTransaction(f.ds, f.db)
59-
return session.NewDefaultSqlSession(f.Log, tx, executor.NewSimpleExecutor(tx), false)
66+
tx := f.CreateTransaction()
67+
return session.NewDefaultSqlSession(f.Log, tx, f.CreateExecutor(tx), false)
6068
}
6169

6270
func (f *DefaultFactory) LogFunc() logging.LogFunc {
63-
return f.Log
71+
return f.Log
6472
}

factory/factory.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,16 @@
99
package factory
1010

1111
import (
12-
"github.com/xfali/gobatis/logging"
13-
"github.com/xfali/gobatis/session"
12+
"github.com/xfali/gobatis/executor"
13+
"github.com/xfali/gobatis/logging"
14+
"github.com/xfali/gobatis/session"
15+
"github.com/xfali/gobatis/transaction"
1416
)
1517

1618
type Factory interface {
17-
InitDB() error
18-
CreateSession() session.SqlSession
19-
LogFunc() logging.LogFunc
19+
InitDB() error
20+
CreateTransaction() transaction.Transaction
21+
CreateExecutor(transaction.Transaction) executor.Executor
22+
CreateSession() session.SqlSession
23+
LogFunc() logging.LogFunc
2024
}

0 commit comments

Comments
 (0)