From 79e15525c49dbc16ee4009359f371c19f7cdb160 Mon Sep 17 00:00:00 2001 From: zhuxiujia Date: Sat, 13 Apr 2019 19:35:04 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Esession=20=E9=9A=90=E5=BC=8F?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- GoMybatisProxy.go | 8 ++++++-- SessionSupport.go | 6 ++++++ example/Example_test.go | 15 ++++++++------- 3 files changed, 20 insertions(+), 9 deletions(-) create mode 100644 SessionSupport.go diff --git a/GoMybatisProxy.go b/GoMybatisProxy.go index ef32372..45e67b3 100644 --- a/GoMybatisProxy.go +++ b/GoMybatisProxy.go @@ -46,9 +46,12 @@ func buildMapper(v reflect.Value, buildFunc func(funcField reflect.StructField) if f.CanSet() { switch ft.Kind() { case reflect.Struct: + if buildFunc != nil { + buildMapper(f,buildFunc)//循环扫描 + } case reflect.Func: if buildFunc != nil { - buildRemoteMethod(f, ft, sf, buildFunc(sf)) + buildRemoteMethod(v,f, ft, sf, buildFunc(sf)) } } } @@ -60,7 +63,7 @@ func buildMapper(v reflect.Value, buildFunc func(funcField reflect.StructField) } } -func buildRemoteMethod(f reflect.Value, ft reflect.Type, sf reflect.StructField, proxyFunc func(args []reflect.Value, tagArgs []TagArg) []reflect.Value) { +func buildRemoteMethod(source reflect.Value,f reflect.Value, ft reflect.Type, sf reflect.StructField, proxyFunc func(args []reflect.Value, tagArgs []TagArg) []reflect.Value) { var tagParams []string var mapperParams = sf.Tag.Get(`mapperParams`) if mapperParams != `` { @@ -98,5 +101,6 @@ func buildRemoteMethod(f reflect.Value, ft reflect.Type, sf reflect.StructField, } else { f.Set(reflect.MakeFunc(ft, fn)) } + println("[GoMybatis] write method success:"+source.Type().Name()+" > "+sf.Name+" "+f.Type().String()) tagParams = nil } diff --git a/SessionSupport.go b/SessionSupport.go new file mode 100644 index 0000000..cc79284 --- /dev/null +++ b/SessionSupport.go @@ -0,0 +1,6 @@ +package GoMybatis + + +type SessionSupport struct { + NewSession func() (Session, error) //session为事务操作 +} diff --git a/example/Example_test.go b/example/Example_test.go index 24ed4bf..8868ce6 100644 --- a/example/Example_test.go +++ b/example/Example_test.go @@ -15,6 +15,8 @@ import ( //自定义结构体参数(属性必须大写) //方法 return 必须包含有error ,为了返回错误信息 type ExampleActivityMapper struct { + GoMybatis.SessionSupport //session事务操作 写法1. ExampleActivityMapper.SessionSupport.NewSession() + NewSession func() (GoMybatis.Session, error) //session事务操作.写法2 ExampleActivityMapper.NewSession() //模板示例 SelectTemplete func(name string) ([]Activity, error) `mapperParams:"name"` SelectCountTemplete func(name string) (int64, error) `mapperParams:"name"` @@ -30,11 +32,10 @@ type ExampleActivityMapper struct { SelectByCondition func(name *string, startTime *time.Time, endTime *time.Time, page *int, size *int) ([]Activity, error) `mapperParams:"name,startTime,endTime,page,size"` UpdateById func(session *GoMybatis.Session, arg Activity) (int64, error) Insert func(arg Activity) (int64, error) - CountByCondition func(name string, startTime time.Time, endTime time.Time) (int, error) `mapperParams:"name,startTime,endTime"` - DeleteById func(id string) (int64, error) `mapperParams:"id"` - Choose func(deleteFlag int) ([]Activity, error) `mapperParams:"deleteFlag"` - SelectLinks func(column string) ([]Activity, error) `mapperParams:"column"` - NewSession func() (GoMybatis.Session, error) //session为事务操作 + CountByCondition func(name string, startTime time.Time, endTime time.Time) (int, error) `mapperParams:"name,startTime,endTime"` + DeleteById func(id string) (int64, error) `mapperParams:"id"` + Choose func(deleteFlag int) ([]Activity, error) `mapperParams:"deleteFlag"` + SelectLinks func(column string) ([]Activity, error) `mapperParams:"column"` } //初始化mapper文件和结构体 @@ -150,7 +151,7 @@ func Test_select_all(t *testing.T) { if err != nil { panic(err) } - var b,_=json.Marshal(result) + var b, _ = json.Marshal(result) fmt.Println("result=", string(b)) } @@ -205,7 +206,7 @@ func Test_local_Transation(t *testing.T) { return } //使用事务 - var session, err = exampleActivityMapper.NewSession() + var session, err = exampleActivityMapper.SessionSupport.NewSession() if err != nil { t.Fatal(err) }