diff --git a/beego_demo1/controllers/cache/memoryController.go b/beego_demo1/controllers/cache/memoryController.go new file mode 100644 index 0000000..c8075eb --- /dev/null +++ b/beego_demo1/controllers/cache/memoryController.go @@ -0,0 +1,21 @@ +package cache + +import ( + "github.com/astaxie/beego" + "github.com/astaxie/beego/cache" + "github.com/astaxie/beego/logs" +) + +type MemoryController struct { + beego.Controller +} + +func (t *MemoryController) Get() { + mc, err := cache.NewCache("memory", `{"interval":"60"}`) + if err != nil { + logs.Error("创建缓存报错了") + } + mc.Put("name", "朱毅骏", 20) + logs.Info(mc.Get("name")) + t.TplName = "cache/cache.html" +} diff --git a/beego_demo1/controllers/log/logController.go b/beego_demo1/controllers/log/logController.go new file mode 100644 index 0000000..0c849f4 --- /dev/null +++ b/beego_demo1/controllers/log/logController.go @@ -0,0 +1,34 @@ +package log + +import ( + "github.com/astaxie/beego" + "github.com/astaxie/beego/logs" +) + +type InternalLogController struct { + beego.Controller +} + +func (t *InternalLogController) Get() { + //日志文件 + logs.SetLogger("file", `{"filename":"logs/test_internal.log"}`) + //删除控制台日志 + //logs.GetBeeLogger().DelLogger("console") + //设置打印日志级别 + logs.SetLevel(logs.LevelDebug) + //日志级别从0开始 + logs.Emergency("this is a emergency,level 0") + logs.Alert("this is a alert,,level 1") + logs.Critical("this is a Critical,level 2") + logs.Error("this is a Error,level 3") + logs.Warning("this is a Warning,level 4") + logs.Notice("this is a Notice,level 5") + logs.Info("this is a Info,level 6") + logs.Informational("this is a Informational,level 6") + logs.Debug("this is a Debug,level 7") + t.TplName = "log/log.html" +} + +func (t *InternalLogController) Post() { + t.TplName = "log/log.html" +} diff --git a/beego_demo1/controllers/test_orm/ormController2.go b/beego_demo1/controllers/test_orm/ormController2.go index 768476d..82adb6c 100644 --- a/beego_demo1/controllers/test_orm/ormController2.go +++ b/beego_demo1/controllers/test_orm/ormController2.go @@ -79,7 +79,8 @@ func (t *OrmController2update) Post() { id, _ := t.GetInt64("id") title := t.GetString("title") author := t.GetString("author") - article := models.Article{Id: int(id), Title: title, Author: author} + readCount, _ := t.GetInt64("readCount") + article := models.Article{Id: int(id), Title: title, Author: author, ReadCount: int(readCount)} count, err := o.Update(&article) if err != nil { log.Errorf("更新失败") diff --git a/beego_demo1/controllers/test_orm/ormController3.go b/beego_demo1/controllers/test_orm/ormController3.go index 79252a4..c69635b 100644 --- a/beego_demo1/controllers/test_orm/ormController3.go +++ b/beego_demo1/controllers/test_orm/ormController3.go @@ -11,16 +11,47 @@ type OrmController3 struct { beego.Controller } +/** +复杂查询 +*/ func (t *OrmController3) Get() { o := orm.NewOrm() - - // //o.QueryTable("article") article := models.Article{} - articles := []models.Article{} + var articles []models.Article qs := o.QueryTable(article) + //大小写敏感 //qs.Filter("title__exact","西游记").One(&article) - qs.All(&articles) - log.Info(articles) + ////大小写不敏感 + //qs.Filter("title__iexact","西游记").One(&article) + + //模糊查询 + //大小写敏感 + //qs.Filter("title__contains","游").One(&article) //LIKE BINARY + //大小写不敏感 + //qs.Filter("title__icontains","Z").One(&article) //LIKE + + //查询所有 + //qs.GroupBy("id").All(&articles) + //log.Info("list:",articles) + + //gt 大于 gte 大于等于 + //qs.Filter("read_count__gt",4).One(&article) + //qs.Filter("read_count__gte",4).One(&article) + //lt 小于 lte 小于等于 + //qs.Filter("read_count__lt",100).One(&article) + //qs.Filter("read_count__lte",100).All(&articles) + //以什么开始 + //qs.Filter("title__startswith","z").All(&articles) + //qs.Filter("title__istartswith","z").All(&articles) + //以什么结束 + //qs.Filter("title__endswith","n").All(&articles) + //qs.Filter("title__iendswith","n").All(&articles) + + //qs.Filter("read_count__in",3,0,20,100).All(&articles) + qs.Filter("read_count__isnull", false).All(&articles) + + log.Info("one打印:", article) + log.Info("all打印:", articles) t.TplName = "test_orm/orm3.html" } diff --git a/beego_demo1/controllers/test_orm/ormController4.go b/beego_demo1/controllers/test_orm/ormController4.go new file mode 100644 index 0000000..3d578f7 --- /dev/null +++ b/beego_demo1/controllers/test_orm/ormController4.go @@ -0,0 +1,28 @@ +package test_orm + +import ( + "beego_demo1/models" + "github.com/astaxie/beego" + "github.com/astaxie/beego/orm" + "github.com/prometheus/common/log" +) + +type OrmController4 struct { + beego.Controller +} + +func (t *OrmController4) Get() { + o := orm.NewOrm() + articles := []models.Article{} + qs := o.QueryTable(new(models.Article)) + //qs.Filter("title__exact","西游记").All(&articles,"title","author") + //qs.Filter("title__exact","西游记").Filter("author__exact","吴承恩").All(&articles,"Id","Title","Author","ReadCount") + //排除 + //qs.Exclude("title__iexact","zhuyijun").All(&articles) + + //qs.Limit(2,1).All(&articles) + qs.Limit(10).Offset(0).All(&articles) + log.Info(articles) + + t.TplName = "test_orm/orm4.html" +} diff --git a/beego_demo1/main.go b/beego_demo1/main.go index 83ae1a3..b4c60a6 100644 --- a/beego_demo1/main.go +++ b/beego_demo1/main.go @@ -37,5 +37,7 @@ func main() { beego.BConfig.WebConfig.EnableXSRF = true beego.BConfig.WebConfig.XSRFKey = "9029zyjlpydzxzyq9029" beego.BConfig.WebConfig.XSRFExpire = 3600 + //开启sql打印 + orm.Debug = true beego.Run() } diff --git a/beego_demo1/models/orm2.go b/beego_demo1/models/orm2.go index d8387c7..fda781e 100644 --- a/beego_demo1/models/orm2.go +++ b/beego_demo1/models/orm2.go @@ -3,9 +3,10 @@ package models import "github.com/astaxie/beego/orm" type Article struct { - Id int `orm:"pk"` - Title string `orm:"title"` - Author string `orm:"author"` + Id int `orm:"pk"` + Title string `orm:"column(title)"` + Author string `orm:"column(author)"` + ReadCount int `orm:"column(read_count)"` } func (a *Article) TableName() string { diff --git a/beego_demo1/routers/router.go b/beego_demo1/routers/router.go index 922dbd3..70cf03f 100644 --- a/beego_demo1/routers/router.go +++ b/beego_demo1/routers/router.go @@ -2,6 +2,8 @@ package routers import ( "beego_demo1/controllers" + "beego_demo1/controllers/cache" + "beego_demo1/controllers/log" "beego_demo1/controllers/test_orm" "github.com/astaxie/beego" ) @@ -42,4 +44,8 @@ func init() { //exper表达式 beego.Router("/orm3", &test_orm.OrmController3{}) + beego.Router("/orm4", &test_orm.OrmController4{}) + //log + beego.Router("/log", &log.InternalLogController{}) + beego.Router("/memory", &cache.MemoryController{}) } diff --git a/beego_demo1/sql/article.sql b/beego_demo1/sql/article.sql index 3b2ac64..eb1e15e 100644 --- a/beego_demo1/sql/article.sql +++ b/beego_demo1/sql/article.sql @@ -25,12 +25,13 @@ CREATE TABLE `article` ( `id` int(11) NOT NULL AUTO_INCREMENT, `title` varchar(64) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL, `author` varchar(20) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL, + `read_count` int DEFAULT 0, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_unicode_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Records of article -- ---------------------------- -INSERT INTO `article` VALUES (1, '西游记', '吴承恩'); +INSERT INTO `article` VALUES (1, '西游记', '吴承恩',0); SET FOREIGN_KEY_CHECKS = 1; diff --git a/beego_demo1/views/cache/cache.html b/beego_demo1/views/cache/cache.html new file mode 100644 index 0000000..da4ce9a --- /dev/null +++ b/beego_demo1/views/cache/cache.html @@ -0,0 +1,10 @@ + + +
+ +
diff --git a/beego_demo1/views/test_orm/orm2_list.html b/beego_demo1/views/test_orm/orm2_list.html
index f9fc198..22f32db 100644
--- a/beego_demo1/views/test_orm/orm2_list.html
+++ b/beego_demo1/views/test_orm/orm2_list.html
@@ -12,11 +12,13 @@
标题
作者
+ 阅读量
{{range $i,$v := .articles}}
diff --git a/beego_demo1/views/test_orm/orm4.html b/beego_demo1/views/test_orm/orm4.html new file mode 100644 index 0000000..7253be2 --- /dev/null +++ b/beego_demo1/views/test_orm/orm4.html @@ -0,0 +1,10 @@ + + +
+ +
+ +
+