插入


指定主键id

  1. var d Dbtest
  2. d.Id = 10
  3. d.Param1 = 100
  4. d.Param2 = "test"
  5. dao := NewDbtestDao()
  6. affect, err := dao.Create(&d) //affect为更新成功行数,类型int64
  7. if err != nil {
  8. return nil
  9. }

自增主键id

  1. var d Dbtest //不指定主键id,值为0时,按db自增ID插入
  2. d.Param1 = 100
  3. d.Param2 = "test"
  4. dao := NewDbtestDao()
  5. affect, err := dao.Create(&d)
  6. if err != nil {
  7. return nil
  8. }
  9. id := d.Id //插入成功后,原变量Id字段为插入自增主键id