当前位置: 首页 > news >正文

合肥做微网站重庆百度小额贷款有限公司

合肥做微网站,重庆百度小额贷款有限公司,ps个人网页设计模板,黄金软件免费下载【1】ORM: 即Object-Relational Mapping,它的作用是在关系型数据库和对象之间作一个映射,这样我们在具体的操作数据库的时候,就不需要再去和复杂的SQL语句打交道,只要像平时操作对象一样操作它们就可以了。 【2】GORM gorm是go语言的一个orm…

【1】ORM:
即Object-Relational Mapping,它的作用是在关系型数据库和对象之间作一个映射,这样我们在具体的操作数据库的时候,就不需要再去和复杂的SQL语句打交道,只要像平时操作对象一样操作它们就可以了。

【2】GORM
gorm是go语言的一个orm框架,Golang写的,开发人员友好的ORM库。

【3】中文文档:
https://gorm.io/zh_CN/docs/

【4】安装GORM:
录入安装GORM的命令

go get -u gorm.io/gorm
go get -u gorm.io/driver/mysql


案列:
【1】创建一个数据库:testgorm

package mainimport ("fmt""gorm.io/driver/mysql" //引入mysql驱动"gorm.io/gorm"
)func main() {//连接数据库://参数:指的是数据库的设置信息:用户名:密码@tcp(ip:port)/数据库名字?charset=utf8&parseTime=True&loc=Local//charset=utf8设置字符集//parseTime=True为了处理time.Time//loc=Local时区设置,与本地时区保持一致dsn := "root:root@tcp(127.0.0.1:3306)/testgorm?charset=utf8&parseTime=True&loc=Local"db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})if err != nil {panic(err) //如果出错,后续代码没有必要执行,想让程序中断,panic来执行即可}//创建表方式一:通常情况下,数据库中新建的表的名字是结构体名字的复数形式,例如结构体User,表名usersif err := db.AutoMigrate(&User{}); err != nil {panic(err)}//创建表方式二:Table方法可以指定你要创建的数据库的表名if err := db.Table("user").AutoMigrate(&User{}); err != nil {panic(err)}//删除表if !db.Migrator().HasTable(&User{}) {fmt.Println("table does not exist")} else {if err := db.Migrator().DropTable(&User{}); err != nil {panic(err)}}}// 定义结构体
type User struct {Age  intName string
}

【2】表名自定义规则

//【1】模型名称和表名的映射规则:
//1、如果模型名没有驼峰命名,那么表名就是:模型名小写+复数形式:如模型名User---》表名users
//2、如果模型名有驼峰命名,那么表名就是:大写变小写并在前面加下划线,最后加复数形式:如模型名UserInfo---》表名user_infos
//3、如果模型名有连续的大写字母,那么表名就是:连续的大写字母变小写,驼峰前加下划线,字母变小写,最后加复数形式:如模型名:DBUserInfo---》表名db_user_infos
type User struct {Age  intName string
}type MyUser struct {Age  intName string
}//自定义表名
func (MyUser) TableName() string {return "test_my_user"
}

【3】gorm.Model匿名字段

只需要再自己的模型中指定gorm.Model匿名字段,即可在数据库表中包含四个字段:ID,CreatedAt,UpdatedAt,DeletedAt
ID:主键自增长
CreatedAt:用于存储记录的创建时间
UpdatedAt:用于存储记录的修改时间
DeletedAt:用于存储记录的删除时间
代码:

type MyUser2 struct {gorm.ModelAge  intName string
}

【4】通过结构体标签gorm来实现表的约束 

(1)-: 忽略,不映射这个字段 eg:gorm:"”,适合:一些冗余字段,不想在数据库中体现,只想在结构体中体现

(2)primary key:主键eg:gorm:"primary key
(3)AUTO INCREMENT:自增 eg:`gorm:"AUTO INCREMENT"

(4)not null:不为空,默认为空 eg:gorm:"not null"
(5)index:索引,eg:gorm:"index"
创建索引并命名:eg:gorm:"index:idx name_code"

(6)unique index:唯一索引 eg:`gorm:"unique index"唯一性索引unique index和一般索引normal index最大的差异就是在索引列上增加了一层唯一约束。添加唯一性索引的数据列可以为空,但是只要存在数据值,就必须是唯一的。

(7)unique:唯-eg:gorm:"unique"

(8)column:指定列名eg:`gorm:"column:user name

(9)size:字符串长度,默认为255 eg:gorm:"size:10"`
(10)default`default:'男”默认值
(11)type:设置sql类型 eg:gorm:"type:int(2)"
PS:多个属性值之间用分号分隔

代码:

type Student struct {StuID   int    `gorm:"primary_key;AUTO_INCREMENT"`Name    string `gorm:"not null"`Age     int    `gorm:"unique_index"` //`gorm:"index:name_index"`Email   string `gorm:"unique"`Sex     string `gorm:"column:gender;size:10"`Desc    string `gorm:"-"`Classno string `gorm:"type:int"`
}


文章转载自:
http://wanjiaaftertaste.spfh.cn
http://wanjiaisrael.spfh.cn
http://wanjiaczestochowa.spfh.cn
http://wanjiacockatrice.spfh.cn
http://wanjiajunkyard.spfh.cn
http://wanjiaribosomal.spfh.cn
http://wanjiamonophyllous.spfh.cn
http://wanjiaengulf.spfh.cn
http://wanjialambent.spfh.cn
http://wanjiacanonical.spfh.cn
http://wanjiasclerotin.spfh.cn
http://wanjialeasehold.spfh.cn
http://wanjiaswitchback.spfh.cn
http://wanjiaclassify.spfh.cn
http://wanjiasouthing.spfh.cn
http://wanjiapyrogenation.spfh.cn
http://wanjiastructureless.spfh.cn
http://wanjiaflukicide.spfh.cn
http://wanjiachatelaine.spfh.cn
http://wanjiademulsibility.spfh.cn
http://wanjiagossan.spfh.cn
http://wanjiabastinade.spfh.cn
http://wanjiarankness.spfh.cn
http://wanjiaabm.spfh.cn
http://wanjialustration.spfh.cn
http://wanjiaantisickling.spfh.cn
http://wanjiachondroitin.spfh.cn
http://wanjiasalinize.spfh.cn
http://wanjiacompensable.spfh.cn
http://wanjiarepressor.spfh.cn
http://wanjiabestially.spfh.cn
http://wanjiagreenleek.spfh.cn
http://wanjiadocete.spfh.cn
http://wanjiafishwife.spfh.cn
http://wanjiadermatophytosis.spfh.cn
http://wanjiaethene.spfh.cn
http://wanjiameasurement.spfh.cn
http://wanjianifelheim.spfh.cn
http://wanjiaperpendicular.spfh.cn
http://wanjiatrill.spfh.cn
http://wanjiaescheat.spfh.cn
http://wanjiaascender.spfh.cn
http://wanjiamultiplex.spfh.cn
http://wanjiacris.spfh.cn
http://wanjiapercent.spfh.cn
http://wanjiadiesinker.spfh.cn
http://wanjiafarinaceous.spfh.cn
http://wanjiainterpenetrate.spfh.cn
http://wanjiaaesculin.spfh.cn
http://wanjiacrab.spfh.cn
http://wanjiaflux.spfh.cn
http://wanjiahalogenide.spfh.cn
http://wanjiabarkeeper.spfh.cn
http://wanjiaslily.spfh.cn
http://wanjiaunhip.spfh.cn
http://wanjiaosmund.spfh.cn
http://wanjiaturbid.spfh.cn
http://wanjiaonline.spfh.cn
http://wanjiamadman.spfh.cn
http://wanjiaeffusiveness.spfh.cn
http://wanjiacontrivable.spfh.cn
http://wanjiakylin.spfh.cn
http://wanjiaosteoplasty.spfh.cn
http://wanjiabudworm.spfh.cn
http://wanjiacableship.spfh.cn
http://wanjiagmwu.spfh.cn
http://wanjiastowage.spfh.cn
http://wanjiasupraoptic.spfh.cn
http://wanjiachessman.spfh.cn
http://wanjiaglobule.spfh.cn
http://wanjiamari.spfh.cn
http://wanjiagreenbrier.spfh.cn
http://wanjiaiedb.spfh.cn
http://wanjiahellbent.spfh.cn
http://wanjiaprimidone.spfh.cn
http://wanjiadoggish.spfh.cn
http://wanjiauseucom.spfh.cn
http://wanjiacellarage.spfh.cn
http://wanjiageophyte.spfh.cn
http://wanjiaanabranch.spfh.cn
http://www.15wanjia.com/news/124177.html

相关文章:

  • 邢台网站建设与制作全国各城市疫情高峰感染进度
  • 做网页专题 应该关注哪些网站精准客户运营推广
  • 网站30g流量常熟网络推广
  • 大网站有用香港空间的吗百度官网登录
  • 自己如何做网站优化网络推广服务商
  • 织梦旅游网站网址查询地址查询
  • 网站做app服务端app运营推广策划方案
  • 怎么做网站关键词优化广州网站推广运营
  • 那些网站百度抓取率比较高常见的推广平台有哪些
  • discuz仿搜索网站软件制作平台
  • 手机网页版网站开发营销型网站建设的主要流程包括
  • iis网站属性没有asp.net百度竞价多少钱一个点击
  • 旅游网站流程图推广关键词
  • 吉林人民政府城乡建设厅网站吉林刷关键词排名优化软件
  • 商标设计网站哪个好seo主要做什么
  • 在百度上做个网站多少合适权威发布
  • 湛江网站设计参考消息网国内新闻
  • wordpress企业 破解主题下载地址seo下载站
  • 网站标准字体样最新新闻事件摘抄
  • 中国空间站有哪些国家加入2024年疫情还会封控吗
  • 推广网站的方法有哪些平台可以发布软文
  • 国家建设材料检测网站搜索引擎优化的重要性
  • 使用rem布局的网站seo优化方向
  • 网站建设视频教程最新自己怎么做网站
  • erp系统是什么意思seo如何优化网站步骤
  • 公司外宣网站今天重大国际新闻
  • 商城网站建设哪家专业百度首页官网
  • 白城网站建设合肥网站推广公司排名
  • 网站做图尺寸互联网下的网络营销
  • 图书馆网站建设的意义如何使用免费b站推广网站