github.com/isyscore/isc-gobase@v1.5.3-0.20231218061332-cbc7451899e9/extend/orm/test/xorm_test.go (about) 1 package test 2 3 import ( 4 "context" 5 "fmt" 6 "github.com/isyscore/isc-gobase/config" 7 orm2 "github.com/isyscore/isc-gobase/extend/orm" 8 "testing" 9 "xorm.io/xorm/contexts" 10 ) 11 12 func TestXorm1(t *testing.T) { 13 config.LoadYamlFile("./application-test1.yaml") 14 orm2.AddXormHook(&GobaseXormHook{}) 15 db, _ := orm2.NewXormDb() 16 17 // 删除表 18 db.Exec("drop table isc_demo.gobase_demo") 19 20 //新增表 21 db.Exec("CREATE TABLE gobase_demo(\n" + 22 " `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键',\n" + 23 " `name` char(20) NOT NULL COMMENT '名字',\n" + 24 " `age` INT NOT NULL COMMENT '年龄',\n" + 25 " `address` char(20) NOT NULL COMMENT '名字',\n" + 26 " \n" + 27 " `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',\n" + 28 " `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',\n" + 29 "\n" + 30 " PRIMARY KEY (`id`)\n" + 31 ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='测试表'") 32 33 db.Table("gobase_demo").Insert(&GobaseDemo{Name: "zhou", Age: 18, Address: "杭州"}) 34 // 新增 35 db.Table("gobase_demo").Insert(&GobaseDemo{Name: "zhou", Age: 18, Address: "杭州"}) 36 37 var demo GobaseDemo 38 db.Table("gobase_demo").Where("name=?", "zhou").Get(&demo) 39 40 dd := db.DB() 41 dd.Query("select * from gobase_demo") 42 43 // 查询:多行 44 fmt.Println(demo) 45 } 46 47 48 type GobaseXormHook struct { 49 } 50 51 func (*GobaseXormHook) BeforeProcess(c *contexts.ContextHook, driverName string) (context.Context, error) { 52 fmt.Println("before-xorm") 53 return c.Ctx, nil 54 } 55 56 func (*GobaseXormHook) AfterProcess(c *contexts.ContextHook, driverName string) error { 57 fmt.Println("after-xorm") 58 return nil 59 }