Code Monkey home page Code Monkey logo

struct2sql's Introduction

struct to sql

使用struct的形式生成sql语句,支持1对1,1对多,多对多

指定tag进行操作, s2sql:"-"忽略, s2sql:relation_field:xxx 指定对应关系字段,用于ognl取出, s2sql:alias:xxx 指定生成sql列别名, struct实现 func TableName() string 修正table name

package main

import "github.com/songzhibin97/struct2sql"

type Student struct {
	StudentID   uint     `json:"student_id"`
	StudentName string   `json:"student_name"`
	Age         int      `json:"age"`
	Class       Class    `json:"class" s2sql:"relation_field:ClassID"`
	HobbyList   []Hobby  `json:"hobby_list" s2sql:"relation_field:HobbyID"`
	CourseList  []Course `json:"course_list" s2sql:"relation_field:CourseID"`
}

type Class struct {
	ClassID   uint   `json:"class_id"`
	ClassName string `json:"class_name"`
	Level     int    `json:"level"`
}

type Hobby struct {
	HobbyID   uint   `json:"hobby_id"`
	HobbyName string `json:"hobby_name"`
}

type Course struct {
	CourseID    uint      `json:"course_id"`
	StudentList []Student `json:"student_list"  s2sql:"relation_field:StudentID"`
}

func main() {
	s := struct2sql.NewStruct2Sql()
	var v, err = s.BuildInstall(Student{
		StudentID:   1,
		StudentName: "bin",
		Age:         2,
		Class: Class{
			ClassID:   1,
			ClassName: "向日葵",
			Level:     1,
		},
		HobbyList: []Hobby{
			{
				HobbyID:   1,
				HobbyName: "唱",
			},
			{
				HobbyID:   2,
				HobbyName: "跳",
			},
			{
				HobbyID:   3,
				HobbyName: "Rap",
			},
			{
				HobbyID:   4,
				HobbyName: "篮球",
			},
		},
		CourseList: []Course{
			{
				CourseID: 10,
			},
			{
				CourseID: 20,
			},
		},
	})
	if err != nil {
		panic(err)
	}
	fmt.Println(v)
	// [{INSERT INTO Student (Age,Class,StudentID,StudentName) VALUES (?,?,?,?) [2 1 1 bin]} {INSERT INTO Student (Age,Class,StudentID,StudentName) VALUES (?,?,?,?) [2 1 1 bin]} {INSERT INTO Student (Age,Class,StudentID,StudentName) VALUES (?,?,?,?) [2 1 1 bin]} {INSERT INTO Student (Age,Class,StudentID,StudentName) VALUES (?,?,?,?) [2 1 1 bin]} {INSERT INTO Student (Age,Class,StudentID,StudentName) VALUES (?,?,?,?) [2 1 1 bin]} {INSERT INTO Student (Age,Class,StudentID,StudentName) VALUES (?,?,?,?) [2 1 1 bin]} {INSERT INTO Student (Age,Class,StudentID,StudentName) VALUES (?,?,?,?) [2 1 1 bin]} {INSERT INTO Student (Age,Class,StudentID,StudentName) VALUES (?,?,?,?) [2 1 1 bin]} {INSERT INTO Student_Course (CourseID,StudentID) VALUES (?,?) [10 1]} {INSERT INTO Student_Course (CourseID,StudentID) VALUES (?,?) [20 1]} {INSERT INTO Student_Course (CourseID,StudentID) VALUES (?,?) [10 1]} {INSERT INTO Student_Course (CourseID,StudentID) VALUES (?,?) [20 1]} {INSERT INTO Student_Course (CourseID,StudentID) VALUES (?,?) [10 1]} {INSERT INTO Student_Course (CourseID,StudentID) VALUES (?,?) [20 1]} {INSERT INTO Student_Course (CourseID,StudentID) VALUES (?,?) [10 1]} {INSERT INTO Student_Course (CourseID,StudentID) VALUES (?,?) [20 1]}]
}

struct2sql's People

Contributors

songzhibin97 avatar

Stargazers

ipanghu avatar SliverHorn avatar  avatar

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.