xorm-删除和软删除实例
程序员文章站
2022-07-02 16:37:13
删除数据Delete方法,参数为struct的指针并且成为查询条件。注意:当删除时,如果user中包含有bool,float64或者float32类型,有可能会使删除失败 go package main import ( "fmt" _ "github.com/go sql driver/mysql ......
- 删除数据delete方法,参数为struct的指针并且成为查询条件。注意:当删除时,如果user中包含有bool,float64或者float32类型,有可能会使删除失败
package main import ( "fmt" _ "github.com/go-sql-driver/mysql" "github.com/go-xorm/xorm" "log" ) var engine *xorm.engine type user struct { name string `xorm:"varchar(25) 'name'"` id int `xorm:"pk 'id' autoincr"` } func main() { var err error engine, err = xorm.newengine("mysql", "root:123456@/test") if err != nil { log.fatal(err) return } //创建表和插入数据 /*err = engine.createtables(user{}) if err != nil { log.fatal(err) return } u := make([]user, 3) u[0].name = "u0" u[1].name = "u1" u[2].name = "u2" _, err = engine.insert(u) if err != nil { log.fatal(err) return }*/ uu := new(user) has ,err :=engine.id(2).get(uu) if err !=nil { log.fatal(err) return } if has { fmt.println("删除deleted,用户名:",uu.name) }else { fmt.println("该数据现在已经不存在") } affected, err := engine.id(2).delete(uu) if err != nil { log.fatal(err) return } fmt.println("删除delete,受影响的行数:", affected) fmt.println() has ,err =engine.id(2).get(uu) if err !=nil { log.fatal(err) return } if has { fmt.println("删除deleted,用户名:",uu.name,"deleteat:") }else { fmt.println("该数据现在已经不存在") } affected, err = engine.id(2).delete(uu) if err != nil { log.fatal(err) return } fmt.println("删除delete,受影响的行数:", affected) fmt.println() /* 输出: 删除deleted,用户名: u1 删除delete,受影响的行数: 1 该数据现在已经不存在 删除delete,受影响的行数: 0 */ }
-
软删除deleted, deleted可以让您不真正的删除数据,而是标记一个删除时间。使用此特性需要在xorm标记中使用deleted标记,对应的字段必须为time.time类型。在delete()时,deleted标记的字段将会被自动更新为当前时间而不是去删除该条记录
package main import ( "fmt" _ "github.com/go-sql-driver/mysql" "github.com/go-xorm/xorm" "log" "time" ) var engine *xorm.engine type user struct { name string `xorm:"varchar(25) 'name'"` id int `xorm:"pk 'id' autoincr"` version int `xorm:"version"` updatedat time.time `xorm:"updated"` deletedat time.time `xorm:"deleted"` } func main() { var err error engine, err = xorm.newengine("mysql", "root:123456@/test") if err != nil { log.fatal(err) return } //创建表和插入数据 /* err = engine.createtables(user{}) if err != nil { log.fatal(err) return } u := make([]user, 3) u[0].name = "u0" u[1].name = "u1" u[2].name = "u2" _, err = engine.insert(u) if err != nil { log.fatal(err) return }*/ uu := new(user) has ,err :=engine.id(2).get(uu) if err !=nil { log.fatal(err) return } if has { fmt.println("软删除deleted,用户名:",uu.name,"deleteat:",uu.deletedat) }else { fmt.println("该数据现在已经不存在") } affected, err := engine.id(2).delete(uu) if err != nil { log.fatal(err) return } fmt.println("软删除delete,受影响的行数:", affected) fmt.println() has ,err =engine.id(2).get(uu) if err !=nil { log.fatal(err) return } if has { fmt.println("软删除deleted,用户名:",uu.name,"deleteat:",uu.deletedat) }else { fmt.println("该数据现在已经不存在") } affected, err = engine.id(2).delete(uu) if err != nil { log.fatal(err) return } fmt.println("软删除delete,受影响的行数:", affected) fmt.println() //那么如果记录已经被标记为删除后,要真正的获得该条记录或者真正的删除该条记录,需要启用unscoped,如下所示: // 此时将可以获得记录,此时将可以真正的删除记录 has ,err =engine.id(2).unscoped().get(uu) if err !=nil { log.fatal(err) return } if has { fmt.println("启用unscoped,用户名:",uu.name,"deleteat:",uu.deletedat) }else { fmt.println("该数据现在已经不存在") } affected, err = engine.id(2).unscoped().delete(uu) if err != nil { log.fatal(err) return } fmt.println("启用unscoped,受影响的行数:", affected) /* 输出: 软删除deleted,用户名: u1 deleteat: 0001-01-01 00:00:00 +0000 utc 软删除deleted,受影响的行数: 1 该数据现在已经不存在 软删除deleted,受影响的行数: 0 启用unscoped,用户名: u1 deleteat: 2019-05-28 15:12:38 +0800 cst 启用unscoped,受影响的行数: 1 */ }
下一篇: Java原子操作类,你知道多少?