gin项目目录:
本地数据库文件:
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `user`
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(60) DEFAULT NULL,
`age` int(4) DEFAULT NULL,
`info` varchar(60) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=25 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('1', 'slene', '81', 'Info');
INSERT INTO `user` VALUES ('2', 'slene81', '81', 'Info81');
INSERT INTO `user` VALUES ('3', 'slene87', '87', 'Info87');
INSERT INTO `user` VALUES ('4', 'slene47', '47', 'Info47');
INSERT INTO `user` VALUES ('5', 'slene59', '59', 'Info59');
INSERT INTO `user` VALUES ('6', 'slene81', '81', 'Info81');
INSERT INTO `user` VALUES ('7', 'slene18', '18', 'Info18');
INSERT INTO `user` VALUES ('8', 'slene25', '25', 'Info25');
INSERT INTO `user` VALUES ('9', 'slene40', '40', 'Info40');
INSERT INTO `user` VALUES ('10', 'slene56', '56', 'Info56');
INSERT INTO `user` VALUES ('11', 'slene0', '0', 'Info0');
INSERT INTO `user` VALUES ('12', 'slene94', '94', 'Info94');
INSERT INTO `user` VALUES ('13', 'slene11', '11', 'Info11');
INSERT INTO `user` VALUES ('14', 'slene62', '62', 'Info62');
INSERT INTO `user` VALUES ('15', 'slene89', '89', 'Info89');
INSERT INTO `user` VALUES ('16', 'slene28', '28', 'Info28');
INSERT INTO `user` VALUES ('17', 'slene74', '74', 'Info74');
INSERT INTO `user` VALUES ('18', 'slene11', '11', 'Info11');
INSERT INTO `user` VALUES ('19', 'slene45', '45', 'Info45');
INSERT INTO `user` VALUES ('20', 'slene37', '37', 'Info37');
INSERT INTO `user` VALUES ('21', 'slene6', '6', 'Info6');
INSERT INTO `user` VALUES ('22', 'slene95', '95', 'Info95');
INSERT INTO `user` VALUES ('23', 'slene66', '66', 'Info66');
INSERT INTO `user` VALUES ('24', 'slene28', '28', 'Info28');
main.go文件源码:
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
"net/http"
)
type User struct {
Id int
Name string
Age int
Info string
Profile Profile
ProfileID int
}
type Profile struct {
Id int
Name string
}
func main() {
// 1.创建路由
r := gin.Default()
// 2.绑定路由规则,执行的函数
// gin.Context,封装了request和response
r.LoadHTMLGlob("temp/*")
//r.GET("/:contr", func(c *gin.Context) {
//
//
// c.HTML(http.StatusOK, "index.html", gin.H{"title": "我是测试", "u": users})
//})
r.GET("/:contr", contr )
// 3.监听端口,默认在8080
// Run("里面不指定端口号默认为8080")
r.Run(":8000")
}
func contr(c *gin.Context){
db, err := gorm.Open("mysql", "root:root@/godb?charset=utf8&parseTime=True&loc=Local")
defer db.Close()
if err != nil {
fmt.Println("数据库崩了 ")
}
//条件Or 与WHERE对立
var users []User
db.Not(&User{Id: 3}).Or(Id: 4).Find(&users)
fmt.Println(users)
//NOT查询 STRUCT模式 条件非 与WHERE对立
//var users []User
//db.Not(&User{Id: 3}).Find(&users)
//fmt.Println(users)
多记录查询 STRUCT模式
//var users []User
//db.Where(&User{Id: 3}).Find(&users)
//fmt.Println(users)
单 记录 查询SQK模式
//user := new(User)
//b := db.Where("id = ?",2).First(&user)// PS: 方法不存在了
//fmt.Println(b)
//fmt.Println(user)
//多记录查询
//var users []User
//db.Where("id > ?",2).Find(&users)
//fmt.Println(users)
c.HTML(http.StatusOK, "index.html", gin.H{"title": "我是测试", "u": users})
}