zoukankan      html  css  js  c++  java
  • xorm update 当传入的为结构体指针时,只有非空和0的field才会被作为更新的字段

    Update方法

    更新数据使用Update方法,Update方法的第一个参数为需要更新的内容,可以为一个结构体指针或者一个Map[string]interface{}类型。当传入的为结构体指针时,只有非空和0的field才会被作为更新的字段。当传入的为Map类型时,key为数据库Column的名字,value为要更新的内容。

    user := new(User)
    user.Name = "myname"
    affected, err := engine.Id(id).Update(user)
    

    这里需要注意,Update会自动从user结构体中提取非0和非nil得值作为需要更新的内容,因此,如果需要更新一个值为0,则此种方法将无法实现,因此有两种选择:

    1.通过添加Cols函数指定需要更新结构体中的哪些值,未指定的将不更新,指定了的即使为0也会更新。

    affected, err := engine.Id(id).Cols("age").Update(&user)
    

    2.通过传入map[string]interface{}来进行更新,但这时需要额外指定更新到哪个表,因为通过map是无法自动检测更新哪个表的。

    affected, err := engine.Table(new(User)).Id(id).Update(map[string]interface{}{"age":0})
    
  • 相关阅读:
    Spring_依赖注入DI
    Spring_懒加载与非懒加载
    Spring_提示模板配置/搭建spring框架/单例与多例/初始化方法和销毁方法
    Spring
    Mybatis_二级缓存
    Mybatis_一级缓存
    Mybatis_一对多延迟加载
    Mybatis_一对一查询
    MapReduce的核心资料索引 [转]
    Hadoop家族的各个成员
  • 原文地址:https://www.cnblogs.com/ithubb/p/15123621.html
Copyright © 2011-2022 走看看