The installation

Iris is cross-platform software. The only requirement is the Go language (1.13+)

Go to the project root directory:

$CD your_project_path GO111MODULE = on $$go env - w go env - w GOPROXY = https://goproxy.cn, direct # golang agent $go a get github.com/kataras/iris/v12@masterCopy the code

Or edit the go.mod file

1.13 the require module your_project_name go (github.com/kataras/iris/v12 v12.0.0)Copy the code

Run the go build command

If the installation fails, clear the module cache

Go clean –modcache # Clear the contents of the mod folder

Project Version Update

Go -get command to get the latest version of Iris. The Master branch is usually stable enough.

$ go get -u github.com/kataras/iris/v12@latest

Refer to mVC-LOGIN to initialize the project code

Go to your project path your_project_path,

Copy iris MVC – login GOPATH/pkg/mod/github.com/kataras/iris/[email protected]/_examples/mvc/login demo files to the project directory Your_project_path;

  • Create a new one in this directoryconffolder
    • New folderdb.goDatabase configuration file
  • datamodelsChange folder name tomodels
  • repositoriestodao
  • Modify the folder as neededpackageAnd the class name

Create a database table

# table structure
CREATE TABLE `player_info` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name_zh` varchar(50) NOT NULL DEFAULT ' ' COMMENT 'Chinese name'.`name_en` varchar(50) NOT NULL DEFAULT ' ' COMMENT 'English name'.`avatar` varchar(255) NOT NULL DEFAULT ' ' COMMENT 'avatar'.`birthday` varchar(50) NOT NULL DEFAULT ' ' COMMENT Date of birth.`height` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Height, in cm'.`weight` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Weight, in g'.`club` varchar(50) NOT NULL DEFAULT ' ' COMMENT 'Club'.`jersy` varchar(50) NOT NULL DEFAULT ' ' COMMENT 'Jersey number and main location'.`country` varchar(50) NOT NULL DEFAULT ' ' COMMENT 'citizenship'.`birthaddress` varchar(255) NOT NULL DEFAULT ' ' COMMENT 'Birthplace'.`feature` varchar(255) NOT NULL DEFAULT ' ' COMMENT 'Personal Characteristics'.`moreinfo` text COMMENT 'More about'.`play_status` tinyint(4) NOT NULL DEFAULT '0' COMMENT 'Status, default 0 normal, 1 deleted'.`created_at` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Creation time'.`updated_at` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Last modified time',
  PRIMARY KEY (`id`))ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
Copy the code

Add data to a data table

INSERT INTO `player_info`(`id`, `name_zh`, `name_en`, `avatar`, `birthday`, `height`, `weight`, `club`, `jersy`, 'country ',' birthAddress ', 'feature ',' moreinfo ', 'play_status ',' created_at ', 'updated_at' VALUES (1, 'kak ', 'kk', '/ img/star/kk.jpeg', '1982-04-22', 192, 84, 'Barcelona ', '18',' Brazil ', 'Brazil ',' Strong body, fast running, good dribbling and a good long shot, A super midfielder who scores goals easily. ', NULL, 0, 0, 1618283254); INSERT INTO `player_info`(`id`, `name_zh`, `name_en`, `avatar`, `birthday`, `height`, `weight`, `club`, `jersy`, 'country ',' birthAddress ', 'feature ',' moreinfo ', 'play_status ',' created_at ', 'updated_at' VALUES (2, 'BirthAddress ',' birthAddress ', 'feature ',' moreinfo ', 'play_status ',' created_at ', '/ public/img/star/zhengzhi. Jpeg', '1980-08-20', 180, 75, hangzhou, '12', 'China' and 'China' and 'zheng zhi can sainz no matter which position in midfield, He was charlton's most powerful midfield engine when he was close to the goal. ', NULL, 0, 0, 1618283248);Copy the code

Generate database mapping Models

Create a new folder template configuration under your_project_path to generate a database mapping configuration based on the Xorm database mapping tool usage

Template_config Enables Xorm database mapping

lang=go
genJson=1
prefix=
Copy the code

Structtemplate.go. TPL Generates a data mapping template

package {{.Models}}

{{$ilen := len .Imports}}
{{if gt $ilen 0}}
import (
    {{range .Imports}}"{{.}}"{{end}}
)
{{end}}

{{range .Tables}}
type {{Mapper .Name}} struct {
{{$table := .}}
{{range .ColumnsSeq}}{{$col := $table.GetColumn .}} {{Mapper $col.Name}}	{{Type $col}} {{Tag $table $col}}
{{end}}
}

{{end}}
Copy the code

After completing the above steps, execute the database mapping command in your_project_path, the project root directory

Xorm reverse mysql [username]:[username]@/[username]? charset=utf8 conf/template

After the command is executed, the player_info.go file is generated in the default path folder models with the following code:

package models

type PlayerInfo struct {
	Id           int    'xorm:"not null PK autoincr comment('主键ID') INT(10)"
	NameZh       string 'xorm:"not null default comment('中文名') VARCHAR(50)"
	NameEn       string 'xorm:"not null default comment('英文名') VARCHAR(50)"
	Avatar       string 'xorm:"not null default comment(' avatar ') VARCHAR(255)"
	Birthday     string 'xorm:"not null default comment(' date ') VARCHAR(50)"
	Height       int    'xorm:"not null default 0 comment(' height, 表 cM ') INT(10)"
	Weight       int    'xorm:"not null default 0 comment(' 表 名, 表 名 ') INT(10)"
	Club         string 'xorm:"not null default comment(' club ') VARCHAR(50)"
	Jersy        string 'xorm:"not null default comment(' VARCHAR(50) ')
	Country      string 'xorm:"not null default comment(' nationality ') VARCHAR(50)"
	Birthaddress string 'xorm:"not null default comment(' address ') VARCHAR(255)"
	Feature      string 'xorm:"not null default comment(' 评 论 ') VARCHAR(255)"
	Moreinfo     string 'xorm:"comment(' more on ') TEXT"'
	PlayStatus   int    'xorm:"not null default 0 comment(' status, default 0, 1 deleted ') TINYINT(4)"
	CreatedAt    int    'xorm:"not null default 0 comment(' createtime ') INT(10)"
	UpdatedAt    int    'xorm:"not null default 0 comment(' 解 决 时 ') INT(10)"
}
Copy the code

Now that the project directory initialization is complete, in the next section we will use Xorm for the Dao and Service layer to encapsulate specific database operations