Giter Site home page Giter Site logo

atrz's Introduction

atrz

简介

在hertz框架中注册增删改查的路由,只需要增加一行代码即可拥有所有表的增删改查操作,列表查询支持字段名的别名、根据字段分组、分页、排序、自定义解析规则等便捷操作。

如examples/simple中main.go的代码

func main() {
	z := server.Default()
	dal.MysqlSetup()
	atrz.SetUp(dal.DB, z)

	z.Spin()
}

创建测试用表

准备工作创建表atm.student 账号密码请修改example/*/dal/mysql.go 中的配置

var dsn = "root:12345678@tcp(localhost:3306)/atm?charset=utf8&parseTime=True&loc=Local"
create table student (
    id bigint unsigned NOT NULL AUTO_INCREMENT COMMENT 'PK',
    name varchar(128) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' ,
    province varchar(128) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' ,
    gender int NOT NULL DEFAULT '0' ,
    age int NOT NULL DEFAULT '0',
    class int NOT NULL DEFAULT '0',
    created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ,
    updated_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

运行

运行 examples/main.go main 方法

创建接口

POST /atr/create/:model

创建5个student

  curl -X POST 127.0.0.1:8888/atr/create/student -H 'Content-Type: application/json' -d "{\"name\":\"Beatty\",\"gender\":1,\"age\":12,\"class\":\"1\",\"province\":\"hebei\"}"
  curl -X POST 127.0.0.1:8888/atr/create/student -H 'Content-Type: application/json' -d "{\"name\":\"Bell\",\"gender\":1,\"age\":8,\"class\":\"2\",\"province\":\"hunan\"}"
  curl -X POST 127.0.0.1:8888/atr/create/student -H 'Content-Type: application/json' -d "{\"name\":\"Abbey\",\"gender\":2,\"age\":8,\"class\":\"1\",\"province\":\"beijin\"}"
  curl -X POST 127.0.0.1:8888/atr/create/student -H 'Content-Type: application/json' -d "{\"name\":\"Adele\",\"gender\":2,\"age\":11,\"class\":\"2\",\"province\":\"beijin\"}"
  curl -X POST 127.0.0.1:8888/atr/create/student -H 'Content-Type: application/json' -d "{\"name\":\"Beckman\",\"gender\":1,\"age\":11,\"class\":\"2\",\"province\":\"henan\"}"

成功响应

{"code":0,"data":null,"message":""}

失败响应

{"code":-1,"data":null,"message":""}
list接口

Get /atr/list/:model/:page

curl http://localhost:8888/atr/list/student/2?class=1&_order=age,desc&_order=gender&_size=1
{
  "code":0,
  "data":{
    "total":2,
    "data":[
      {
        "age":8,
        "class":1,
        "created_at":"2024-02-05T15:06:57+08:00",
        "gender":2,
        "id":3,
        "name":"Abbey",
        "province":"beijin",
        "updated_at":"2024-02-05T15:06:57+08:00"
      }
    ]
  },
  "message":""
}
info接口

GET /atr/info/:model/:id

curl http://localhost:8888/atr/info/student/1
{
    "code":0,
    "data":{
        "age":12,
        "class":1,
        "created_at":"2024-02-05T15:06:57+08:00",
        "gender":1,
        "id":1,
        "name":"Beatty",
        "province":"hebei",
        "updated_at":"2024-02-05T15:06:57+08:00"
    },
    "message":""
}

POST /atr/update/:model

update接口
curl -X POST 127.0.0.1:8888/atr/update/student/5 -H 'Content-Type: application/json' -d "{\"name\":\"Sandra\",\"gender\":1,\"age\":13,\"class\":\"2\"}"
{"code":0,"data":null,"message":""}
delete 接口

POST /atr/delete/:model/:id

curl -X POST 127.0.0.1:8888/atr/delete/student/5
{"code":0,"data":null,"message":""}

批量创建接口

POST /atr/batch/create/:model

 curl -X POST 127.0.0.1:8888/atr/batch/create/student -H 'Content-Type: application/json' -d "[{\"name\":\"Scarlet\",\"gender\":2,\"age\":8,\"class\":\"3\",\"province\":\"beijin\"},{\"name\":\"Tania\",\"gender\":2,\"age\":9,\"class\":\"2\",\"province\":\"hebei\"}]"
{"code":0,"data":null,"message":""}

路由分组

RouterPrefix       = "/atr"
CustomRouterPrefix = "/custom"

默认路由有/atr的前缀 这个是可以设置的通过WithRouterPrefix 需要注意如果使用了自定义路由,自定义路由的前缀和默认路由前缀不能一样,因为绑定了不同的middleware分组。

更多功能

绑定模型 绑定模型可以通过模型的注解完成参数校验,和对gorm注解的支持
代码结构
自定义路由
自定义控制器
自定义参数解析 list接口支持
创建前数据完善 create/update/batchcreate 接口在插入数据前可以自定对参数的补充变形,插入和修改前的数据完善
批量创建前数据完善 create/update/batchcreate 接口在插入数据前可以自定对参数的补充变形,插入和修改前的数据完善
修改前数据完善 create/update/batchcreate 接口在插入数据前可以自定对参数的补充变形,插入和修改前的数据完善
展示字段配置 Select/Hidden/AddColumns 对展示数据进行配置、隐藏、扩充
控制访问

list支持分组和聚合函数 参考 examples/custom_route

atrz's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.