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
}
func main() {
// 1.创建路由
r := gin.Default()
// 2.绑定路由规则,执行的函数
// gin.Context,封装了request和response
r.LoadHTMLGlob("temp/*")
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("数据库崩了 ")
}
//db.Exec("DROP TABLE users;") //删表
//删除 记录操作
//status := db.Exec("delete users WHERE id = ? ",7)
//fmt.Println(status)
//更新数据操作
//status := db.Exec("UPDATE users SET name =? WHERE id = ? ", "li si ",4)
//fmt.Println(status)
//查询一条记录
//var user User
//db.Raw("SELECT id,name, age,info FROM users WHERE id = ?", 3).Scan(&user)
//fmt.Println(user)
//查询多条记录
var users []User
err1 := db.Raw("SELECT id,name, age,info FROM users WHERE id > ?", 3).Scan(&users)
fmt.Println(err1)
fmt.Println(users)
c.HTML(http.StatusOK, "index.html", gin.H{"title": "我是测试"})
}