beego如何根据时间查询?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了beego如何根据时间查询?相关的知识,希望对你有一定的参考价值。

beego mysql如何时间筛选当日、本周、本月数据?采用高级查询
num, err := o.QueryTable("Fruits").Filter("Shi","2021").Limit(page_size, tebs).All(&posts)

参考技术A 在etc/nginx目录下,新建一个文件夹server,在文件夹里面新建.conf文件。然后把下面的代码拷贝进去就好了,如图: 然后在etc/nginx/nginix.conf 主配置文件里用Include引入这个文件就好了。如图:然后重启一下服务就好了,重启Nginx就好了,重启命令:service nginx restart。是不是很简单, `(*∩_∩*)′。 此外还有,另外一个编译的小细节要注意的是,开发是的模式是dev,编译部署之前要把模式修改为prod,除了要改conf里面的,还要改main函数里面的,不然会有问题。 参考技术B 在etc/nginx目录下,新建一个文件夹server,在文件夹里面新建.conf文件。然后把下面的代码拷贝进去就好了,如图: 然后在etc/nginx/nginix.conf 主配置文件里用Include引入这个文件就好了。如图:然后重启一下服务就好了,重启Nginx就好了,重启命令:service nginx restart。是不是很简单, `(*∩_∩*)′。 此外还有,另外一个编译的小细节要注意的是,开发是的模式是dev,编译部署之前要把模式修改为prod,除了要改conf里面的,还要改main函数里面的,不然会有问题。 参考技术C 在etc/nginx目录下,新建一个文件夹server,在文件夹里面新建.conf文件。然后把下面的代码拷贝进去就好了,如图: 然后在etc/nginx/nginix.conf 主配置文件里用Include引入这个文件就好了。如图:然后重启一下服务就好了,重启Nginx就好了,重启命令:service nginx restart。是不是很简单, `(*∩_∩*)′。 此外还有,另外一个编译的小细节要注意的是,开发是的模式是dev,编译部署之前要把模式修改为prod,除了要改conf里面的,还要改main函数里面的,不然会有问题。 参考技术D 在etc/nginx目录下,新建一个文件夹server,在文件夹里面新建.conf文件。然后把下面的代码拷贝进去就好了,如图: 然后在etc/nginx/nginix.conf 主配置文件里用Include引入这个文件就好了。如图:然后重启一下服务就好了,重启Nginx就好了,重启命令:service nginx restart。是不是很简单, `(*∩_∩*)′。 此外还有,另外一个编译的小细节要注意的是,开发是的模式是dev,编译部署之前要把模式修改为prod,除了要改conf里面的,还要改main函数里面的,不然会有问题。 第5个回答  2021-11-08 问题 使用beego的orm时,mysql数据库为datetime格式的字段插入之后,数据库看到的时间会比正常时间靠前8个小时 解决 连接数据库时加上loc=Local即可,比如root:root...追问

你好,请问beego获取的时间为空怎么办,其他字段都可以获取到,数据库中也有时间字段,就是获取不到

我的写法

数据库连接

麻烦帮我看看,到底是哪里出了问题

本回答被提问者采纳

Beego框架 get请求编写

目录


根据业务需求,就是需要查询两表的数据,我用了一个笨方法。直接上代码

代码

controllers

// GetAll ...
// @Title Get All
// @Description get TestData
// @Param	query	query	string	false	"Filter. e.g. col1:v1,col2:v2 ..."
// @Param	fields	query	string	false	"Fields returned. e.g. col1,col2 ..."
// @Param	sortby	query	string	false	"Sorted-by fields. e.g. col1,col2 ..."
// @Param	order	query	string	false	"Order corresponding to each sortby field, if single value, apply to all sortby fields. e.g. desc,asc ..."
// @Param	limit	query	string	false	"Limit the size of result set. Must be an integer"
// @Param	offset	query	string	false	"Start position of result set. Must be an integer"
// @Success 200 object models.TestData
// @Failure 403
// @router / [get]
func (c *TestDeviceDataController) GetAll() 
	//TestDevice variable
	var imei string
	var producer string
	var productName string
	// var productTime  time.Time
	var testResult string
	// var testTime     time.Time
	var tester string
	var testerJobnum string

	//TestData variable
	var testData string
	var testItem string
	var deviceId int

	var limit int = 10
	var offset int = 0

	if v, err := c.GetInt("pageSize"); err == nil 
		limit = v
	
	if v, err := c.GetInt("current"); err == nil 
		offset = (v - 1) * limit
	
	if v := c.GetString("imei"); v != "" 
		imei = v
	
	if v := c.GetString("producer"); v != "" 
		producer = v
	
	if v := c.GetString("productName"); v != "" 
		productName = v
	
	// if v := c.GetString("productTime"); v != "" 
	// 	productTime = v
	// 
	if v := c.GetString("testResult"); v != "" 
		testResult = v
	
	// if v := c.GetString("testTime"); v != "" 
	// 	testTime = v
	// 
	if v := c.GetString("tester"); v != "" 
		tester = v
	
	if v := c.GetString("testerJobnum"); v != "" 
		testerJobnum = v
	
	if v := c.GetString("testData"); v != "" 
		testData = v
	
	if v := c.GetString("testItem"); v != "" 
		testItem = v
	
	if v, err := c.GetInt("deviceId"); err == nil 
		deviceId = v
	
	l, total := models.GetAllTestDeviceData(limit, offset, imei, producer, productName, testResult, tester, testerJobnum, testData, testItem, deviceId)

	c.SuccessData(ListDataData: l, Total: total)

models

func GetAllTestDeviceData(limit int, offset int, imei string, producer string, productName string, testResult string,
	tester string, testerJobnum string, testData string, testItem string, deviceId int) (data []TestDevice, total int64) 
	var testDeviceData []TestDevice
	var ctestData []*TestData
	o := orm.NewOrm()

	qs := o.QueryTable(new(TestDevice).TableName())
	qd := o.QueryTable(new(TestData).TableName())
	if imei != "" 
		qs = qs.Filter("imei", imei)
	
	if producer != "" 
		qs = qs.Filter("producer", producer)
	
	if productName != "" 
		qs = qs.Filter("productName", productName)
	
	if testResult != "" 
		qs = qs.Filter("testResult", testResult)
	
	if tester != "" 
		qs = qs.Filter("tester", tester)
	
	if testerJobnum != "" 
		qs = qs.Filter("testerJobnum", testerJobnum)
	
	if testData != "" 
		qd = qd.Filter("testData", testData)
	
	if testItem != "" 
		qd = qd.Filter("testItem", testItem)
	
	// if deviceId >= 0 
	// 	qs = qs.Filter("deviceId", deviceId)
	// 
	total, _ = qs.Count()

	qs.Limit(limit).Offset(offset).OrderBy("-id").All(&testDeviceData)

	for k, v := range testDeviceData 
		qd.Filter("test_device_id", v.Id).All(&ctestData)
		// v.DevGetJsonData = ctestData
		testDeviceData[k].DevGetJsonData = ctestData
		fmt.Println(v)
	

	return testDeviceData, total

查询结果


  "code": 0,
  "msg": "success",
  "data": 
    "data": [
      
        "Id": 6,
        "dev_imei": "222222222264456",
        "dev_producer": "Manufacturer Name",
        "dev_producer_name": "smart_city_cover",
        "dev_producer_time": "2022-03-05T00:00:00+08:00",
        "dev_test_result": "ko",
        "dev_test_time": "2022-03-09T08:41:45+08:00",
        "dev_tester": "fgh",
        "dev_tester_jobnum": "fds",
        "data": [
          
            "Id": 61,
            "devdata_test_data": "ui,",
            "devdata_test_item": "rteg",
            "devdata_test_device_id": 6,
            "data": null
          ,
          
            "Id": 62,
            "devdata_test_data": "bffgsdf",
            "devdata_test_item": "qweqrtew",
            "devdata_test_device_id": 6,
            "data": null
          
        ]
      ,
      
        "Id": 5,
        "dev_imei": "222222222264453",
        "dev_producer": "Manufacturer Name",
        "dev_producer_name": "smart_city_cover",
        "dev_producer_time": "2022-03-05T00:00:00+08:00",
        "dev_test_result": "bfghn",
        "dev_test_time": "2022-03-07T09:11:18+08:00",
        "dev_tester": "fgh",
        "dev_tester_jobnum": "fds",
        "data": [
          
            "Id": 49,
            "devdata_test_data": "kk,",
            "devdata_test_item": "rteg",
            "devdata_test_device_id": 5,
            "data": null
          ,
          
            "Id": 50,
            "devdata_test_data": "bffgsdf",
            "devdata_test_item": "qweqrtew",
            "devdata_test_device_id": 5,
            "data": null
          
        ]
      ,
      
        "Id": 4,
        "dev_imei": "222222222266442",
        "dev_producer": "Manufacturer Name",
        "dev_producer_name": "smart_city_cover",
        "dev_producer_time": "2022-03-05T00:00:00+08:00",
        "dev_test_result": "bfghn",
        "dev_test_time": "2022-03-07T09:11:18+08:00",
        "dev_tester": "fgh",
        "dev_tester_jobnum": "fds",
        "data": [
          
            "Id": 33,
            "devdata_test_data": "kk,",
            "devdata_test_item": "rteg",
            "devdata_test_device_id": 4,
            "data": null
          ,
          
            "Id": 34,
            "devdata_test_data": "bffgsdf",
            "devdata_test_item": "qweqrtew",
            "devdata_test_device_id": 4,
            "data": null
          
        ]
      ,
      
        "Id": 3,
        "dev_imei": "222222222266441",
        "dev_producer": "Manufacturer Name",
        "dev_producer_name": "smart_city_cover",
        "dev_producer_time": "2022-03-05T00:00:00+08:00",
        "dev_test_result": "bfghn",
        "dev_test_time": "2022-03-07T09:11:18+08:00",
        "dev_tester": "fgh",
        "dev_tester_jobnum": "fds",
        "data": [
          
            "Id": 31,
            "devdata_test_data": "kk,",
            "devdata_test_item": "rteg",
            "devdata_test_device_id": 3,
            "data": null
          ,
          
            "Id": 32,
            "devdata_test_data": "bffgsdf",
            "devdata_test_item": "qweqrtew",
            "devdata_test_device_id": 3,
            "data": null
          
        ]
      
    ],
    "total": 4
  

限制页码查询

current 代表页数
pageSize 代表每页显示几个
http://localhost:8080/v1/testDeviceData?pageSize=2&current=1


  "code": 0,
  "msg": "success",
  "data": 
    "data": [
      
        "Id": 6,
        "dev_imei": "222222222264456",
        "dev_producer": "Manufacturer Name",
        "dev_producer_name": "smart_city_cover",
        "dev_producer_time": "2022-03-05T00:00:00+08:00",
        "dev_test_result": "ko",
        "dev_test_time": "2022-03-09T08:41:45+08:00",
        "dev_tester": "fgh",
        "dev_tester_jobnum": "fds",
        "data": [
          
            "Id": 61,
            "devdata_test_data": "ui,",
            "devdata_test_item": "rteg",
            "devdata_test_device_id": 6,
            "data": null
          ,
          
            "Id": 62,
            "devdata_test_data": "bffgsdf",
            "devdata_test_item": "qweqrtew",
            "devdata_test_device_id": 6,
            "data": null
          
        ]
      ,
      
        "Id": 5,
        "dev_imei": "222222222264453",
        "dev_producer": "Manufacturer Name",
        "dev_producer_name": "smart_city_cover",
        "dev_producer_time": "2022-03-05T00:00:00+08:00",
        "dev_test_result": "bfghn",
        "dev_test_time": "2022-03-07T09:11:18+08:00",
        "dev_tester": "fgh",
        "dev_tester_jobnum": "fds",
        "data": [
          
            "Id": 49,
            "devdata_test_data": "kk,",
            "devdata_test_item": "rteg",
            "devdata_test_device_id": 5,
            "data": null
          ,
          
            "Id": 50,
            "devdata_test_data": "bffgsdf",
            "devdata_test_item": "qweqrtew",
            "devdata_test_device_id": 5,
            "data": null
          
        ]
      
    ],
    "total": 4
  

m2m

后面同事跟我说有个多对多模型定义,我摸了半天没摸懂,看了go - 在beego orm中插入具有m2m的模型文章仿写了下

点击进入手册,看多对多关系

开始我一直折腾模型定义的rel_table和rel_through,以及参考文章后面发现没有调用m2m,我也不知道啥问题

func M2mTest() 
	o := orm.NewOrm()
	var data TestData
	data.DataTestData = "CoolDude"
	id, err := o.Insert(&data)
	if err != nil 
		log.Printf(err.Error())
	 else 
		log.Printf("Player ID: %v", id)
	

	var device TestDevice
	id, err = o.Insert(&device)
	if err != nil 
		log.Printf(err.Error())
	 else 
		log.Printf("Game ID: %v", id)
	

	m2m := o.QueryM2M(&device, "DevGetJsonData")
	num, err := m2m.Add(data)
	fmt.Println(m2m.Add(data))
	if err == nil 
		log.Printf("Added nums: %v", num)
	
	// m2m.Remove(63)

以上是关于beego如何根据时间查询?的主要内容,如果未能解决你的问题,请参考以下文章

Beego框架 get请求编写

beego利用casbin进行权限管理——第四节 策略更新

Beego 框架文件存储

Beego 框架文件存储

beego——过滤器

Beego 过滤器