作用域允许您重用常用逻辑,共享逻辑需要定义为类型

func(*gorm.DB) *gorm.DB

查询

func Scope1(db *gorm.DB) *gorm.DB {
return db.Where("name = ?", "哈哈哈")
}
func Scope2(slice1 []int) func(tx *gorm.DB) *gorm.DB {
return func(tx *gorm.DB) *gorm.DB {
return tx.Where("age IN (?)", slice1)
}
} var users []User
db.Debug().Scopes(Scope2([]int{7, 55})).Find(&users)
fmt.Println(users)

分页

func Paginate(ctx *gin.Context) func(db *gorm.DB) *gorm.DB {
// 分页功能实现
return func(db *gorm.DB) *gorm.DB {
page, _ := strconv.Atoi(ctx.Query("page"))
if page == 0 {
page = 1
}
size, _ := strconv.Atoi(ctx.Query("size"))
fmt.Println(size)
switch{
case size > 100:
size = 100
case size < 1:
size = 10
}
offset := (page - 1) * size
return db.Offset(offset).Limit(size)
}
} // 分页
router := gin.Default() router.GET("/json", func(context *gin.Context) {
var users []User
db.Scopes(Paginate(context)).Find(&users) // 等价于:Paginate(context)(db).Find(&users)
context.JSON(http.StatusOK, users)
}) router.Run(":8000")

更新

Scope 更新、删除示例:

func CurUser(ctx *gin.Context) func(db *gorm.DB) *gorm.DB {
return func(db *gorm.DB) *gorm.DB {
name := ctx.Query("name")
if name != "" {
var user User
fmt.Println(name)
if db.Session(&gorm.Session{}).First(&user, "name = ?", name).Error == nil {
return db.Where("id = ?", user.ID)
}
}
db.AddError(errors.New("invalid user"))
return db
}
} // 分页
router := gin.Default() router.GET("/json", func(context *gin.Context) {
var users []User
// 更新 //db.Model(new(User)).Scopes(CurUser(context)).Update("name", "新版user")
// 删除
db.Scopes(CurUser(context)).Delete(&User{})
context.JSON(http.StatusOK, users)
}) router.Run(":8000")

gorm中的scope的更多相关文章

  1. 依赖注入及AOP简述(十)——Web开发中常用Scope简介 .

    1.2.    Web开发中常用Scope简介 这里主要介绍基于Servlet的Web开发中常用的Scope. l        第一个比较常用的就是Application级Scope,通常我们会将一 ...

  2. 理解vue中的scope的使用

    理解vue中的scope的使用 我们都知道vue slot插槽可以传递任何属性或html元素,但是在调用组件的页面中我们可以使用 template scope="props"来获取 ...

  3. MAVEN中的Scope

    Dependency Scope 在POM 4中,<dependency>中还引入了<scope>,它主要管理依赖的部署.目前<scope>可以使用6个值: *im ...

  4. Maven依赖中的scope详解

    scope的分类 compile 默认就是compile,什么都不配置也就是意味着compile.compile表示被依赖项目需要参与当前项目的编译,当然后续的测试,运行周期也参与其中,是一个比较强的 ...

  5. Maven依赖中的scope详解,在eclipse里面用maven install可以编程成功,到服务器上用命令执行报VM crash错误

    Maven依赖中的scope详解 项目中用了<scope>test</scope>在eclipse里面用maven install可以编译成功,到服务器上用命令执行报VM cr ...

  6. angularjs指令中的scope

    共享 scope 使用共享 scope 的时候,可以直接从父 scope 中共享属性.因此下面示例可以将那么属性的值输出出来.使用的是父 scope 中定义的值. js代码: app.controll ...

  7. angularjs 中的scope继承关系——(2)

    转自:http://www.lovelucy.info/understanding-scopes-in-angularjs.html angularjs 中的scope继承关系 ng-include ...

  8. spring中的scope详解

    spring容器中的bean默认是单例模式的,改成非单例模式需要在类上加上@Scope("prototype") 1. scope概论 spring中scope是一个非常关键的概念 ...

  9. HTML表格中<td scope="col">与<td scope="row">的含义

    HTML表格中<td scope="col">与<td scope="row">的含义 表格中 <td scope="c ...

随机推荐

  1. 【LeetCode】1. Two Sum 两数之和

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:two sum, 两数之和,题解,leetcode, 力 ...

  2. 【LeetCode】576. Out of Boundary Paths 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 状态搜索 记忆化搜索 相似题目 参考资料 ...

  3. 1246 - Colorful Board

    1246 - Colorful Board    PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB ...

  4. Cookie、Session、Token、JWT

    什么是认证(Authentication)------->就是验证当前用户的身份,证明"你是你自己" 互联网中的认证: 用户名密码登录 邮箱发送登录链接 手机号接收验证码 只 ...

  5. 解决ubuntu突然无法联网问题

    一.问题描述 今天使用笔记本远程办公的时候,突然电脑无法联网了,使用chrome浏览器访问网页出现如下错误 This site can't be reachedwww.baidu.com's serv ...

  6. VAE with a VampPrior

    目录 概 主要内容 分级的VAE 代码 Tomczak J. & Welling M. VAE with a VampPrior. In International Conference on ...

  7. TGAN

    目录 概 主要内容 Saito M., Matsumoto E. & Saito S. Temporal Generative Adversarial Nets with Singular V ...

  8. 【MySQL作业】MySQL函数——美和易思字符串函数应用习题

    点击打开所使用到的数据库>>> 1.将所有客户的姓名与电话以"-"作为分隔符进行连接显示. 使用 concat(s1,s2,-) 函数将所有客户的姓名与电话以&q ...

  9. 【ElasticSearch】异常 Request cannot be executed; I/O reactor status: STOPPED

    Caused by: java.lang.RuntimeException: Request cannot be executed; I/O reactor status: STOPPED at or ...

  10. xorm 条件查询时区的问题

    问题描述:如果在查询的时候,直接传时间格式作为条件,时间会被驱动程序转为UTC格式,因此会有8个小时的误差. 解决方案1: 将查询时间转为字符串 db.where("time > ?& ...