router.go

package routers

import (
"gowebProject/controllers" "github.com/astaxie/beego"
) func init() {
beego.Router("/users/user", &controllers.UserController{}, "Post:AddUser")
}

myModules.go

package models

import (
"fmt"
"time" "github.com/astaxie/beego"
"github.com/astaxie/beego/orm"
) type User struct {
Id int64 `json:"id"` //定义json中字段和module的映射关系
Name string `json:"name"`
Email string `json:"email"`
Age int64 `json:"age"`
}
func CreateUser(user *User) {
	o := orm.NewOrm()
	if successNums, err := o.Insert(user); err != nil {
		fmt.Println("insert fail.....")
	} else {
		fmt.Println("success inserted %d datas", successNums)
	}
}

userController.go

func (this *UserController) AddUser() {
var ob md.User //这是一个model,struct类型
body := this.Ctx.Input.RequestBody //这是获取到的json二进制数据
json.Unmarshal(body, &ob) //解析二进制json,把结果放进ob中
// user := &md.User{Id: ob.UserName, Mobile: ob.Mobile} md.CreateUser(&ob) //这是添加用户函数
// if nil != err {
// this.Data["json"] = map[string]interface{}{"result": false, "msg": err}
// } else {
// this.Data["json"] = map[string]interface{}{"result": true, "msg": "新增成功"}
// }
// this.Data["json"] = map[string]interface{}{"result": false, "msg": err}
this.Data["json"] = ob
this.ServeJSON()
}

golang web框架 beego 学习 (六) request body和module的映射的更多相关文章

  1. golang web框架 beego 学习 (一) 环境搭建

    下面的命令我都是在$GOPATH的路径下执行的: 1. 首先下载beego框架: go get github.com/astaxie/beego (注意:运行上面命令时没有反应,需要在etc/host ...

  2. golang web框架 beego 学习 (三) beego获取参数

    直接上常用的例子吧: A:     获取URL中的参数 router func init() { beego.Router("/task/?:id/?:name", &co ...

  3. golang web框架 beego 学习 (四) 连接mysql

    1 DB参数配置在app.conf appname = gowebProject httpport = runmode = dev [db] host= localhost port= databas ...

  4. golang web框架 beego 学习 (二) router and controller

    1 Router和Controller的常用配置 beego.Router("/user/admin", &controllers.UserController{}) // ...

  5. golang web框架 beego 学习 (五) 配置文件

    app.conf: appname = gowebProject httpport = runmode = dev copyrequestbody = true [db] host= localhos ...

  6. golang web框架 beego 学习 (七)json转数组

    Modules type User struct { Id int64 `json:"id"` Name string `json:"name"` Email ...

  7. golang web框架 beego

    尝试了下,在go环境ready的情况下,花了2分钟完成了beego安装.项目生成和启动,效率还是不错的 1.安装: go get github.com/astaxie/beego go get git ...

  8. golang web框架设计2:自定义路由

    继续学习谢大的Go web框架设计 HTTP路由 http路由负责将一个http的请求交到对应的函数处理(或者一个struct的方法),路由在框架中相当于一个事件处理器,而这个时间包括 用户请求的路径 ...

  9. Go语言及Web框架Beego环境无脑搭建

    [原]Go语言及Web框架Beego环境无脑搭建 本文涉及软件均以截至到2013年10月12日的最新版本为准 1. 相关软件准备: 1) go1.2rc1.windows-386.msi,对应32位w ...

随机推荐

  1. 关于File.separator 文件路径:wind与linux下路径问题 .

    最近有个在页面上传Excel文件至服务器指定目录并进行数据校验.最后入库及进行进一步处理的应用情境,我写好代码在模拟环境下测试,完全没问题:但客户试用的时候,却老是报告“No such file or ...

  2. mysql删除完全重复数据保留一条

    CREATE TABLE tmp AS (SELECT DISTINCT * FROM oa_organization);--将不重复的数据存入新建临时表tmp DELETE FROM oa_orga ...

  3. Spring AOP的作用,动态代理模式

    AOP即面向切面编程.AOP是基于代理模式的. 代理模式: 当我们需要修改一个类,在类中加入代码时,为了不破坏这个类的封装性.可以使用代理模式,建立一个代理类. 比如:修改需求,在调用UserCont ...

  4. scheduled定时任务+实例请求数据库

    1.scheduled定时任务类:ScheduledDemo.java package com.nantian.scheduled; import java.util.Date; import org ...

  5. loj 2011

    对于第 $i$ 天的询问前 $i - c - 1$ 天都会影响答案主席树维护 #include <iostream> #include <cstdio> #include &l ...

  6. TensorFlow(八):tensorboard可视化

    import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data from tensorflow.c ...

  7. fasttext模型 训练THUCNews

    # _*_coding:utf-8 _*_ import fasttext import jieba from sklearn import metrics import random def rea ...

  8. python合并多个txt文件

    python合并多个txt文件 #合并一个文件夹下的多个txt文件 #coding=utf-8 import os #获取目标文件夹的路径 filedir = os.getcwd()+'\\数据' # ...

  9. javaSE集合---进度2

    一.集合框架 1.特点 对象封装数据,对象多了也需要存储,集合用于存储对象. 对象的个数确定可以使用数组,但是不确定的话,可以用集合,因为集合是可变长度的. 2.集合和数组的区别 数组是固定长度的,集 ...

  10. 关于Scanner调用 sc.nextInt() 异常try后不能二次输入导致死循环问题

    先看下简化的代码,引出问题所在: public class Run { public static void main(String[] args) { Scanner sc = new Scanne ...